Watir with Tor browser - ruby

I've been playing with Watir and Tor browser and I can't get them to work.
The correct browser opens, however, I'm unable to open a website.
My code:
Selenium::WebDriver::Firefox.path = '\path\Tor Browser\Start Tor Browser.exe'
driver = Selenium::WebDriver.for :firefox
browser = Watir::Browser.new :firefox, :driver => driver
This results in a message box popping up, which says:
Your Firefox profile cannot be loaded. It may be missing or inaccessible.
The correct browser also opens up, however, the website doesn't.
I've also tried:
Selenium::WebDriver::Firefox.path = '\path\Tor Browser\Browser\firefox.exe'
driver = Selenium::WebDriver.for :firefox
browser = Watir::Browser.new :firefox, :driver => driver
With this, the message box doesn't appear, but still, the website is not loaded.
I'm not sure, if the problem is in my code or in the browser.
Thank you very much for any help!

Start Tor then run this, assuming your socks port is 9050
require 'watir-webdriver'
profile = Selenium::WebDriver::Firefox::Profile.new
profile['network.proxy.socks'] = "127.0.0.1"
profile['network.proxy.socks_port'] = 9050
profile['network.proxy.type'] = 1
$browser = Watir::Browser.new :firefox, :profile => profile
$browser.goto "whatsmyip.org"

Firefox and Tor are not the same browser. Watir uses webdriver API to control the browser. You can't use Firefox webdriver API to control another browser.
In your second example, browser don't open the site, but you don't tell him to. You should add :
browser.goto('http://my.example.com')
And don't forget to put a browser.quit at the end of your code to close the browser.

Related

How to launch IE inPrivate browsing with Ruby and Watir

I need to launch the Watir IE browser inPrivate mode.
Chrome`s icognito mode can be launched fairly easily with:
#browser = Watir::Browser.new :chrome, switches: ['--incognito']
I tried this in IE, but it doesn`t work
#browser = Watir::Browser.new :ie, switches: ['-private']
I tried some of the indications here, but did not succeed
https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver
with
ie.forceCreateProcessApi with true value and ie.browserCommandLineSwitches with -private

chrome browser closes automatically after the program finishes in ruby using watir

I am using chrome 56, chrome driver 2.27(latest release) with selenium web driver 3.1.0. Referring to the issue(https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/1811) where chrome closes all the instances once the program finishes and it does not give me a chance to debug. I just want to know if this is fixed then why it is still happening ? or i am missing something ?
I am using the following code. Any help is appreciated.
require "uri"
require "net/http"
require 'watir-webdriver'
require 'selenium-webdriver'
#b = Watir::Browser.new :chrome
#b.goto 'http://www.google.com'
Firstly, watir-webdriver gem is deprecated. The updated code is in the watir gem. Also, you shouldn't need to require any of those other gems directly.
The chromedriver service is stopped when the ruby process exits. If you do not want the browsers that were started by chromedriver to close as well, you need to use the detach parameter. Currently this is done like so:
require 'watir'
caps = Selenium::WebDriver::Remote::Capabilities.chrome
caps[:chrome_options] = {detach: true}
#b = Watir::Browser.new :chrome, desired_capabilities: caps
Declare these
caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {'detach' => true})
browser = Watir::Browser.new :chrome, desired_capabilities: caps
On a side note! this might give a problem when you are running multiple scenario tests, chromedriver will actively refuse connection in case an other test initiates in the same chrome session. Ensure you have browser.close whenever required.

Automate Chrome Extensions With Selenium and Ruby

I am currently working on an automation project where I need to use Ruby/Selenium to discover specific http headers returned to the user after authentication to a web app. I am able to automate the web app just fine; however, when I try to use a Chrome extension the browser returns the following error:
The webpage at chrome-extension://[extension address] might be temporarily down or it may have moved permanently to a new web address.
After looking into this, it appears that the Selenium web driver is using a different Chrome profile than my regular Chrome profile. As such, I was wondering if someone knew if there is a way to to tell Selenium to use my regular Chrome profile with the extension loaded or build a new profile and install the extension during runtime.
So far, most of the answers I have found were centralized around Python and Java. Please let me know if I can provide more information.
To launch Chrome with the default profile on Windows:
require 'selenium-webdriver'
switches = ['user-data-dir='+ENV['LOCALAPPDATA']+'\\Google\\Chrome\\User Data']
driver = Selenium::WebDriver.for :chrome, :switches => switches
driver.navigate.to "https://www.google.co.uk"
Or to add an extension to the created profile:
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :chrome,
:desired_capabilities => Selenium::WebDriver::Remote::Capabilities.chrome({
'chromeOptions' => {
'extensions' => [
Base64.strict_encode64(File.open('C:\\App\\extension.crx', 'rb').read)
]
}
})
driver.navigate.to "https://www.google.co.uk"

How might I simulate a private browsing experience in Watir? (Selenium)

Watir is a Selenium-based black-box testing tool that can automate tasks on the browser.
I would like to be able to open up a Watir::Browser.new that is in private browsing mode.
Thanks
For Firefox (I am not sure about the other browsers), you can setup the profile to have private browsing enabled:
profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.privatebrowsing.dont_prompt_on_enter'] = true
profile['browser.privatebrowsing.autostart'] = true
b = Watir::Browser.new :firefox, :profile => profile
This was the solution from https://github.com/watir/watir-webdriver/issues/95.
It seems to work (at least the main menu says it is private browsing).
For Chrome
browser = Watir::Browser.new :chrome, switches: ['--incognito']

Using default Firefox profile in Watir Webdriver but switching off JavaScript

I would like to use my default profile in firefox when watir-webdriver launches a friefox browser, but then I want to switch off javascript in that browser this way:
browser = Watir::Browser.new :ff, :profile => "default"
browser.profile['javascript.enabled']=false
The second reference doesn't work, because the "profile" is not a member of the browser object. How can I access it? Or any other method to switch off javascript either in the default profile or in the running firefox window? I want to do this programmatically, because at the end of my Ruby script I would like to switch it on again.
I think you would do it like this:
profile = Selenium::WebDriver::Firefox::Profile.new
profile['javascript.enabled'] = false
browser = Watir::Browser.new :ff, :profile => profile

Resources