Using default Firefox profile in Watir Webdriver but switching off JavaScript - ruby

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

Related

changing chrome prefs in `ruby` after starting the browser

I have created a browser using
`#prefs = {download: { prompt_for_download: false, default_directory: "#{Dir.home}/new2" } }
Watir::Browser.new :chrome, prefs: #prefs`
and when my script runs, it initiates another window while clicking Print option on Chrome
I want to change the prefs of the opened window to #prefs once it opens
Is there any way of writing it in ruby, watir, page-object
I have no idea how to change the prefs of newly opened window
Those things can only be set at the time of browser instantiation. They are the parameters used by the browser driver when creating the session and can not be changed on an active season.

WebDriver Ruby Custom FirefoxProfile

I have a Firefox profile that I'd like to launch for testing. It's currently the only profile loaded up on my test machine's copy of Firefox. When I load WebDriver Remote it's not loading my profile. First I tried this:
capabilities = Selenium::WebDriver::Remote::Capabilities.firefox(:firefox_profile => "myprofile")
driver = Selenium::WebDriver.for(:remote, :desired_capabilities => capabilities)
That didn't work and opened up a browser with a blank profile. Then I remember that in PHP WebDriver there was a requirement to copy the profile and Base64 encode it. So I did that:
profile = File.read '/home/me/firefoxprofile.zip.b64'
capabilities = Selenium::WebDriver::Remote::Capabilities.firefox(:firefox_profile => profile)
driver = Selenium::WebDriver.for(:remote, :desired_capabilities => capabilities)
But I'm still stuck with a blank profile again. :(
I set a lot of custom preferences and extensions in my profile and would like to load it for each test.
UPDATE:
I also tried adding -FirefoxProfilePath with the webdriver.jar and that also failed to load my profile.
How can I load my custom Firefox profile in WebDriver Remote for testing?

Watir with Firefox 48 - How do I use a custom profile?

I can't seem to find anything about this, and I'm starting to get upset, because this code used to work:
browser = Watir::Browser.new :firefox, :profile => "Anna"
However, now that code, while it will launch firefox successfully if I use :marionette => true, if I use :profile => "Anna" or even if I do this:
caps = Selenium::WebDriver::Remote::Capabilities.firefox({:profile => profile,:marionette=> true})
browser = Watir::Browser.new :firefox, :desired_capabilities => caps
Watir will still launch firefox and work properly, but it is still using its own profile. I have custom x509 certificates I need to use in that profile (and the site is throwing "This site is not secure" screens at me because it's still in development), so how can I use it? I know there's a way to do it, but I've been Googling for an hour now and have yet to find it. Removing :marionette => true will let the profile load properly, but since Firefox 48 doesn't support the old Firefox driver, I can't make it work.

How to modify the Firefox profile used by Watir-Webdriver

I'm working on web-application automation using Ruby 1.9.3p484 and Watir-webdriver (0.8.0) with Firefox 41.0 for Ubuntu.
I want the browser to not load any images. To do that, I try to change the 'permissions.default.image' firefox parameter to 2.
I have tried the following code:
profile = Selenium::WebDriver::Firefox::Profile.new
profile['permissions.default.image'] = 2
browser = Watir::Browser.new(:firefox, :profile => profile)
browser.goto url
But, the browser keep loading the images. In the about:config page, the 'permissions.default.image' is still set to 1.
Any ideas on what could be wrong?
I don't know why, but if I use from_name method in Selenium class init, then you code is working perfectly:
profile = Selenium::WebDriver::Firefox::Profile.from_name "default"

Watir with Tor browser

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.

Resources