Set proxy for selenium chrome driver in ruby - ruby

Browsermob proxy:-
https://github.com/jarib/browsermob-proxy-rb
I can able to create and set proxy for firefox profile but not on chrome.
I don't know which options i have to use for chrome to set proxy.
Am using the following code:-
For firefox:-
require 'selenium/webdriver'
require 'browsermob/proxy'
server = BrowserMob::Proxy::Server.new("/path/to/downloads/browsermob-proxy/bin/browsermob-proxy") #=> #<BrowserMob::Proxy::Server:0x000001022c6ea8 ...>
server.start
proxy = server.create_proxy #=> #<BrowserMob::Proxy::Client:0x0000010224bdc0 ...>
profile = Selenium::WebDriver::Firefox::Profile.new #=> #<Selenium::WebDriver::Firefox::Profile:0x000001022bf748 ...>
profile.proxy = proxy.selenium_proxy
driver = Selenium::WebDriver.for :firefox, :profile => profile
proxy.new_har "google"
driver.get "http://google.com"
har = proxy.har #=> #<HAR::Archive:0x-27066c42d7e75fa6>
har.entries.first.request.url #=> "http://google.com"
har.save_to "/tmp/google.har"
proxy.close
driver.quit
For chrome:-
require 'selenium/webdriver'
require 'browsermob/proxy'
server = BrowserMob::Proxy::Server.new("/path/to/downloads/browsermob-proxy/bin/browsermob-proxy") #=> #<BrowserMob::Proxy::Server:0x000001022c6ea8 ...>
server.start
proxy = server.create_proxy #=> #<BrowserMob::Proxy::Client:0x0000010224bdc0 ...>
profile = Selenium::WebDriver::Chrome::Profile.new #=>
profile.proxy = proxy.selenium_proxy
driver = Selenium::WebDriver.for :chrome, :prefs => profile
proxy.new_har "google"
driver.get "http://google.com"
har = proxy.har #=> #<HAR::Archive:0x-27066c42d7e75fa6>
har.entries.first.request.url #=> "http://google.com"
har.save_to "/tmp/google.har"
proxy.close
driver.quit
In chrome, errors throws on the following line
profile.proxy = proxy.selenium_proxy
Error:- NoMethodError: undefined method `proxy=' for #
How to set proxy on chrome driver profile ?

Hey not sure if resolve this issue already, I faced same problem but finally got that work by using this code:
require 'selenium/webdriver'
require 'browsermob/proxy'
server = BrowserMob::Proxy::Server.new("/path/to/downloads/browsermob-proxy/bin/browsermob-proxy") #=> #<BrowserMob::Proxy::Server:0x000001022c6ea8 ...>
server.start
proxy = Selenium::WebDriver::Proxy.new(:http => #proxy.selenium_proxy.http)
caps = Selenium::WebDriver::Remote::Capabilities.chrome(:proxy => proxy)
driver = Selenium::WebDriver.for(:chrome, :desired_capabilities => caps)
Then you can save the har file using har= proxy.har
By using this basically you avoiding to chromedriver to point to wrong port and localhost. Also this approach works also on IEdriver just change the caps to
Selenium::WebDriver::Remote::Capabilities.internetexplorer(:proxy => proxy)
Hope this approach helps :)

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

Should not bypass certificate error in firefox

I'm using watir-webdriver and ruby. I need to go to the 'Untrusted connection' page in firefox. But watir handles the certificate validation by default. Is there anyway I can change the default settings?
require 'watir-webdriver'
browser= Watir::Browser.new :firefox
browser.goto "https://url"
Try this:
profile = Selenium::WebDriver::Firefox::Profile.new
profile.assume_untrusted_certificate_issuer = true
b = Watir::Browser.new WEB_DRIVER, :profile => profile

Ruby Capybara and Firefox: Download file without showing dialog

I am trying to get a Capybara session to automatically save downloaded files in Firefox, without displaying the open/save dialog.
This is my test code:
require 'capybara/dsl'
require 'selenium-webdriver'
cb = Capybara
cb.register_driver :my_firefox_driver do |app|
profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.dir'] = "~/Downloads"
profile['browser.download.folderList'] = 2
profile['browser.helperApps.alwaysAsk.force'] = false
profile['browser.download.manager.showWhenStarting'] = false
profile['browser.helperApps.neverAsk.saveToDisk'] = "text/csv"
profile['csvjs.disabled'] = true
Capybara::Selenium::Driver.new(app, :browser => :firefox, :profile => profile)
end
cb.current_driver = :my_firefox_driver
# just to check that we are reaching the correct URL
cb.visit "https://www.google.com/finance/historical?q=NYSE:IBM&startdate=Jan+1%2C+1980&enddate=Sep+18%2C+2014&num=30"
sleep(3)
# attempt a CSV download
cb.visit "https://www.google.com/finance/historical?q=NYSE:IBM&startdate=Jan+1%2C+1980&enddate=Sep+18%2C+2014&output=csv"
sleep(15)
Firefox version is 30. I have tried various profile settings as well as several MIME types, to no avail.
Thanks in advance for any suggestions or solutions.

browsermob-proxy get .har file and permission denied

I'm trying to get work with your gem and in windows 7 I did the following:
gem install browsermob-proxy
all went with success
but then I try to launch code:
require 'selenium/webdriver'
require 'browsermob/proxy'
server = BrowserMob::Proxy::Server.new('C:/Ruby193/lib/ruby/gems/1.9.1/gems/browsermob-proxy-0.1.3/lib/browsermob-proxy.rb') #=> #<BrowserMob::Proxy::Server:0x000001022c6ea8 ...>
server.start
proxy = server.create_proxy #=> #<BrowserMob::Proxy::Client:0x0000010224bdc0 ...>
profile = Selenium::WebDriver::Firefox::Profile.new #=> #<Selenium::WebDriver::Firefox::Profile:0x000001022bf748 ...>
profile.proxy = proxy.selenium_proxy
driver = Selenium::WebDriver.for :firefox, :profile => profile
proxy.new_har "google"
driver.get "http://google.com"
har = proxy.har #=> #<HAR::Archive:0x-27066c42d7e75fa6>
har.entries.first.request.url #=> "http://google.com"
har.save_to "/tmp/google.har"
proxy.close
driver.quit
and get error:
C:/Ruby193/lib/ruby/gems/1.9.1/gems/browsermob-proxy-0.1.3/lib/browsermob/proxy/server.rb:16:in ´initialize' : Permission denied - not executable: C:/Ruby193/lib/ruby/gems/1.9.1/gems/browsermob-proxy-0.1.3/lib/browsermob-proxy.rb (Errno::EACCES)
from gethar.rb:in 'new'
from gethar.rb:in '<main>'
I thought that there are no permissions in windows like in linux?
So any help please
The error message is misleading. This isn't a permissions issue; you need to point to the browsermob bat file, which is separate from the gem.
You can download it here: http://bmp.lightbody.net/
Then point to the file location, mine looks like this:
server = BrowserMob::Proxy::Server.new('C:/browsermob-proxy/bin/browsermob-proxy.bat')
That should fix your problem.

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

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)

Resources