How to update my driver initialization to avoid deprecation warning? - selenium

The warning I'm receiving is: WARN Selenium [DEPRECATION] [:capabilities_hash] passing a Hash value to :capabilities is deprecated. Use Capabilities instance initialized with the Hash, or build values with Options class instead.
My driver setup looks like:
Capybara.register_driver :aws_chrome do |app|
client = Selenium::WebDriver::Remote::Http::Default.new
client.read_timeout = 180
capabilities = {
'browserName': 'chrome',
'aws:maxDurationSecs': 2400
}
Capybara::Selenium::Driver.new(app, browser: :remote, http_client: client, url: remote_url, capabilities: capabilities)
end
I'm not sure what a capabilities instance is supposed to be, and there's no documentation on how to use the Options class that I could find. I also need to be able to set the browserName to firefox as needed, so I'm not sure if the Options class is specific to chrome.
Thanks in advance.

I'm not familiar with ruby, but for using options instead of capabilities try:
Capybara.register_driver :aws_chrome do |app|
client = Selenium::WebDriver::Remote::Http::Default.new
client.read_timeout = 180
options = Selenium::WebDriver::Chrome::Options.new
options.add_option('aws:maxDurationSecs', 2400)
Capybara::Selenium::Driver.new(app, browser: :remote, http_client: client, url: remote_url, options: options)
end
This is Options class api doc for selenium 4:
https://www.selenium.dev/selenium/docs/api/rb/Selenium/WebDriver/Options.html

Related

How do I correct this Selenium initialisation command deprecation warning?

Using Rails 6 I'm trying to set up selenium in headless mode for system tests, I'm using this statement in application_system_test_case.db:
driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1400]
(according to Agile Web Dev Rails 6 tutorial)
but it gives me this deprecation warning:
Selenium [DEPRECATION] [:browser_options] :options as a parameter for driver initialization is deprecated. Use :capabilities with an Array of value capabilities/options if necessary instead.
I've done some searching in Selenium docs but I my basic code skills still leave me unclear as to how I should correct this. Can anyone advise how I can correct this?
(My amateur guesswork trials of things like:
driven_by :selenium, :capabilities['headless_chrome', 'screen_size: 1400, 1400']
all result in errors)
Updated version for the new warning with options instead of capabilities
Capybara.register_driver :headless_chrome do |app|
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
options: options
)
end
Capybara.register_driver :chrome do |app|
options = Selenium::WebDriver::Chrome::Options.new
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
options: options
)
end
Capybara.default_driver = :chrome
In Selenium 4, the options must be passed in array capabilities:
def selenium_options
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
options
end
# optional
def selenium_capabilities_chrome
Selenium::WebDriver::Remote::Capabilities.chrome
end
def driver_init
caps = [
selenium_options,
selenium_capabilities_chrome,
]
Selenium::WebDriver.for(:chrome, capabilities: caps)
end
driver = driver_init
I stumbled upon this thread a couple of times already. What was bothering to me were not only the deprecated messages, but also the puma server logs while launching the test suite. I've ended up fixing the deprecation warning and shutting the puma logs. Here's my current setup:
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
# provides devise methods such as login_session
include Devise::Test::IntegrationHelpers
# removes noisy logs when launching tests
Capybara.server = :puma, { Silent: true }
Capybara.register_driver :headless_chrome do |app|
options = Selenium::WebDriver::Chrome::Options.new(args: %w[headless window-size=1400,1000])
Capybara::Selenium::Driver.new(app, browser: :chrome, capabilities: options)
end
Capybara.register_driver(:chrome) do |app|
options = Selenium::WebDriver::Chrome::Options.new(args: %w[window-size=1400,1000])
Capybara::Selenium::Driver.new(app, browser: :chrome, capabilities: options)
end
ENV['HEADLESS'] ? driven_by(:headless_chrome) : driven_by(:chrome)
end
So you can launch tests, for instance, with:
HEADLESS=1 rails test:all
Suppress the warning
One line fix:
# rails_helper.rb
Selenium::WebDriver.logger.ignore(:browser_options)
Suggested here
OR
(probably) Any version of Capybara > 3.36.0
Edit: #silvia96 has 3.38.0 and still getting warning
It's quite a confusing bug because if you look at the Capybara driver registration you can see it already knows about using capabilities. The actual bug is because the Gem version test is set as ~ instead of >=. The fix is in main and any version of Capybara after 3.36.0 will, likely, fix it.
Combining the useful solutions from others, here's what mine looks like.
Remove puma logs, make headless chrome, ignore browser options error:
require "test_helper"
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :selenium, using: :headless_chrome
Capybara.server = :puma, { Silent: true }
Selenium::WebDriver.logger.ignore(:browser_options)
end

:driver_opts is deprecated. Use :service with an instance of Selenium::WebDriver::Service instead

2019-06-19 15:44:43 WARN Selenium [DEPRECATION] :driver_opts is deprecated. Use :service with an instance of Selenium::WebDriver::Service instead.
Screenshot error, but continue to execute.
wrong number of arguments (given 0, expected 1)
2019-06-19 15:44:43 WARN Selenium [DEPRECATION] :driver_opts is deprecated. Use :service with an instance of Selenium::WebDriver::Service instead.
2019-06-19 15:44:43 WARN Selenium [DEPRECATION] :driver_opts is deprecated. Use :service with an instance of Selenium::WebDriver::Service instead.
visit in hook, after scenario +1s, #1s
2019-06-19 15:44:43 WARN Selenium [DEPRECATION] :driver_opts is deprecated. Use :service with an instance of Selenium::WebDriver::Service instead.
I've been using the below code snippet to init a session with old 'selenium-webdriver' v3.6.0 and got the above error:
Updated on June 23rd.
if Capybara.default_driver == :selenium
Capybara.register_driver :selenium do |app|
# In the block, we build up an `options` hash to pass to
# Capybara::Selenium::Driver.new(app, options)
# which in turn calls
# Selenium::WebDriver.for(options[:browser], options)
browser = Configuration.fetch('browser.type', :firefox)
options = {
browser: browser, # chrome
}
if Configuration.fetch('options.webdriver.use_hub', false)
{...}
elsif browser == :firefox
{...}
elsif browser == :chrome
chrome_logpath = "../chromedriver.log"
options[:service] = ::Selenium::WebDriver::Service.chrome(
args: {
verbose: true,
log_path: chrome_logpath,
}
)
chrome_options = Selenium::WebDriver::Chrome::Options.new
chrome_options.add_argument("user-agent='QA Test'")
chrome_options.add_option('w3c',false)
options[:options] = chrome_options
end
Capybara::Selenium::Driver.new(app, options)
end
end
After bumped that gem to v3.142.0 then I got that error. Tracing back to the Selenium Webdriver Changelog in https://github.com/SeleniumHQ/selenium/blob/master/rb/CHANGES then i found the following description, which might break the current code
3.141.592 (2019-04-18)
Chrome:
Added support for instantiating service class directly and moved all driver executable configuration there (command-line arguments, port, etc.)
Passing driver_opts, driver_path and port to driver initializer is now deprecated
so use Selenium::WebDriver::Service.chrome instead,
which allows to customize executable behavior in similar way.
Once initialized, this object can be passed as :service keyword
during driver initialization.
* Deprecated Chrome.driver_path= in favor of Service::Chrome.driver_path=
Googling for a while i found some results and workaround like using 'webdriver' gem but i don't like it that much.
So wonder if something that i can change my snippet above to adapt with that selenium-webdriver ver 3.142.0 and onwards? I'm using Capybara v3.18.0 at the moment.
Thanks everyone,
That's not an error, it's a deprecation warning. It's telling you that you will need to change your code before selenium-webdriver v4.0 is released. If you feel you must update your code today it would be something like
elsif browser == :chrome
options[:service] = ::Selenium::WebDriver::Service.chrome(
args: {
verbose: true,
log_path: "../chromedriver.log",
}
)
chrome_options = Selenium::WebDriver::Chrome::Options.new
# add user agent to that class options
chrome_options.add_argument("user-agent='QA Test'")
options[:options] = chrome_options
end
The other stuff you show
Screenshot error, but continue to execute.
wrong number of arguments (given 0, expected 1)
is something different and isn't coming from any of the code you're showing.

How to stop capybara/selenium chrome window from coming to front/focus?

I run a test suite locally that takes about 15 minutes. During this time it's impossible to get any work done because every feature spec brings the chrome window to the front. Is there a configuration option to avoid this, to NOT bring the capybara/selenium window to the front?
This is my current config:
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app,
browser: :chrome,
desired_capabilities: {
"chromeOptions" => {
"args" => %w{ window-size=1740,768 }
}
}
)
end
Capybara.server_port = 57124
Capybara.app_host = "http://localhost:#{Capybara.server_port}" # S3 accesss control blocks default capybara 127.0.0.1
Capybara.default_max_wait_time = 10 #default is 2
You can't if you want/need to run it in non-headless mode, but if you run in headless mode you won't have that issue.

How to ignore ssl certificate in codeception chrome headless browser?

I added in acceptance.suite.yml
chromeOptions:
args: ["--headless", "--disable-gpu","--test-type","--ignore-certificate-errors"],
but no luck? what can i do now
description edit:
When i start tests in headless mode they get stuck, in the _output file (fail.png) i get blank page. So i think that they get to "Insecure Connection" page and can't get through it, and my question is how to avoid that page
I suspect the argument you want is allow-insecure-localhost. This line worked for me to configure my acceptance.suite.yml file in CodeCeption.
- WebDriver:
url: xxx.com
window_size: false # disabled in ChromeDriver
port: 9515
browser: chrome
capabilities:
goog:chromeOptions:
args: ["allow-insecure-localhost","headless","start-maximized"]
This page lists all the options that chrome supports https://peter.sh/experiments/chromium-command-line-switches/#allow-insecure-localhost. Google themselves link to that (3rd party) page from their own page describing ChromeDriver config https://sites.google.com/a/chromium.org/chromedriver/capabilities.
Not sure if this already answered but according to codeception documentation
https://codeception.com/docs/modules/WebDriver
modules:
enabled:
- WebDriver:
config:
url: 'http://localhost/'
browser: chrome
capabilities:
acceptInsecureCerts: true
This works for me,
ChromeOptions options = (ChromeOptions) caps.getCapability(ChromeOptions.CAPABILITY);
options.addArguments("--headless", "--disable-gpu", "--window-size=1366,768", "--no-sandbox");
caps.setAcceptInsecureCerts(true);
Please try following code .
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--no-sandbox");
options.addArguments("--headless", "--window-size=1920,1200", "--ignore-certificate-errors");
options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
options.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
driver = new ChromeDriver(options);

Headers and Selenium Webdriver 2

Is there any way to add a header in Selenium WebDriver test? (Like with Firefox Modify Headers plugin) I cannot use HtmlUnitDriver, because the browser has to be visible.
WebDriver does not allow you to change or set the headers using any of the browser based drivers. You can find a lot of information about their decision about the headers and the response codes at the following URL.
http://code.google.com/p/selenium/issues/detail?id=141
We use Apache HTTP Client for this type of testing where we do not want to check the rendered page elements, but just the responses and header information.
You can also give browser mob proxy with your selenium tests as well as mentioned in the url above. I have used this for other purposes and it is awesome.
There are alternative methods to do this like Browsermob-Proxy
Since Browsermob-proxy has its own limitations while we work on the selenium grid, the below is how I fixed the problem in my case. Hopefully, might be helpful for anyone with a similar setup.
Add the ModHeader extension to the chrome browser
How to download the Modheader? Link
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File(C://Downloads//modheader//modheader.crx));
// Set the Desired capabilities
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
// Instantiate the chrome driver with capabilities
WebDriver driver = new RemoteWebDriver(new URL(YOUR_HUB_URL), options);
Go to the browser extensions and capture the Local Storage context ID of the ModHeader
Navigate to the URL of the ModHeader to set the Local Storage Context
.
// set the context on the extension so the localStorage can be accessed
driver.get("chrome-extension://idgpnmonknjnojddfkpgkljpfnnfcklj/_generated_background_page.html");
Where `idgpnmonknjnojddfkpgkljpfnnfcklj` is the value captured from the Step# 2
Now add the headers to the request using Javascript
.
((Javascript)driver).executeScript(
"localStorage.setItem('profiles', JSON.stringify([{ title: 'Selenium', hideComment: true, appendMode: '',
headers: [
{enabled: true, name: 'token-1', value: 'value-1', comment: ''},
{enabled: true, name: 'token-2', value: 'value-2', comment: ''}
],
respHeaders: [],
filters: []
}]));");
Where token-1, value-1, token-2, value-2 are the request headers and values that are to be added.
Now navigate to the required web-application.
driver.get("your-desired-website");
Here is a short example of how to use Seleniumwire with Python:
from seleniumwire import webdriver
def set_chrome_driver():
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_argument("--disable-infobars")
options.add_argument("--no-proxy-server")
driver = webdriver.Chrome(executable_path=r'C:\Automation_package\chromedriver.exe')
driver.get('http://172.1.1.1:5000/path/api/')
driver.header_overrides = {"iv-user": "Admin", "iv-groups": "SuperAdmin", "iv-roles": "Viewers",}
driver.get('http://172.1.1.1:5000/path/api/')