How to open tor browser using watir? - ruby

With my ruby code I want to open Tor Browser instead of Firefox,for this I tried this code
path='C:\Tor Browser\App\tor.exe'
Selenium::WebDriver::Firefox.path = path
driver = Selenium::WebDriver.for :firefox
ie = Watir::Browser.new :firefox, :driver => driver
I got this error
c:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/firefox/launcher.rb:79:in `connect_until_stable': unable to obtain stable firefox connection in 60 seconds (127.0.0.1:7055) (Selenium::WebDriver::Error::WebDriverError)
from c:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/firefox/launcher.rb:37:in `block in launch'
from c:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/firefox/socket_lock.rb:20:in `locked'
from c:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/firefox/launcher.rb:32:in `launch'
from c:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/firefox/bridge.rb:24:in `initialize'
from c:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/common/driver.rb:31:in `new'
from c:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/common/driver.rb:31:in `for'
from c:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver.rb:67:in `for'
from C:/new_trademap/newTrademapTest.rb:28:in `<main>'
What should I have to do ? or there is any other way to do so?
links link1 google group link

The answer:
First, open the Tor browser (it needs to be running already). Then, in your code, make sure to simply start with:
Selenium::WebDriver::Firefox::Binary.path=/Applications/TorBrowser.app/Contents/MacOS/TorBrowser.app/Contents/MacOS/firefox
browser = Watir::Browser.new :firefox
*In my case: /home/tor/tor-browser_en-US/start-tor-browser
Nothing else is required - don't bother messing with profile

Your path C:\Tor Browser\App\tor.exe is not a path to firefox, it is a path to the Tor executable. What you will probably need to do is to modify your start-tor-browser script to run that tor.exe but not start firefox and then initialize Tor's Firefox separately.
Another option would be to modify your path of C:\Tor Browser\App\tor.exe to the path for the TorBrowser's Firefox binary instead, now that the TorBrowser bundles Tor and Firefox into a single executable.

Related

Watir - Error when I try to take screenshot of div

I use Watir and Watir-element-screenshot to get screenshot of elements.
Browser: Chrome and Firefox (latest versions)
My code:
require 'watir'
require 'watir/extensions/element/screenshot'
browser = Watir::Browser.new :firefox
browser.goto 'google.com.ua'
browser.text_field(id: "lst-ib").set "ruby\n"
browser.window.maximize
browser.element(id: "resultStats").screenshot("2.png")
browser.close
And take this error:
C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/chunky_png-1.3.8/lib/chunky_png/color.rb:968:in `scanline_bytesize': undefined method `>>' for 2439.0:Float (NoMethodError)
Did you mean? >
from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/chunky_png-1.3.8/lib/chunky_png/canvas/png_encoding.rb:207:in `encode_png_image_pass_to_stream'
from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/chunky_png-1.3.8/lib/chunky_png/canvas/png_encoding.rb:174:in `encode_png_image_without_interlacing'
from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/chunky_png-1.3.8/lib/chunky_png/canvas/png_encoding.rb:161:in `encode_png_pixelstream'
from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/chunky_png-1.3.8/lib/chunky_png/canvas/png_encoding.rb:85:in `to_datastream'
from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/chunky_png-1.3.8/lib/chunky_png/image.rb:62:in `to_datastream'
from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/chunky_png-1.3.8/lib/chunky_png/canvas/png_encoding.rb:35:in `write'
from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/chunky_png-1.3.8/lib/chunky_png/canvas/png_encoding.rb:43:in `block in save'
from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/chunky_png-1.3.8/lib/chunky_png/canvas/png_encoding.rb:43:in `open'
from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/chunky_png-1.3.8/lib/chunky_png/canvas/png_encoding.rb:43:in `save'
from C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/watir-extensions-element-screenshot-0.0.3/lib/watir/extensions/element/screenshot.rb:12:in `screenshot'
from 2.rb:11:in `<main>'
What the reason of this message and how can i repair it?
You can use this code to take screen shot and save in specific location.
browser.screenshot.save("path to save image/2.png")

Error: disable image loading on watir with firefox

i've got an error by changing the profile for the watir-webdriver. I use the following code to disable loading images in firefox:
profile = Selenium::WebDriver::Firefox::Profile.from_name "default"
profile['permissions.default.image'] = 2
browser = Watir::Browser.new :firefox, :profile => profile
This error message occur:
/var/lib/gems/2.3.0/gems/selenium-webdriver-3.0.0/lib/selenium/webdriver/remote/w3c_bridge.rb:80:in `initialize': unknown option: {:profile=>#<Selenium::WebDriver::Firefox::Profile:0x00000000e90700 #model="/home/amvisor/.mozilla/firefox/9ud9suhs.default", #native_events=false, #secure_ssl=false, #untrusted_issuer=true, #load_no_focus_lib=false, #additional_prefs={"permissions.default.image"=>2}, #extensions={}>} (ArgumentError)
from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.0.0/lib/selenium/webdriver/firefox/w3c_bridge.rb:34:in `initialize'
from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.0.0/lib/selenium/webdriver/common/driver.rb:49:in `new'
from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.0.0/lib/selenium/webdriver/common/driver.rb:49:in `for'
from /var/lib/gems/2.3.0/gems/selenium-webdriver-3.0.0/lib/selenium/webdriver.rb:82:in `for'
from /var/lib/gems/2.3.0/gems/watir-webdriver-0.9.3/lib/watir-webdriver/browser.rb:46:in `initialize'
from xxx.rb:38:in `new'
from xxx.rb:38:in `<main>'
What means:
initialize: unknown option: {:Profile
Can anybody help?
Thanks!
This works to use an existing Firefox profile with Firefox 48+:
profile = Selenium::WebDriver::Zipper.zip('/path/to/profile/xxx.default')
caps = Remote::Capabilities.firefox(firefox_options: {profile: profile})
browser = Watir::Browser.new :firefox, desired_capabilities: caps
Ruby bindings do not yet fully support all of the features of Firefox profiles from the old Firefox Driver in the new geckodriver. If you need these additional features you can use Firefox <48 (I recommend installing Extended Support Release) and pass in marionette: false in capabilities.

How to specify the location of the chromedriver binary

Earlier I had put the Chrome binary, "chromedriver.exe", in the "C:/Windows" directory and Watir was picking it from there. Now I have to move my project to another machine so I can't hardcode the executable path. I also want the binary to be kept with our code on Git rather than making each test engineer manually update the binary as newer versions are released.
Now I have placed Chrome binary at a absolute path, but it is not being found. Here is what I tried (hooks.rb):
Before do
puts "inside hooks in before"
profile = Selenium::WebDriver::Chrome::Profile.new
profile['download.prompt_for_download'] = false
profile['download.default_directory'] = File.join(File.absolute_path('../..', File.dirname(__FILE__)),"browsers/chromedriver.exe")
#browser = Watir::Browser.new :chrome, :profile => profile
end
The output is:
inside hooks in before
Selenium::WebDriver::Error::WebDriverError: Unable to find the chromedriver executable. Please download the server from http://chromedriver.storage.googleapis.com/index.html and place it somewhere on your PATH. More info at http://code.google.com/p/selenium/wiki/ChromeDriver.
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver/chrome/service.rb:21:in `executable_path'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver/chrome/service.rb:34:in `default_service'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver/chrome/bridge.rb:14:in `initialize'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver/common/driver.rb:37:in `new'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver/common/driver.rb:37:in `for'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver.rb:67:in `for'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.11/lib/watir-webdriver/browser.rb:46:in `initialize'
C:/Users/Admin/watircukepractice/test_puppies/features/support/hooks.rb:11:in `new'
C:/Users/Admin/watircukepractice/test_puppies/features/support/hooks.rb:11:in `Before'
I am on Windows 7, Using Ruby version 1.9.3p551 and I am referring to tutorial http://watirwebdriver.com/chrome/.
How do I tell Watir (and Selenium-WebDriver) the location of the chromedriver.exe?
Solution 1 - Selenium::WebDriver::Chrome.driver_path=
There is a Selenium::WebDriver::Chrome.driver_path= method that allows specifying of the chromedriver binary:
require 'watir'
# Specify the driver path
chromedriver_path = File.join(File.absolute_path('../..', File.dirname(__FILE__)),"browsers","chromedriver.exe")
Selenium::WebDriver::Chrome.driver_path = chromedriver_path
# Start the browser as normal
b = Watir::Browser.new :chrome
b.goto 'www.google.com'
b.close
Solution 2 - Specify :driver_path during browser initialization
As an alternative, you can also specify the driver path when initializing the browser. This is a bit nicer in that you do not need to have Selenium code, but would be repetitive if you initialize the browser in different places.
# Determine the driver path
chromedriver_path = File.join(File.absolute_path('../..', File.dirname(__FILE__)),"browsers","chromedriver.exe")
# Initialize the browser with the driver path
browser = Watir::Browser.new :chrome, driver_path: chromedriver_path
Solution 3 - Update ENV['PATH']
When I had originally answered this question, for whatever reason, I had not been able to get the above solution to work. Setting the value did not appear to be used when Selenium-WebDriver started the driver in. While the first solution is the recommended approach, this is an alternative.
Another option is to programmatically add the desired directory to the path, which is stored in the ENV['PATH']. You can see in the Selenium::WebDriver::Platform that the binary is located by checking if the executable exists in any of the folders in the path (from version 2.44.0):
def find_binary(*binary_names)
paths = ENV['PATH'].split(File::PATH_SEPARATOR)
binary_names.map! { |n| "#{n}.exe" } if windows?
binary_names.each do |binary_name|
paths.each do |path|
exe = File.join(path, binary_name)
return exe if File.executable?(exe)
end
end
nil
end
To specify the folder that includes the binary, you simply need to change the ENV['PATH'] (to append the directory):
require 'watir'
# Determine the directory containing chromedriver.exe
chromedriver_directory = File.join(File.absolute_path('../..', File.dirname(__FILE__)),"browsers")
# Add that directory to the path
ENV['PATH'] = "#{ENV['PATH']}#{File::PATH_SEPARATOR}#{chromedriver_directory}"
# Start the browser as normal
b = Watir::Browser.new :chrome
b.goto 'www.google.com'
b.close
In Selenium webdriver 3.x configs, change:
caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {"binary" => <path to chrome (example: chrome portable)>})
Capybara::Selenium::Driver.new(app, :browser => :chrome, :driver_path => <path to chrome driver>, :desired_capabilities => caps)
driver_path: Define path to chromedriver chromedriver page
binary: Define path to binary chrome app chromepage. You can use a chrome portable to use differents versions of chrome.
Update your Selenium driver_path directly.
Just call this before launching your new Chrome browser window:
Selenium::WebDriver::Chrome::Service.driver_path = Rails.root.join( "lib", "chromedriver" ).to_s
Of course, change the path to where your chromedriver is.
NOTE: driver_path needs to be a String, so don't pass in a File or Path object.
This works for me:
options = Selenium::WebDriver::Chrome::Options.new
Selenium::WebDriver::Chrome::Service.driver_path = '/app/.chromedriver/bin/chromedriver'
chrome_bin_path = ENV.fetch('GOOGLE_CHROME_SHIM', nil)
if chrome_bin_path
options.binary = chrome_bin_path if chrome_bin_path
options.add_argument '--no-sandbox'
options.add_argument '--window-size=1200x600'
options.add_argument '--headless'
options.add_argument '--disable-gpu'
end
browser = Watir::Browser.new(:chrome, options:)

Why does Selenium Web Driver say Service Unavailable?

When I run the code below:
require "selenium-webdriver"
require 'rubygems'
require 'watir-webdriver'
b = Watir::Browser.new :phantomjs
b.goto 'http://www.google.com'
puts b.title
b.close
the following error is displayed:
/home/jotsarup/.gem/gems/selenium-webdriver-2.41.0/lib/selenium/webdriver/remote/http/common.rb:66:in `create_response': unexpected response, code=503, content-type="text/html" (Selenium::WebDriver::Error::WebDriverError)
<HTML><TITLE>503 Service Unavailable</TITLE>
<H1>503 Service Unavailable</H1>
Failed to connect to server <B>127.0.0.1</B></HTML>
from /home/jotsarup/.gem/gems/selenium-webdriver-2.41.0/lib/selenium/webdriver/remote/http/default.rb:66:in `request'
from /home/jotsarup/.gem/gems/selenium-webdriver-2.41.0/lib/selenium/webdriver/remote/http/common.rb:40:in `call'
from /home/jotsarup/.gem/gems/selenium-webdriver-2.41.0/lib/selenium/webdriver/remote/bridge.rb:634:in `raw_execute'
from /home/jotsarup/.gem/gems/selenium-webdriver-2.41.0/lib/selenium/webdriver/remote/bridge.rb:99:in `create_session'
from /home/jotsarup/.gem/gems/selenium-webdriver-2.41.0/lib/selenium/webdriver/remote/bridge.rb:68:in `initialize'
from /home/jotsarup/.gem/gems/selenium-webdriver-2.41.0/lib/selenium/webdriver/phantomjs/bridge.rb:32:in `initialize'
from /home/jotsarup/.gem/gems/selenium-webdriver-2.41.0/lib/selenium/webdriver/common/driver.rb:45:in `new'
from /home/jotsarup/.gem/gems/selenium-webdriver-2.41.0/lib/selenium/webdriver/common/driver.rb:45:in `for'
from /home/jotsarup/.gem/gems/selenium-webdriver-2.41.0/lib/selenium/webdriver.rb:67:in `for'
from /home/jotsarup/.gem/gems/watir-webdriver-0.6.8/lib/watir-webdriver/browser.rb:46:in `initialize'
from test_phantom.rb:7:in `new'
from test_phantom.rb:7:in `<main>'
phantomjs is not connected. I also tried Firefox and the results are the same.
It looks like you are failing to reach outside of your local machine based on "Failed to connect to server 127.0.0.1" 127.0.0.1 is your loopback address (for your machine) and I have seen this issue arise in the past when there is a firewall up. If you are in a company that requires traffic to be routed through the firewall I would recommend seeing if they see any traffic trying to make it out from your machine. If you're not in a company requiring a firewall then I would recommend dropping the firewall/proxy for testing.
Looks like you are behind the PROXY. Add the following snippet before starting the server:
ENV['HTTP_PROXY'] = ENV['http_proxy'] = nil
b = Watir::Browser.new :phantomjs

Selenium/Ruby unknown driver, firefox

I wrote a selenium test that I have working in chrome, I'm trying to get selenium to run the test in every browser. When I try and access firefox I get an error though.
Code up to the error...
require "rubygems"
require "selenium-webdriver"
driver = Selenium::WebDriver.for :Firefox
I get the following error on the line where driver is declared...
/Users/username/.rvm/gems/ruby-2.1.0/gems/selenium-webdriver-2.39.0/lib/selenium/webdriver/common/driver.rb:49:in 'for': unknown driver: :Firefox (ArgumentError)
from /Users/samuelowens/.rvm/gems/ruby-2.1.0/gems/selenium-webdriver-2.39.0/lib/selenium/webdriver.rb:67:in 'for'
from test.rb:47:in `<main>'
Any idea why that might be and how to fix it? Is there an easy way to write a selenium test and check it in multiple browsers? Thanks.
Not capital case Firefox but firefox
driver = Selenium::WebDriver.for :firefox

Resources