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

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.

Related

How to update my driver initialization to avoid deprecation warning?

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

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

What are the capabilities of the browser opened by selenium-webdriver?

I am curious to know the capabilities of the Chrome browser opened through the code:
driver=webdriver.chromedriver()
Is it same as that of incognito mode or is there a different kind?
To extract the capabilities of the ChromeDriver / Chrome Browser you can use the capabilities property which returns a dictionary and you can use the following solution:
Code Block:
from selenium import webdriver
driver = webdriver.Chrome(executable_path="C:\\Utility\\BrowserDrivers\\chromedriver.exe")
my_dict = driver.capabilities
for key,val in my_dict.items():
print (key, "=>", val)
driver.quit()
Console Output:
acceptInsecureCerts => False
browserName => chrome
browserVersion => 76.0.3809.100
chrome => {'chromedriverVersion': '76.0.3809.68 (420c9498db8ce8fcd190a954d51297672c1515d5-refs/branch-heads/3809#{#864})', 'userDataDir': 'C:\\Users\\Debanjan.B\\AppData\\Local\\Temp\\scoped_dir3888_683123771'}
goog:chromeOptions => {'debuggerAddress': 'localhost:26050'}
networkConnectionEnabled => False
pageLoadStrategy => normal
platformName => windows nt
proxy => {}
setWindowRect => True
strictFileInteractability => False
timeouts => {'implicit': 0, 'pageLoad': 300000, 'script': 30000}
unhandledPromptBehavior => dismiss and notify
You can find a relevant discussion in How to provide custom capabilities on the selenium server?

Enable Popups chrome browser for protractor test

Protractor creates a brand new Chrome profile every time it runs. So what i want to do it will enable popup for chrome browser every time when protractor test runs.
I don't want to use existing profile.Is there any way to do ?
You can create instance of chrome custom profile and it can be passed to capabilities.
export const chromeProfile = {
browserName : 'chrome',
maxInstances: 1,
chromeOptions: {
args: [
// mention list of all setting for chrome browser
//For example following two lines disable chrome popup
'disable-infobars=true',
'--disable-popup-blocking'
],
prefs: {
// disable password popup
'credentials_enable_service': false
}
}
};

undefined method `visit' for Selenium::WebDriver::Driver

Trying to write a simple capybara example against google but getting
undefined method `visit' for #<Selenium::WebDriver::Driver:0x000000055f8cc8>
I can use
driver.get("http://www.google.com/")
but I can't use
driver.visit("http://www.google.com/")
I have:
require "rspec"
require 'selenium-webdriver'
require "capybara"
require "capybara/rspec"
require "capybara/dsl"
RSpec.configure do |config|
config.include Capybara::DSL
end
Capybara.configure do |config|
config.run_server = false
config.default_driver = :selenium
config.app_host = 'https://www.google.com'
end
describe "Google Search", type: :feature do
it "Tests Google" do
driver = Selenium::WebDriver.for :chrome
driver.visit "http://www.google.com/" <-- Error
fill_in('input', with: '123')
find_element('input', "Google Search").click
driver.quit
end
end
Note that I have to use chrome as my selenium firefox setup is out of sync (common problem over time - it's unable to start firefox in 60 secs). But chrome works and the browser comes up.
This simple ruby only example does work however so seems like some sort of rspec setup issue
require 'rubygems'
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :chrome
driver.get "http://google.com"
element = driver.find_element :name => "q"
element.send_keys "Cheese!"
element.submit
puts "Page title is #{driver.title}"
wait = Selenium::WebDriver::Wait.new(:timeout => 10)
wait.until { driver.title.downcase.start_with? "cheese!" }
puts "Page title is #{driver.title}"
driver.quit
You should not be using the driver directly - you should be calling visit on the session (if you're managing your own sessions you would call it on whatever variable you used, if you're letting Capybara manage the sessions you should be calling it on page).
The reason Firefox isn't working for you is because Firefox 47 broke something with selenium - https://github.com/SeleniumHQ/selenium/issues/2110 - it will be fixed in a 47.0.1 release soon, or you can revert to 46. If you want to stick with chrome you should register a version of the driver using chrome in your spec/rails_helper and specify that
Capybara.register_driver :selenium_chrome do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
Capybara.default_driver = :selenium_chrome # for most people this would normally be assigned to javascript_driver, but since you're using selenium for all tests we can just assign to default_driver
Then you would just do
describe "Google Search", type: :feature do
it "Tests Google" do
page.visit "http://www.google.com/" #technically the page may not be required here but it can prevent method name collisions with other libraries
page.fill_in('input', with: '123')
find_element('input', "Google Search").click # I'm guessing this is your own defined method since Capybara doesn't have a find_element method?
end
end