Needed to execute the below set of code. Please refer my code:
Capybara.register_driver :logging_selenium_chrome do |app|
caps = Selenium::WebDriver::Remote::Capabilities.chrome(loggingPrefs:
{browser: 'ALL'})
browser_options = ::Selenium::WebDriver::Chrome::Options.new()
Capybara::Selenium::Driver.new(app, browser: :chrome, options:
browser_options, desired_capabilities: caps)
end
but keeps on getting
uninitialized constant Selenium::WebDriver::Chrome::Options (NameError)
Anyone having any idea what might be causing this!!!
::Selenium::WebDriver::Chrome::Options was added in selenium-webdriver 3.4.1 - Upgrade to the latest selenium-webdriver gem (3.5.2 currently)
It seems that you need to Capybara for declaring browser_options. see the below:
Capybara.register_driver :logging_selenium_chrome do |app|
caps = Selenium::WebDriver::Remote::Capabilities.chrome(loggingPrefs:
{browser: 'ALL'})
browser_options = Capybara::Selenium::WebDriver::Chrome::Options.new()
Capybara::Selenium::Driver.new(app, browser: :chrome, options:
browser_options, desired_capabilities: caps)
end
Related
The following is my setup
just three files to start with. no folder structure
Gemfile
gem 'capybara'
gem 'selenium-webdriver'
spec_helper.rb
require 'capybara/rspec'
require "selenium/webdriver"
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome)
end
Capybara.register_driver :headless_chrome do |app|
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
chromeOptions: { args: %w(headless disable-gpu) }
)
Capybara::Selenium::Driver.new app,
browser: :chrome,
desired_capabilities: capabilities
end
Capybara.javascript_driver = :headless_chrome
run.rb
require_relative 'spec_helper'
describe "test process" do
it "checks google" do
visit("www.google.com")
puts "LAUNCHED"
end
end
New to testing. Any help would be appreciated.
I ran it using
rspec run.rb
By default Capybaras methods are only included in RSpec tests of type :feature and :system - https://github.com/teamcapybara/capybara/blob/master/lib/capybara/rspec.rb#L10
Tag your test with the correct type and the methods will be available
describe 'test proces', type: :feature do
I'm trying to launch chrome in headless but the browser keeps on launching. Tried several different ways
used chrome options and added arguments
used chrome capabilities as well
My chrome version: 86
OS - ubuntu
capybara - 3.32.2
First Helper file:
spec_helper.rb
Capybara.register_driver :headless_chrome do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome,
options: Selenium::WebDriver::Chrome::Options.new(args: %w[headless no-sandbox disable-gpu]))
end
Capybara.default_driver = :headless_chrome
Capybara.javascript_driver = :headless_chrome
Second try of Helper file:
Capybara.register_driver :headless_chrome do |app|
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
desired_capabilities:Selenium::WebDriver::Remote::Capabilities.chrome(
chromeOptions: {
args: %w[headless disable-gpu disable-popup-blocking no-sandbox]
}
)
)
end
Capybara.default_driver = :headless_chrome
Capybara.javascript_driver = :headless_chrome
Thanks in advance
Simplest way is to not register your own driver and just use the one provided by Capybara
Capybara.default_driver = :selenium_chrome_headless
Capybara.javascript_driver = :selenium_chrome_headless
Note - this will only work if you're not using rails system tests (no indication in your question that you're using rails though) since they override these settings - see the rails/rspec system test docs (driven_by) for that.
Resolved this by adding chromeOptions separately and had to remove -- from all arguments
Apparently, for Selenium >= 3.8, you have to specify goog:chromeOptions instead of chromeOptions.
So in your example:
...
Selenium::WebDriver::Remote::Capabilities.chrome(
"goog:chromeOptions": {
args: %w[headless disable-gpu disable-popup-blocking no-sandbox]
}
)
)
I was experiencing the same issue and this solved it for me.
Writing a standalone piece of code I can use
session.visit('/forms')
but how could I use
visit('/forms')
Code:
require 'webdrivers'
require 'rspec'
require 'capybara'
require 'capybara/rspec'
Capybara.configure do |config|
config.run_server = false
config.default_driver = :chrome
config.app_host = 'https://google.com'
end
options = Selenium::WebDriver::Chrome::Options.new(args: ['window-size=1200,1200'])
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end
session = Capybara::Session.new(:chrome)
session.visit('/forms') # <-- this works :)
visit('/forms') # <-- but this doesn't :(
I get
undefined method `visit' for main:Object (NoMethodError)
One option is to use include Capybara::DSL, i.e.
include Capybara::DSL
session = Capybara::Session.new(:chrome)
visit('/forms')
However this does give a warning
including Capybara::DSL in the global scope is not recommended!
not sure how to get around that yet.
I used profile["permissions.default.image"] = 2 earlier but now it doesn't work for me:
require "capybara/rspec"
require "selenium/webdriver"
Capybara.register_driver :my_driver do |app|
profile = Selenium::WebDriver::Firefox::Profile.new
profile["permissions.default.image"] = 2
desired_capabilities = Selenium::WebDriver::Remote::Capabilities.firefox firefor_profile: profile
Capybara::Selenium::Driver.new(app,
desired_capabilities: desired_capabilities,
browser: :firefox,
)
end
Capybara.default_driver = :my_driver
feature do
scenario do
visit "http://lenta.ru"
STDIN.gets
end
end
I still see images.
$ /Applications/Firefox.app/Contents/MacOS/firefox-bin -v
Mozilla Firefox 38.3.0
gem "selenium-webdriver", "2.48.0"
Tests that I run well in Chrome and Firefox appear flaky or blinky in IE9. I understand a java based fix for this is setting a capability
caps.setCapability("requireWindowFocus", true);
I interpreted a rough ruby attempt in the env.rb...
Capybara.default_driver = :selenium
Capybara.register_driver :selenium do |app|
case get_browser
when 'ie'
caps = Selenium::WebDriver::Remote::Capabilities.internet_explorer
caps.setCapability("requireWindowFocus", true)
Capybara::Selenium::Driver.new(app, :browser => :internet_explorer, :desired_capabilities => caps)
else
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
end
and caught the following nomethod error
NoMethodError: undefined method `setCapability' for #<Selenium::WebDriver::Remote::Capabilities:0x3e9b218>
A ruby fix for this will be greatly appreciated.
Based on the Ruby bindings page, the capabilities are set like:
caps = Selenium::WebDriver::Remote::Capabilities.internet_explorer
caps["requireWindowFocus"] = true
Capybara::Selenium::Driver.new(app, :browser => :internet_explorer, :desired_capabilities => caps)