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

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

Related

Chrome headless download pdf

I've got a script which downloads a pdf from a site which keeps updating every month and I want to automate this. It works but I cannot get it to work headless and I think it's because it's not handling the downloads correctly. It seems to start the chrome in headless ok and my navigation commands seem to work, but when it goes to download nothing happens.
#!/usr/bin/env ruby
#
require 'capybara'
require 'rb-inotify'
require 'webdrivers/chromedriver'
def initialise
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome, options: chrome_options)
end
#session = Capybara::Session.new(:chrome)
end
# Settings and profile for the Chrome Browser
# NOTE: still cannot get headless working
def chrome_options
opts = Selenium::WebDriver::Chrome::Options.new
opts.add_argument('--headless') unless ENV['UI']
opts.add_argument('--no-sandbox')
opts.add_argument('--disable-gpu')
opts.add_argument('--disable-dev-shm-usage')
opts.add_argument('--window-size=1920,1080')
opts.add_preference(:download,
directory_upgrade: true,
prompt_for_download: false,
default_directory: "~/Downloads")
opts.add_preference(:plugins,
plugins_disabled: ["Chrome PDF Viewer"])
opts.add_preference(:browser, set_download_behavior: { behavior: 'allow' })
opts
end
Update I'm using Chrome version 81.0.4044.113-1
Across different versions of Chrome and selenium-webdriver the settings required to get downloads working have changed/grown. It looks like you're missing one of them.
opts.add_preference('download.default_directory', '~/Downloads')
Another thing you can also do, depending on versions, is
def initialise
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome, options: chrome_options).tap do |driver|
driver.browser.download_path = '~/Downloads'
end
#session = Capybara::Session.new(:chrome)
end
I added some configurations to your code, probably it will work:
#!/usr/bin/env ruby
#
require 'capybara'
require 'rb-inotify'
require 'webdrivers/chromedriver'
def initialise
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome, options: chrome_options)
end
#session = Capybara::Session.new(:chrome)
end
# Settings and profile for the Chrome Browser
# NOTE: still cannot get headless working
def chrome_options
download_directory = "~/Downloads"
opts = Selenium::WebDriver::Chrome::Options.new
opts.add_argument('--headless') unless ENV['UI']
opts.add_argument('--no-sandbox')
opts.add_argument('--disable-gpu')
opts.add_argument('--disable-dev-shm-usage')
opts.add_argument('--window-size=1920,1080')
opts.add_preference(:download,
directory_upgrade: true,
prompt_for_download: false,
default_directory: download_directory)
opts.add_preference(:browser, set_download_behavior: { behavior: 'allow' })
driver = Capybara::Selenium::Driver.new(app, browser: :chrome,
options: options)
bridge = driver.browser.send(:bridge)
path = '/session/:session_id/chromium/send_command'
path[':session_id'] = bridge.session_id
bridge.http.call(:post, path, cmd: 'Page.setDownloadBehavior',
params: {
behavior: 'allow',
downloadPath: download_directory
})
driver
opts.add_preference(:plugins,
always_open_pdf_externally: true)
opts.add_preference(:browser, set_download_behavior: { behavior: 'allow' })
opts
end

Capybara - Chromium driver with Selenium socks proxy not working but poltergeist works

Using Chromium 62.0.3168.0 I have confirmed it uses my socks5 proxy when running chrome directly: ./chrome --headless --remote-debugging-port=9222 --proxy-server=socks5://127.0.0.1:9055 http://bot.whatismyipaddress.com but when I try to run through Capybara I earn an error:
SOCKSError::ServerFailure: general SOCKS server failure
How do I get Capybara to use headless_chrome through my socks proxy?
My chrome Capybara setup that isn't working:
Capybara.default_driver = :headless_chromium
Capybara.register_driver :headless_chromium do |app|
Capybara::Selenium::Driver.new(app, {
browser: :chrome,
desired_capabilities: Selenium::WebDriver::Remote::Capabilities.chrome(
"chromeOptions" => {
'binary' => "#{ENV['HOME']}/chromium/src/out/Default/chrome",
'args' => ["no-sandbox", "disable-gpu", "headless",
"proxy-server=socks5://127.0.0.1:9055"]
}
)
})
end
If I remove the proxy-server arg from the above code, the :headless_chromium driver works but doesnt use the socks proxy.
Here is my Capybara setup for poltergeist that does succesfully use the socks proxy:
Capybara.default_driver = :poltergeist
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, {
:phantomjs => Phantomjs.path,
:phantomjs_options => ["--proxy-type=socks5", "--proxy=127.0.0.1:9055"]
})
end

Selenium Tag Ruby Cucumber

I'm using Selenium-Webdriver with the ChromeDriver installed as well as PhantomJS to automate my test pack. There are some scenarios that I would prefer to run in browser when my pack is running headless. I can tag my scenario as #selenium but when it loads, it opens up in firefox. How do I get it to open up ChromeDriver when using the #selenium tag please?
I have the following in my env.rb file to run in browser:
if ENV['chrome']
Capybara.default_driver = :selenium
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app,
:browser => :chrome,
desired_capabilities: {
"chromeOptions" => {
"args" => %w{ window-size=1080,720 }
}
}
)
I set chrome=true to run in browser via cmd.
The solution, following on from Dave McNulla's guidance is as follows:
Before ('#tag') do |scenario|
Capybara.default_driver = :selenium
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app,
:browser => :chrome,
desired_capabilities: {
"chromeOptions" => {
"args" => %w{ window-size=1080,720 }
}
}
)
end
end
I've set it to open in Chrome, only because it's default download action doesn't open up a pop up window, it goes directly to the downloads folder which is ideal for my tests.

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

Remote WebDriver ignore certificate errors for Chrome

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.

Resources