Watir: How to set `chrome://settings/content/pdfDocuments` - ruby

I am trying to set the options for Watir with chrome driver, but can no figure out how to set:
chrome://settings/content/pdfDocuments to true or false.
What would the pref variable be to set this?
Watir::Browser.new :chrome, options: {prefs: prefs}

Are you looking for this? https://stackoverflow.com/a/42927095/4072371
prefs = {"plugins.always_open_pdf_externally" => true}
Watir::Browser.new :chrome, options: {prefs: prefs}
Make sure you are using a String for the prefs key

Related

Selenium Webdriver - set preferred browser language DE

I have a problem setting the preferred (accepted language) within headless Chrome using Selenium Webdriver and Ruby. I use the following WebDriver settings:
Selenium::WebDriver::Chrome.driver_path = #config[<path to the Chrome Driver>]
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
options.add_argument('--disable-translate')
options.add_argument("--lang=de")
The driver is then initialized with:
#selenium_driver = Selenium::WebDriver.for :chrome, options: options
Everything works fine but at some pages Chrome returns English content even when I navigate to the German page URL (e.g. page.de). In these cases the Chrome driver returns the English content due to an internal forwarding to page.de/en. I do not specify the en path in my queried URL.
I have tried to set the language using the Webdriver preference:
options.add_preference('accept_languages', 'de')
instead of the add_argument but it doesn't change anything of the behavior.
Does anyone have an idea how to force a headless Chrome controlled by Selenium Webdriver within Ruby to request page content in a defined language or - not optimal but it might help as a workaround - to stop the forwarding?
Any help greatly appreciated
Best
Krid
I found a solution that works for me. As in many cases the problem was sitting in front of the screen and simply doesn't work precisely enough ;-)
Instead of using
options.add_argument("--lang=de")
you have to use
options.add_argument("--lang=de-DE")
When I use an IETF language tag the code I initially posted works as expected.
I'am using this in my test_helper.rb Works fine for me.
Capybara.register_driver :selenium do |app|
Chromedriver.set_version "2.36"
desired_capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
'chromeOptions' => {
'prefs' => {
'intl.accept_languages' => 'en-US'
},
args: ['disable-gpu', 'headless']
}
)
Capybara::Selenium::Driver.new(app, { browser: :chrome, desired_capabilities: desired_capabilities })
end
Capybara.javascript_driver = :chrome
Capybara.default_driver = :selenium
This prefs hash inside an options hash did the trick for me. It's at the end of the driven_by :selenium line.
(Inside test/application_syste_test_case.rb)
# frozen_string_literal: true
require 'test_helper'
require 'capybara/rails'
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :selenium, using: :chrome, screen_size: [1400, 1400], options: { prefs: { 'intl.accept_languages' => 'de,de-DE;q=0.9,en;q=0.1' } }
# ...
2021-06-14 UPDATE:
The previous example produces this deprecation warning:
WARN Selenium [DEPRECATION] :prefs is deprecated. Use Selenium::WebDriver::Chrome::Options#add_preference instead.
IMO, the solution below is uglier, but I'm posting it for when it's fully deprecated and the original stops working.
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by(:selenium,
using: :chrome,
screen_size: [1400, 1400],
options: {
options: Selenium::WebDriver::Chrome::Options.new(
prefs: { 'intl.accept_languages' => 'de,de-DE;q=0.9,en;q=0.1' }
)
},
)
You should be able to solve your problem by adding an experimental option:
options.add_option('prefs', {'intl.accept_languages': 'en,en_US'})
I'm sure it works with Python, but I've not tried with Ruby: this approach is the correct one, not sure about the implementation.
You can find in this repository the code which handles your problem in Python code, and in this Q&A how to implement experimental_options in Ruby
For me works:
options = Selenium::WebDriver::Firefox::Options.new
options.add_preference("intl.accept_languages", 'de-DE')
Capybara::Selenium::Driver.new(app, browser: :firefox, options: options)

What is replica of this java code "setExperimentalOption" in ruby to open chrome browser with 'useAutomationExtension' as false

The below java code works fine in my machine to disable automation extension.
How I can write a replica of this code in ruby
Java
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("useAutomationExtension", false);
In Watir Ruby
this code doesn't work
Watir::Browser.new(:chrome, :switches => %w[--disable-popup-blocking --disable-extensions --disable-infobars] )
Maybe try passing it as a raw option:
b = Watir::Browser.new :chrome, options: {options: {'useAutomationExtension' => false}}
You should be able to pass them along with args: See the update of Watir 6.6: http://watir.com/watir-6-6/
In short:
Watir::Browser.new :chrome, options: {args: %w[--disable-popup-blocking --disable-extensions --disable-infobars]}
This is how I like to start the browser keeping all options variable.
browser_name = ENV['BROWSER'] || 'chrome'
settings = {}
if ENV['DEVICE'] == 'remote'
settings[:url] = 'http://selenium__standalone-chrome:4444/wd/hub/'
end
if browser_name == 'chrome'
args = ['--ignore-certificate-errors', '--disable-popup-blocking', '--disable-translate', '--use-fake-device-for-media-stream', '--use-fake-ui-for-media-stream']
settings[:options] = {args: args}
end
Watir::Browser.new browser_name.to_sym, settings
I found the root cause which is not allowing the script to run.
I navigated to below path in "regedit" and deleted the "ExtensionInstallBlacklist" folder but this is a temporary solution the registry will be auto created in some time.
Path:
“HKEY_CURRENT_USER\Software\Policies\Google\Chrome\ExtensionInstallBlacklist”

Ruby & Selenium - how to pass arguments to browser?

My search has only turned up answers for Java - this and this
The selenium gem doesn't include addCommandLineOptions as far as I can tell, but it does have WebDriver::Remote::Capabilities.
How do I use it to add arguments? I know you pass it as desired_capabilities: to the driver constructor, but in what format?
Unfortunately the documentation has been especially useless
You can set --start-maximizedby following for Chrome. See this post for more details.
Capybara.register_driver :chrome_maximize do |app|
caps = Selenium::WebDriver::Remote::Capabilities.chrome(
'chromeOptions' => {
"args" => [ "--start-maximized", "--otherthings" ]
}
)
$driver = Capybara::Selenium::Driver.new(app, {:browser => :chrome, :desired_capabilities => caps})
end
I finally figured out!!! Here is a working example for gem "selenium-webdriver". Should work too for Capybara as well.
First line is if you want to run a custom binary. In case of --headless command line argument, support starts from firefox version 55. Don't forget to make firefox-nightly available to $PATH Env Var.
Selenium::WebDriver::Firefox.path = "/home/user/bin/firefox-nightly"
caps = Selenium::WebDriver::Remote::Capabilities.firefox(
"moz:firefoxOptions" => {
args: ["--headless"] # and other arguments...
}
)
driver = Selenium::WebDriver.for :firefox, desired_capabilities: caps
# do stuff here ....
driver.quit

unable to locate element, using {:id=>"", :tag_name=>"select"}

I want to select value from drop down box using Ruby with watir-webdriver. Here is the command
browser.select_list(:id, "ctl00_SampleContent_ComboBox1_ComboBox1_OptionList").select("Whiskey")
and i got an error
unable to locate element, using {:id=>"ctl00_SampleContent_ComboBox1_ComboBox1_OptionList", :tag_name=>"select"}
Any ideas what is wrong?
Here is full code:
# 1.Open http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/Default.aspx
#browser = Watir::Browser.new
#browser = Watir::Browser.new :ie
profile = Selenium::WebDriver::Firefox::Profile.from_name 'WatirWebDriver'
#profile.add_extension 'autoauth-2.1-fx+fn.xpi'
browser = Watir::Browser.new :firefox, :profile => profile
browser.goto 'http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/Default.aspx'
#2.Click ComboBox link on the left pane of the page
browser.a(:id, 'ctl00_SamplesLinks_ctl15_SamplesLink').click
#3.Verify that http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/ComboBox/ComboBox.aspx URL opened
if browser.url.eql? "http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/ComboBox/ComboBox.aspx"
puts "Error loading page \"http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/ComboBox/ComboBox.aspx URL opened\""
return false
end
#4.Select “Whiskey” in the combo-box
#browser.select_list(:id, 'ctl00_SampleContent_ComboBox1_ComboBox1_OptionList').select_value('Whiskey')
puts "!!!"
browser.select_list(:id, "ctl00_SampleContent_ComboBox1_ComboBox1_OptionList").when_present.select("Whiskey")
This does the job:
require "watir-webdriver"
browser = Watir::Browser.new
browser.goto "http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/ComboBox/ComboBox.aspx"
browser.button(id: "ctl00_SampleContent_ComboBox1_ComboBox1_Button").click
browser.ul(:id, "ctl00_SampleContent_ComboBox1_ComboBox1_OptionList").li(text: "Whiskey").click

Firefox 4 with watir webdriver: Need help using helperApps.neverAsk to save CSV without prompting

I learned how to use Firefox 4 with watir and webdriver (on Win7 x64), setting profile items. Example:
profile = Selenium::WebDriver::Firefox::Profile.new
profile["browser.download.useDownloadDir"] = true
profile["browser.download.dir"] = 'D:\\FirefoxDownloads'
profile["browser.helperApps.neverAsk.saveToDisk"] = "application/csv"
driver = Selenium::WebDriver.for :firefox, :profile => profile
browser = Watir::Browser.new(driver)
What I try to do with the example below, is setting CSV files to be always downloaded to a specific directory, never opened.
The code above succeeds in setting all the files automatically downloaded to the specified directory, but setting browser.helperApps.neverAsk.saveToDisk has no effect: I still get the open/save question.
After the script runs, the Firefox window is still open, and I enter the URL about:config.
I can see that browser.helperApps.neverAsk.saveToDisk was correctly set to application.csv , but in firefox/options/options/applications I don't see the entry for CSV files.
It seems that the menu setting, that is really effective, is not really bound with the about:config setting.
What am I doing wrong?
I've done some testing of this for you, unfortunately there doesn't seem to be a standard content-type for CSV files. You can try passing a comma separated list of content-types, hopefully one of those work for you. For me it was application/octet-stream that did the trick...
require 'watir-webdriver'
require 'selenium-webdriver'
profile = Selenium::WebDriver::Firefox::Profile.new
profile["browser.download.useDownloadDir"] = true
profile["browser.download.dir"] = '/tmp'
profile["browser.helperApps.neverAsk.saveToDisk"] = "text/plain, application/vnd.ms-excel, text/csv, text/comma-separated-values, application/octet-stream"
driver = Selenium::WebDriver.for :firefox, :profile => profile
browser = Watir::Browser.new(driver)
browser.goto "http://altentee.com/test/test.csv"
In Firefox 6+, I couldn't get this to work without specifically setting the 'browser.download.folderList' value:
profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.folderList'] = 2 #custom location
profile['browser.download.dir'] = download_directory
profile['browser.helperApps.neverAsk.saveToDisk'] = "text/csv, application/csv"
b = Watir::Browser.new :firefox, :profile => profile
See: http://watirwebdriver.com/browser-downloads/

Resources