Is the browsermob-proxy gem for Ruby not working for anyone else? - ruby

I've been trying to implement Browsermob Proxy to detect network traffic for something I'm trying to automate, and I've copied the example code in the Github repository to my own code, but the terminal tells me the proxy.har (the har portion) is an undefined method. Upon further inspection, the proxy.rb file is pretty much completely empty, without any defined methods nor constructors.
Here's the github repo:
https://github.com/jarib/browsermob-proxy-rb
Can anyone tell me what's going on here?
Before ('#selenium_firefox_networkproxy') do
#server = BrowserMob::Proxy::Server.new("/Users/eliotchan/.rvm/gems/ruby-1.9.2-p320#cucumber/gems/browsermob-proxy-0.0.9/lib/browsermob-proxy.rb")
#server.start
#proxy = server.create_proxy
#profile = Selenium::WebDriver::Firefox::Profile.new
#profile.proxy = proxy.selenium_proxy
#driver = Selenium::WebDriver.for(
:remote,
:url => "http://"+FIREFOX_IP+"/wd/hub",
:desired_capabilities => :firefox,
:profile => #profile)
#proxy.new_har "Analytics"
#driver.manage.timeouts.implicit_wait = 30
#verification_errors = []
end
After ('#selenium_firefox_networkproxy') do
#harfile = #proxy.har
#har.save_to "/Users/eliotchan/Documents/Analytics.har"
#proxy.close
#driver.quit
#verification_errors.should == []
end
The error I get is undefined method `har' for nil:NilClass (NoMethodError)

Related

How to create chrome profile via Ruby Selenium Binding(or WATIR)

I know how to create profile for Firefox
require 'watir'
options = Selenium::WebDriver::Firefox::Options.new
options.profile = "default"
#driver = Selenium::WebDriver.for :firefox, options: options
#b = Watir::Browser.new #driver
But when I do same thing for Chrome it's not creating, Infact I realized that options(please look above) object doesn't even have the method profile= so I try adding profile like as given below(I saw how people are creating in Java Selenium Binding so I have done the same but it's not working here)
options = Selenium::WebDriver::Firefox::Options.new
options.add_argument('user-data-dir=C:\Users\user_name\AppData\Local\Google\Chrome\User Data\Default')
#driver = Selenium::WebDriver.for :chrome, options: options
Can someone help me how to create Chrome Profile via Ruby Selenium binding(or WATIR)?
Using an existing or creating a new profile can be done via Chromedrivers user-data-dir argument. In Watir, you can pass the argument via the :args parameter:
browser = Watir::Browser.new :chrome,
args: ['user-data-dir=C:\Users\user_name\AppData\Local\Google\Chrome\User Data']
Note that if you trying to use the existing default profile, you do not want to include the "Default" directory in the path.
I made a function for creating new browser. You can use it.
def new_browser
if Rails.env.production?
chrome_bin = ENV.fetch('GOOGLE_CHROME_SHIM', nil)
Selenium::WebDriver::Chrome.path = "/app/.apt/usr/bin/google-chrome"
Selenium::WebDriver::Chrome.driver_path = "/app/vendor/bundle/bin/chromedriver"
end
profile = Selenium::WebDriver::Chrome::Profile.new
profile['general.useragent.override'] = 'Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10'
driver = Selenium::WebDriver.for :chrome, :profile => profile
pid = driver.instance_variable_get(:#service).instance_variable_get(:#process).instance_variable_get(:#pid)
begin
browser = Watir::Browser.new driver
rescue => e
system("kill -9 #{#pid}")
end
return {:browser => browser , :pid => pid}
end
Since you asked in the comments for a more detailed explanation for Capybara, I post it as an answer (although you seem to already have a working solution now - sorry for the delayed answer).
In my rails projects I usually configure the Selenium chrome driver as follows:
gem 'chromedriver-helper'
in the Gemfile (or install it locally). Then in a system-test initializer define
Capybara.register_driver :selenium_chrome_headless_no_sandbox do |app|
browser_options = ::Selenium::WebDriver::Chrome::Options.new
browser_options.args << '--headless'
browser_options.args << '--disable-gpu'
browser_options.args << '--no-sandbox'
Capybara::Selenium::Driver.new(app, browser: :chrome, options: browser_options)
end
and later (configuring RSpec) I set it as the used driver like:
RSpec.configure do |config|
config.before(:each, type: :system, js: true) do
driven_by :selenium_chrome_headless_no_sandbox
end
end
Maybe this helps someone. Cheers
edit: added chromedriver-helper

file_detector error watir webdriver

I need to upload file through file field form. Locally it works fine. But through Selenium Grid test need to upload file to remote machine.
The solution is using file detector. As described here https://github.com/watir/watir-webdriver/issues/175 or here https://saucelabs.com/resources/selenium-file-upload
In my hooks.rb
require "watir-webdriver"
client = Selenium::WebDriver::Remote::Http::Default.new
capabilities = Selenium::WebDriver::Remote::Capabilities.new(browser_name: ENV['BROWSER'].to_sym, :http_client => client)
browser = if ENV['REMOTE']
Watir::Browser.new(
:remote,
url: 'http://remoteurl:4444/wd/hub',
desired_capabilities: capabilities,
:http_client => client
)
else
Watir::Browser.new(ENV['BROWSER'].to_sym, :http_client => client)
end
browser.driver.file_detector = lambda do |args|
str = args.first.to_s
str if File.exist?(str)
end
But when I run tests they end up with error:
undefined method `file_detector=' for #<Selenium::WebDriver::Driver:0x000000012902b0> (NoMethodError)
How can I upload file via Selenium Grid?
Looking through the code, the file_detector= method is defined in the Selenium::WebDriver::DriverExtensions::UploadFiles module. The only bridge that uses this driver extension is the Selenium::WebDriver::Remote::Bridge class.
In other words, the method will only be available to browsers created using the :remote type:
browser = Watir::Browser.new(:remote)
I assume your are getting this exception when using non-remote drivers (ie when going down the "else" part of the "if" statement). Try moving the setting of the file_detector to only be when using a remote driver.
if ENV['REMOTE']
browser = Watir::Browser.new(
:remote,
url: 'http://remoteurl:4444/wd/hub',
desired_capabilities: capabilities,
:http_client => client
)
browser.driver.file_detector = lambda do |args|
str = args.first.to_s
str if File.exist?(str)
end
else
browser = Watir::Browser.new(ENV['BROWSER'].to_sym, :http_client => client)
end

need changes to watir/loader.rb to make webdriver-user-agent work

I am using webdriver-user-agent mentioned here – http://watirwebdriver.com/mobile-devices/
This is the code I am using when trying out this gem
Browser: FF/Chrome
Ruby: 1.9.3 / Selenium :2.30.0 / Watir : 4.0.2
http_client = Selenium::WebDriver::Remote::Http::Default.new
http_client.timeout = HTTP_TIMEOUT
profile = Selenium::WebDriver::Firefox::Profile.new
device = ENV["DEVICE"]
orientation = ENV["ORIENTATION"]
driver = UserAgent.driver(:browser => :firefox, :agent =>device, :orientation=>orientation)
devices = UserAgent.resolution_for(device,orientation)
UserAgent.resize_inner_window(driver,devices[0],devices[1])
Watir::Browser.new driver
Now when the last statement is executed, i get the following error
(STEP) Launching FIREFOX (using web driver user agent)……
browser:
#
undefined method `to_sym’ for # (NoMethodError)
/Users/user/.rvm/gems/ruby-1.9.3-p194/gems/watir-4.0.2/lib/watir/loader.rb:42:in `load_driver_for’
/Users/user/.rvm/gems/ruby-1.9.3-p194/gems/watir-4.0.2/lib/watir/loader.rb:8:in `new’
Based on some investigation, problem is happening at highlighted line below as its trying to .to_sym on the selenium webdriver object.
def load_driver_for(browser)
if browser && browser.to_sym != :ie && Watir.driver == :classic
Watir.driver = :webdriver
end
Watir.load_driver
end
But if we add a line like given below, this gem is working as expected.
def load_driver_for(browser)
if “#{ENV["BROWSER"]}”.eql?(“chrome_useragent”)||”#{ENV["BROWSER"]}”.eql?(“firefox_useragent”)
Watir.driver = :webdriver
else
if browser && browser.to_sym != :ie && Watir.driver == :classic
Watir.driver = :webdriver
end
Watir.load_driver
end
end
since this is watir code outside of our framework, this is not the right way to do this, any suggestion on how to avoid this situation ?
The problem is reproducible when you do:
require 'watir'
require 'webdriver-user-agent'
driver = Webdriver::UserAgent.driver(:browser => :chrome, :agent => :iphone, :orientation => :landscape)
browser = Watir::Browser.new driver
browser.goto 'tiffany.com'
browser.url.should == 'http://m.tiffany.com/International.aspx'
You can fix the issue by requiring watir-webdriver directly instead of through the watir metagem. Change the first line to:
require 'watir-webdriver'

How do I use selenium with Ruby?

I made some tests with the Firefox Selenium and then had it exported to Ruby. Although the tests all ran fine in Firefox, I am having trouble running the same suite in Ruby.
I tried to run one of the example programs they have and I also get the same connection refused error. Here is the error I got when trying to run their google_test suite.
tellingsen$ ruby google_test.rb
Loaded suite google_test
Started
E
Finished in 0.001558 seconds.
1) Error:
test_page_search(ExampleTest):
Errno::ECONNREFUSED: Connection refused - connect(2)
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:560:in `initialize'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:560:in `open'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:560:in `connect'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/timeout.rb:62:in `timeout'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/timeout.rb:93:in `timeout'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:560:in `connect'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:553:in `do_start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:542:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:1035:in `request'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:845:in `post'
/Users/tellingsen/.gem/ruby/1.8/gems/selenium-client-1.2.18/lib/selenium/client/protocol.rb:89:in `http_post'
/Users/tellingsen/.gem/ruby/1.8/gems/selenium-client-1.2.18/lib/selenium/client/protocol.rb:12:in `remote_control_command'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/timeout.rb:62:in `timeout'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/timeout.rb:93:in `timeout'
/Users/tellingsen/.gem/ruby/1.8/gems/selenium-client-1.2.18/lib/selenium/client/protocol.rb:11:in `remote_control_command'
/Users/tellingsen/.gem/ruby/1.8/gems/selenium-client-1.2.18/lib/selenium/client/protocol.rb:19:in `string_command'
/Users/tellingsen/.gem/ruby/1.8/gems/selenium-client-1.2.18/lib/selenium/client/base.rb:85:in `start_new_browser_session'
google_test.rb:21:in `setup'
1 tests, 0 assertions, 0 failures, 1 errors
Can someone help me with this?
Note:
Mac OS: 10.6.4
Macbook Pro
Ruby: 1.8.7
gem: selenium-client 1.2.18
EDIT
Here is the google_test.rb that I tried
#!/usr/bin/env ruby
#
# Sample Test:Unit based test case using the selenium-client API
#
require "test/unit"
require "rubygems"
gem "selenium-client", ">=1.2.18"
require "selenium/client"
class ExampleTest < Test::Unit::TestCase
attr_reader :browser
def setup
#browser = Selenium::Client::Driver.new \
:host => "localhost",
:port => 4444,
:browser => "*firefox",
:url => "http://www.google.com",
:timeout_in_second => 60
browser.start_new_browser_session
end
def teardown
browser.close_current_browser_session
end
def test_page_search
browser.open "/"
assert_equal "Google", browser.title
browser.type "q", "Selenium seleniumhq"
browser.click "btnG", :wait_for => :page
assert_equal "Selenium seleniumhq - Google Search", browser.title
assert_equal "Selenium seleniumhq", browser.field("q")
assert browser.text?("seleniumhq.org")
assert browser.element?("link=Cached")
end
end
I figured it out after a few hours of searching on forums and through google.
What I needed to do was have the selenium server running for it to work. I was able to download it from this site http://seleniumhq.org/download/ (current: Selenium RC February 23, 2010 1.0.3).
From there I opened up a new terminal and did
cd Downloads/selenium-remote-control-1.0.3/selenium-server-1.0.3
java -jar selenium-server.jar
Then ran my ruby generated script with another terminal window
ruby google_test.rb
And it worked!
This is Selenium Webdriver example for simple google search
Save as google_search.rb
require "selenium-webdriver"
require "test/unit"
class GoogleSearch < Test::Unit::TestCase
def setup
#driver = Selenium::WebDriver.for :firefox
#base_url = "http://www.google.com/"
#driver.manage.timeouts.implicit_wait = 30
#verification_errors = []
end
def teardown
#driver.quit
assert_equal [], #verification_errors
end
def test_google_search
#driver.get(#base_url)
#driver.find_element(:name, "q").clear
#driver.find_element(:name, "q").send_keys "Thiyagarajan Veluchamy"
#driver.find_element(:name, "btnK").click
end
def element_present?(how, what)
#driver.find_element(how, what)
true
rescue Selenium::WebDriver::Error::NoSuchElementError
false
end
def verify(&blk)
yield
rescue Test::Unit::AssertionFailedError => ex
#verification_errors << ex
end
end
$ruby google_search.rb
Here is a much simpler version of the script:
require "selenium-webdriver"
#driver = Selenium::WebDriver.for :chrome
#base_url = "http://www.google.com/"
#driver.get(#base_url)
#driver.find_element(:name, "q").send_keys "Stack Overflow"
Methods available on the #driver object can be found here: http://selenium.googlecode.com/svn/trunk/docs/api/rb/Selenium/WebDriver/Driver.html
find_element gives you access to the Element class. Methods available on the Element class can be found here:
http://selenium.googlecode.com/svn/trunk/docs/api/rb/Selenium/WebDriver/Element.html

Selenium Ruby Reporting

I'm trying to set the environment for testing using Selenium and selenium-client gem.
I prefer unit test style over RSpec style of tests.
Do I have to build my own system for reporting then?
How can I add exception handling without having begin-rescue-end in each test? Is there any way to do that using mixins?
I'm not sure I understand what your question means in terms of reporting but the selenium-client gem handles both BDD and UnitTesting.
Below is code copied from the rubyforge page:
require "test/unit"
require "rubygems"
gem "selenium-client", ">=1.2.16"
require "selenium/client"
class ExampleTest < Test::Unit::TestCase
attr_reader :browser
def setup
#browser = Selenium::Client::Driver.new \
:host => "localhost",
:port => 4444,
:browser => "*firefox",
:url => "http://www.google.com",
:timeout_in_second => 60
browser.start_new_browser_session
end
def teardown
browser.close_current_browser_session
end
def test_page_search
browser.open "/"
assert_equal "Google", browser.title
browser.type "q", "Selenium seleniumhq"
browser.click "btnG", :wait_for => :page
assert_equal "Selenium seleniumhq - Google Search", browser.title
assert_equal "Selenium seleniumhq", browser.field("q")
assert browser.text?("seleniumhq.org")
assert browser.element?("link=Cached")
end
end
As for exception handling, UnitTesting handles the exceptions with an Error message.
That being said, I may have misunderstood your question.
Initial build of Extent is available for Ruby. You can view the sample here. Latest source is available at github.
Sample code:
# main extent instance
extent = RelevantCodes::ExtentReports.new('extent_ruby.html')
# extent-test
extent_test = extent.start_test('First', 'description string')
# logs
extent_test.log(:pass, 'step', 'details')
extent.end_test(extent_test)
# flush to write everything to html file
extent.flush

Resources