Remote WebDriver ignore certificate errors for Chrome - ruby

How ignore sertificate with Remote WebDriver for Chrome? I try run this code:
#encoding: utf-8
require 'selenium-webdriver'
include Selenium
capabilities = WebDriver::Remote::Capabilities.chrome(:native_events => true)
driver = WebDriver.for(:remote,
:desired_capabilities => capabilities,
:url => "http://192.168.1.44:4444/wd/hub",
:switches => %w[--ignore-certificate-errors]
)
driver.navigate.to "https://trunk.plus1.oemtest.ru/"
puts driver.title
driver.close
And get an error message:
home/igor/.rvm/gems/ruby-1.9.2-p290#selenium/gems/selenium-webdriver-2.12.0/lib/selenium/webdriver/remote/bridge.rb:51:in `initialize': unknown option: {:switches=>["--ignore-certificate-errors"]} (ArgumentError)

The approach described above is not supported by latest chromedriver anymore. According to this doc chromeOptions should be used instead:
caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {"args" => [ "--ignore-certificate-errors" ]})
driver = Selenium::WebDriver.for :remote, url: 'http://localhost:4444/wd/hub', desired_capabilities: caps

This should do the trick:
caps = Selenium::WebDriver::Remote::Capabilities.chrome
caps['chrome.switches'] = %w[--ignore-certificate-errors]
driver = Selenium::WebDriver.for(:remote, :desired_capabilities => caps)

It seems like now the correct way of requesting insecure certs is setting accept_insecure_certs = true on the Selenium::WebDriver::Remote::Capabilities instance.

Related

Getting Net::ReadTimeout: visiting a website in Ruby Capybara Cucumber

I'm running automated tests using Ruby/Cucumber/Capybara/Chromedriver, but i get an error while running a simple test scenario. I tried to solve problem by updating version of Ruby, Capybara, Cucumber, Chromdriver and Chrome. I need your help. Here is the details:
Try to:
visit 'http://www.google.com'
Getting:
WARNING: The formatter Teamcity::Cucumber::Formatter is using the deprecated formatter API which will be removed in v4.0 of Cucumber.
2018-04-16 14:32:43 WARN Selenium [DEPRECATION] :args or :switches is deprecated. Use Selenium::WebDriver::Chrome::Options#add_argument instead.
Net::ReadTimeout: Net::ReadTimeout
./features/step_definitions/common_steps.rb:46:in `/^testing$/'
Chrome Version:65.0.3325.181
Chrome Driver: 2.37
cucumber: 3.1.0
capybara: 3.0.1
ruby: 2.2.6
When I removed "--disable-extensions" from env.rb file as you see below it works fine..
Capybara::Selenium::Driver.new(app, :browser => :chrome, :switches => %w[--disable-extensions --disable-web-security --start-maximized])
Capybara::Selenium::Driver.new(
app,browser: :chrome,
desired_capabilities: {
'chromeOptions' => {
'useAutomationExtension' => false,
'args' => ['--disable-web-security', '--start-maximized', '--disable-infobars']
}
}
)
client = Selenium::WebDriver::Remote::Http::Default.new
client.read_timeout = 120
Capybara.register_driver :headless_chrome do |app|
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
chromeOptions: {args: %w[headless disable-gpu disable-popup-blocking window-size=10_000,1080 log-level=3]}
)
Capybara::Selenium::Driver.new(app,
browser: :chrome,
desired_capabilities: capabilities,
http_client: client)
end

Chrome 62 and Flash

I have a Flash-based app that I need to test using Cucumber. As flash is not enabled by default I need to enable it before each test and whitelist the url I believe. If I pause the test in it's background phase I can manually set these options.
How can I automate this approach though, I have looked into adding options and preferences, but still cannot seem to get to work.
So this is my standard setup in an env.rb file
Capybara.register_driver :chrome do |app|
chrome_binary = '/Applications/Google Chrome.app'
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => { "binary" => chrome_binary + '/Contents/MacOS/Google Chrome' })
Capybara::Selenium::Driver.new(app, :browser => :chrome, :desired_capabilities => capabilities, :options => options)
end
Further reading has highlighted options such as
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('arg-here')
prefs = {"enable flash here ? "}
options.add_experimental_option("prefs", prefs)
The add_experimental_option throws undefined method add_experimental_option for #<Selenium::WebDriver::Chrome::Options:0x007fca30c10988>
Has anyone automated this process?
To enable Flash before each test and WhiteList the url you can use the following code block to configure the WebDriver instance to allow Flash:
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values.plugins", 1);
prefs.put("profile.content_settings.plugin_whitelist.adobe-flash-player", 1);
prefs.put("profile.content_settings.exceptions.plugins.*,*.per_resource.adobe-flash-player", 1);
prefs.put("PluginsAllowedForUrls", "https://your_url.com");
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);
Here you can find a detailed discussion on Manage Flash in Chrome and on PluginsAllowedForUrls
Update :
You haven't mentioned in your comment through which client you are not able to find setExperimentalOption or set_experimental_option. Here is the snapshot from my IDE which have no errors/warnings :
And here is the JavaDoc :
This is what worked for me in the end
Capybara.register_driver :chrome do |app|
chrome_binary = '/Applications/Google Chrome.app'
prefs = {"profile.default_content_setting_values.plugins" => 1,
"profile.content_settings.plugin_whitelist.adobe-flash-player" => 1,
"profile.content_settings.exceptions.plugins.*,*.per_resource.adobe-flash-player" => 1,
"PluginsAllowedForUrls" => "hendricks-as3.localhost.bbc.co.uk"
}
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => { "binary" => chrome_binary + '/Contents/MacOS/Google Chrome', "prefs" => prefs })
Capybara::Selenium::Driver.new(app, :browser => :chrome, :desired_capabilities => capabilities)
end
Reading this documentation helped me realise what to do. (also DebanjanB answer helped me with the specific profile options but as in Java no good to my specific needs, thanks though :-) )
https://sites.google.com/a/chromium.org/chromedriver/capabilities

ChromeOptions To Not Prompt For Download When Running RemoteDriver

I'm trying to debug why when running remote webdriver tests on a headless linux host download dialogs are presented in chrome. I believe the chrome version is 45.
Couple of Env Details
Selenium 2.53 (gem)
Selenium 2.53 Server Jar
Chrome Driver 2.21
The framework/Tests are written in Ruby utilizing Capybara for driving web tests. Here is a brief snippet of how the remote driver is initialized.
prefernces = {
:download => {
:prompt_for_download => false,
:default_directory => '/home/john.doe/Downloads/'
}
}
caps = Selenium::WebDriver::Remote::Capabilities.chrome()
caps['chromeOptions'] = {'prefs' => prefernces}
http_client = Selenium::WebDriver::Remote::Http::Default.new
http_client.timeout = 240
options = {
browser: :remote,
url: "http://<server_url>:4444/wd/hub",
desired_capabilities: caps,
http_client: http_client
}
# Returns Remote Driver
Capybara::Selenium::Driver.new(app, options)
I have verified via the hub that the chromeOptions are set, but when a file is downloaded, we're presented with a file dialog prompt.
I've burned the candle looking for a solution to this problem. Thanks for the help and consideration!
try removing the / from the end of the default_directory and also setting directory_upgrade: true. Other than that make sure the browser has permission to write to the selected directory (note also the correct spelling of preferences)
preferences = {
:download => {
:default_directory => '/home/john.doe/Downloads',
:directory_upgrade => true,
:prompt_for_download => false,
}
}
caps = Selenium::WebDriver::Remote::Capabilities.chrome(
'chromeOptions' => {'prefs' => preferences}
)
Here is an example to download a file with Capybara / Selenium / Chrome :
require 'capybara'
require 'selenium-webdriver'
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app,
:url => "http://localhost:4444/wd/hub",
:browser => :chrome,
:desired_capabilities => Selenium::WebDriver::Remote::Capabilities.chrome(
'chromeOptions' => {
'prefs' => {
'download.default_directory' => File.expand_path("C:\\Download"),
'download.directory_upgrade' => true,
'download.prompt_for_download' => false,
'plugins.plugins_disabled' => ["Chrome PDF Viewer"]
}
}
)
)
end
session = Capybara::Session.new(:chrome)
session.visit "https://www.mozilla.org/en-US/foundation/documents"
session.click_link "IRS Form 872-C"
sleep 20

Custom profile for Chrome

Environment: Mac OS X 10.8.3, Ruby 2.0.0p0, selenium-webdriver 2.32.1, ChromeDriver 26.0.1383.0.
I want to change default browser language. I am testing if the site detects the browser language correctly and displays the pages in the language.
I was able to set Firefox language to German:
require "selenium-webdriver"
profile = Selenium::WebDriver::Firefox::Profile.new
profile["intl.accept_languages"] = "de"
caps = Selenium::WebDriver::Remote::Capabilities.firefox(firefox_profile: profile)
caps.platform = "Linux"
caps.version = 20
driver = Selenium::WebDriver.for(
:remote,
url: "http://USERNAME:ACCESS-KEY#ondemand.saucelabs.com:80/wd/hub",
desired_capabilities: caps)
driver.navigate.to "http://sandbox.translatewiki.net/"
I want to do the same using Chrome (and other browsers, if possible).
I have tried several things trying to open the page in German in Chrome, but every time the page is displayed in English, instead of in German.
require "selenium-webdriver"
profile = Selenium::WebDriver::Chrome::Profile.new
profile["intl.accept_languages"] = "de"
caps = Selenium::WebDriver::Remote::Capabilities.chrome(firefox_profile: profile)
caps.platform = "Linux"
caps.version = ""
driver = Selenium::WebDriver.for(
:remote,
url: "http://USERNAME:ACCESS-KEY#ondemand.saucelabs.com:80/wd/hub",
desired_capabilities: caps)
driver.navigate.to "http://sandbox.translatewiki.net/"
If I change firefox_profile: profile to profile: profile or chrome_profile: profile, the page opens in English (instead of in German) every time.
As far as I can see in the API docs, only :firefox_profile is supported.
I was able to do it on a local machine, but not when using Sauce Labs.
This should work:
require "selenium-webdriver"
profile = Selenium::WebDriver::Chrome::Profile.new
profile["intl.accept_languages"] = "de"
caps = Selenium::WebDriver::Remote::Capabilities.chrome(
platform: "Linux",
version: "",
'chrome.profile' => profile.as_json['zip']
)
Selenium::WebDriver.for(:remote,
url: "http://...#ondemand.saucelabs.com:80/wd/hub",
desired_capabilities: caps
)
Wow, the documentation for SauceLabs + Chrome + Selenium + Ruby is very inconsistent and sometimes contradictory. Unfortunately I do not have a SauceLabs account to test so all I can do is give you suggestions.
This documentation says it is a known issue that ChromeDriver does not support a custom profile. This post shows how to set a custom profile for Chrome. Go figure.
Setting a profile or a default language for that matter is NOT part of the standard WebDriver wire protocol so you may be out of luck.
One workaround would be to set your browser to use a proxy and in the proxy add/replace the Accept-Language header in the proxy.
Still, looking through the Selenium Ruby code, it looks like that post might be on to something, so give this a try:
profile = Selenium::WebDriver::Chrome::Profile.new
profile["intl.accept_languages"] = "de"
caps = Selenium::WebDriver::Remote::Capabilities.chrome
caps['chromeOptions'] = { 'profile' => profile.as_json['zip'] }
driver = Selenium::WebDriver.for(
:remote,
url: "http://USERNAME:ACCESS-KEY#ondemand.saucelabs.com:80/wd/hub",
desired_capabilities: caps)
driver.navigate.to "http://sandbox.translatewiki.net/"
EDIT: It seems like the --lang- switch does not do what you want, so ignore the following. I'm leaving it here for posterity.
This might work (forget about the profile, use command line switches):
caps = Selenium::WebDriver::Remote::Capabilities.chrome
caps['chrome.switches'] = ['--lang-de']
I am seeing the German Translation on my local machine using:
profile = Selenium::WebDriver::Chrome::Profile.new
profile["intl.accept_languages"] = "de"
#driver = Selenium::WebDriver.for :chrome, :profile => profile
#target = 'http://sandbox.translatewiki.net/'
osx: 10.7.5
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.4.2]
For now you can just use this method
def launch_browser options={}
language = options.fetch(:language, "en_US")
url = options.fetch(:url, "www.google.com")
prefs = {
:intl => {
:accept_languages => language
}
}
browser = Watir::Browser.new :chrome, :prefs => prefs
browser.goto url
end
and then you just have to call
launch_browser :language => "de"

profile.assume_untrusted_certificate_issuer = false not working for me

Using watir-webdriver for automation I'm not able to handle Firefox "Untrusted Connection" . Already tried this:
require 'watir-webdriver'
profile = Selenium::WebDriver::Firefox::Profile.new
profile.assume_untrusted_certificate_issuer = false
browser = Watir::Browser.new(:firefox, :profile => profile)
browser.goto("http://xxx.xxx.xxx.com)
Still got the same result ? Any help would be appreciated...
This worked for me :
#profile=Selenium::WebDriver::Firefox::Profile.from_name "default"
#profile.assume_untrusted_certificate_issuer=false
#profile.secure_ssl = true
browser = Watir::Browser.new :firefox, :profile => #profile
Capabilities help me:
capabilities = Selenium::WebDriver::Remote::Capabilities.firefox(accept_insecure_certs: true)
$driver = Selenium::WebDriver.for :firefox, desired_capabilities: capabilities
geckodriver 0.14.0, Mozilla Firefox 52.0, selenium-webdriver 3.2.1

Resources