browsermob-proxy get .har file and permission denied - ruby

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.

Related

Set proxy for selenium chrome driver in 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 :)

Error while creating and instance of IE

Hi all while I try to create an instance of IE I am facing the following error
my system configurations
Windows 7,64 bit
NameError: uninitialized constant Watir::IE
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'watir'
=> true
irb(main):003:0> ie=Watir::IE.new
NameError: uninitialized constant Watir::IE
from (irb):3
from D:/Ruby_1.8.7/lib/ruby/site_ruby/1.8/rubygems/specification.rb:630
irb(main):004:0>
To launch a browser in watir, you shoul use :
b = Watir::Browser.new :ie
# or :ff or :chrome or :opera ...
Note that for Internet explorer, you should download IEDriverServer.exe and add it to your path.

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'

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)

Watir Web driver to download file

I'm writing Ruby script to automate Downloading files from a list of files using Watir web driver. Any pointers or approaches on Automating Popping up of Window and Directory Chooser to save the file into a location? Thanks.
change default Watir preferences for download location
for chrome
profile = Selenium::WebDriver::Chrome::Profile.new
download_dir = File.join(Rails.root, 'lib', 'assets')
profile['download.default_directory'] = download_dir
profile['download.prompt_for_download'] = false
#b = Watir::Browser.new :chrome, :profile => profile
for firefox
profile = Selenium::WebDriver::Firefox::Profile.new
download_dir = File.join(Rails.root, 'lib', 'assets')
profile['browser.download.dir'] = download_dir
profile['browser.helperApps.neverAsk.saveToDisk'] = "text/csv,application/pdf"
#b = Watir::Browser.new. :firefox, :profile => profile
note: to be be able to access the Rails.root/lib folder easily from within your rails app, you'll need to add this code or something like it to your config/application.rb file:
config.autoload_paths += Dir["#{config.root}/lib/**/"]
for more information: http://watir.com/guides/downloads/

Resources