Selenium Webdriver Error - SessionNotCreatedError - ruby

I'm developing automated tests with Capybara on ruby. And I'm struggling to solve this error for days. I've tried to change chrome/chromedriver versions to every combination on earth and still getting errors! I've also reinstalled cucumber/ruby/devkit etc..
By the way, my automated tests were working pretty well, and suddenly they were not anymore.
Someone helps please!
C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.0.3/lib/selenium/webdriver/remote/response.rb:69:in `assert_ok': session not created exception (Selenium::WebDriver::Error::SessionNotCreatedError)
from disconnected: Unable to receive message from renderer
(Session info: chrome=54.0.2840.71)
(Driver info: chromedriver=2.26.436362 (5476ec6bf7ccbada1734a0cdec7d570bb042aa30),platform=Windows NT 10.0.10586 x86_64)
require 'capybara'
require 'selenium-webdriver'
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(
app,
browser: :chrome
)
end
Selenium::WebDriver::Chrome.driver_path = 'C:\tools\chromedriver.exe'
Selenium::WebDriver::Chrome.path = 'C:\tools\chrome64_54.0.2840.71\chrome.exe'
Capybara.default_driver = :selenium
url = "https://github.com/jnicklas/capybara"
internet = Capybara::Session.new(:selenium)
internet.visit url
sleep(20)

I would suggest try downgrading your Chrome version to v54 or v50 and try then. From your error log it seems a http request is not being established and so the webdriver is unable to create a session. Integrate a Selenium standalone server in your project and try running the tests then. The latest version is v3.0.1, you can download it from here:
http://www.seleniumhq.org/download/

Related

How to fix "Chrome process did not produce websocket url within 2 seconds" when running chrome headless in Docker

I'm using Cuprite driver for Capybara in my Ruby feature specs.
The specs run fine locally but fail with the error, Chrome process did not produce websocket url within 2 seconds, when the specs are run on our CI server. The CI server runs the specs within a Docker container.
The Docker image installs a recent version of Chrome, 77.0, from the Google PPA.
The driver needs to be configured to pass the --no-sandbox option to Chrome:
Capybara.register_driver :cuprite do |app|
browser_options = {}.tap do |opts|
opts['no-sandbox'] = nil if ENV['CI']
end
Capybara::Cuprite::Driver.new(app, browser_options: browser_options)
end
I was just struggling with this issue on GitHub Actions. It turns out that the default process_timeout of 1 second is just too short, so Chrome didn't have time to start on the CI servers. It now works for me after extending process_timeout to a much larger value (20 seconds.) Here's the full code for my Capybara :cuprite driver:
Capybara.register_driver(:cuprite) do |app|
browser_options = {}
browser_options['no-sandbox'] = nil if ENV['CI']
headless = !ENV['SHOW_BROWSER']
Capybara::Cuprite::Driver.new(
app,
browser_options: browser_options,
headless: headless,
process_timeout: 20
)
end
Capybara.javascript_driver = :cuprite

Uninitialized constant Selenium::WebDriver::Chrome::OptionsDid you mean? Selenium::WebDriver::Options (NameError)

Recently I went through the chrome headless browser selenium automation. I use Selenium-cucumber with ruby.Now I wanted to run my whole project in chrome headless mode using Selenium::WebDriver::Chrome::Options.
I updated selenium-webdriver, chrome driver and my chrome version is 61.0.3163.100
In order to get to know its working, I created test.rb with below code:
require "selenium-webdriver"
#configure the driver to run in headless mode
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
options.add_argument('--disable-gpu')
driver = Selenium::WebDriver.for :chrome, options: options
driver.get('http://stackoverflow.com/')
puts driver.title
driver.quit
I was able to get the response as below:
>>ruby test.rb
[1012/112029.980:ERROR:devtools_http_handler.cc(786)]
DevTools listening on 127.0.0.1:12703
Stack Overflow - Where Developers Learn, Share, & Build Careers
Now as I got the response correctly I want to configure the driver to run my project in headless mode.
so I edited the env.rb file of the support folder:
Imported require 'selenium-webdriver' and edited the below part where we create driver instance:
else # else create driver instance for desktop browser
begin
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('headless')
options.add_argument('--disable-gpu')
driver = Selenium::WebDriver.for :chrome, options: options
driver.get('http://stackoverflow.com/')
puts driver.title
rescue Exception => e
puts e.message
Process.exit(0)
end
end
I ran the command >>cucumber BROWSER=chrome
uninitialized constant Selenium::WebDriver::Chrome::Options
Did you mean? Selenium::WebDriver::Options
Please help me to resolve this issue and How can I run my whole selenium-cucumber project in headless mode?

How do I use Selenium Webdriver on headless Chrome?

I'm learning to use Selenium for basic things like taking a screenshot, scraping, and testing and would like to use it with headless Chrome, which is now stable as of Chrome 59.
I have been able to take a screenshot using the 'selenium-webdriver' gem and chromedriver, but not headless.
Here is the ruby script that I am running which hangs after starting to initialize the driver
require 'rubygems'
require 'selenium-webdriver'
Selenium::WebDriver.logger.level = :debug
p 'initializing driver'
driver = Selenium::WebDriver.for :chrome, switches: %w[--headless --disable-gpu --screenshot --hide-scrollbars]
p 'navigating to Google'
driver.navigate.to "http://google.com"
driver.save_screenshot("./screen.png")
driver.quit
and the output from the logs:
:> ruby rubytest.rb
"initializing driver"
2017-06-07 15:55:43 DEBUG Selenium Executing Process
["/Users/name/Documents/scrapings/python/env/bin/chromedriver", "--port=9515"]
2017-06-07 15:55:43 DEBUG Selenium polling for socket on ["127.0.0.1", 9515]
Starting ChromeDriver 2.29.461585 (0be2cd95f834e9ee7c46bcc7cf405b483f5ae83b) on port 9515
Only local connections are allowed.
2017-06-07 15:55:43 INFO Selenium -> POST session
2017-06-07 15:55:43 INFO Selenium >>> http://127.0.0.1:9515/session | {"desiredCapabilities":{"browserName":"chrome","version":"","platform":"ANY","javascriptEnabled":true,"cssSelectorsEnabled":true,"takesScreenshot":false,"nativeEvents":false,"rotatable":false,"chromeOptions":{"args":["--headless","--disable-gpu","--screenshot","--hide-scrollbars"]}}}
2017-06-07 15:55:43 DEBUG Selenium > {"Accept"=>"application/json", "Content-Type"=>"application/json; charset=utf-8", "Content-Length"=>"284"}
[RUBY BACKTRACE TO DRIVER INITIALIZATION]
I have tried using the JavaScript and Python drivers with similar code and nothing works. When I try this with Python, the error message is
WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
(Driver info: chromedriver=2.29.461585 (0be2cd95f834e9ee7c46bcc7cf405b483f5ae83b),platform=Mac OS X 10.12.5 x86_64)
I found this blog post useful for setting up headless chrome with selenium in ruby
require "selenium-webdriver"
# configure the driver to run in headless mode
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
driver = Selenium::WebDriver.for :chrome, options: options
driver.navigate.to "https://www.google.com"
# resize the window and take a screenshot
driver.manage.window.resize_to(800, 800)
driver.save_screenshot "screenshot.png"
Managed to work through this in the end via various docs, blog posts and gists.
caps = Selenium::WebDriver::Remote::Capabilities.chrome("desiredCapabilities" => {"takesScreenshot" => true}, "chromeOptions" => {"binary" => "/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary"})
driver = Selenium::WebDriver.for :chrome, desired_capabilities: caps, switches: %w[--headless --no-sandbox --disable-gpu --remote-debugin-port=9222 --screen-size=1200x800]
You need to use a very recent version of Chrome (I'm using Canary) and tell Selenium the path to the binary. You also need to set the desired capabilities for 'takesScreenshot' to true.
I've written a blog post about how to do it. Summing up:
1) Make sure you have Chrome version 57+ on Linux, 59+ on macOS or 60+ on Windows;
2) Add/update the gem selenium-webdriver;
3) Make sure you’re using ChromeDriver version 2.30 or higher;
4) Add the following driver to your spec_helper.rb or rails_helper.rb:
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new app, browser: :chrome,
options: Selenium::WebDriver::Chrome::Options.new(args: %w[headless disable-gpu])
end
Capybara.javascript_driver = :chrome

Selenium/Ruby unknown driver, firefox

I wrote a selenium test that I have working in chrome, I'm trying to get selenium to run the test in every browser. When I try and access firefox I get an error though.
Code up to the error...
require "rubygems"
require "selenium-webdriver"
driver = Selenium::WebDriver.for :Firefox
I get the following error on the line where driver is declared...
/Users/username/.rvm/gems/ruby-2.1.0/gems/selenium-webdriver-2.39.0/lib/selenium/webdriver/common/driver.rb:49:in 'for': unknown driver: :Firefox (ArgumentError)
from /Users/samuelowens/.rvm/gems/ruby-2.1.0/gems/selenium-webdriver-2.39.0/lib/selenium/webdriver.rb:67:in 'for'
from test.rb:47:in `<main>'
Any idea why that might be and how to fix it? Is there an easy way to write a selenium test and check it in multiple browsers? Thanks.
Not capital case Firefox but firefox
driver = Selenium::WebDriver.for :firefox

Selenium WebDriver Change Firefox path to Tor

I'm trying to change webdriver in ruby to open a tor browser instead of the default firefox broswer. I'm using the following code and I have a tor browser open before I run this code.
path='C:\Users\Bonnnie\Downloads\Tor Browser\App\tor.exe'
Selenium::WebDriver::Firefox.path = path
driver = Selenium::WebDriver.for :firefox
I get the following error:
unable to obtain stable firefox connection in 60 seconds
I think I might be linking to the wrong tor file.
The following worked for me with selenium-webdriver 2.48.1 and Tor Browser Bundle 5.0.3 on Ubuntu Linux 15.04.
require 'selenium-webdriver'
tor_dir = '/opt/tor-browser_en-US'
# The Tor binary relies on these shared libraries
ENV['LD_LIBRARY_PATH']= [
File.join(tor_dir, 'Browser/'),
File.join(tor_dir, 'Browser/TorBrowser/Tor/')
].join(':')
Selenium::WebDriver::Firefox::Binary.path =
File.join(tor_dir, 'Browser/firefox')
profile = Selenium::WebDriver::Firefox::Profile.new(
File.join(tor_dir, 'Browser/TorBrowser/Data/Browser/profile.default'))
driver = Selenium::WebDriver.for :firefox, :profile => profile
driver.get('https://check.torproject.org/')
require 'selenium-webdriver'
tor_dir = '/home/me/tor-browser_en-US' # change as necessary
options = Selenium::WebDriver::Firefox::Options.new
options.binary = File.join(tor_dir, 'Browser/firefox')
proxy = Selenium::WebDriver::Proxy.new(socks: '127.0.0.1:9050')
proxy.socks_version = 5
options.proxy = proxy
driver = Selenium::WebDriver.for :firefox, options: options
driver.get('https://check.torproject.org')
driver.title
=> "Congratulations. This browser is configured to use Tor."
I had to set the browser proxy to use my locally running tor, as Selenium evidently doesn't use the one bundled with Tor Browser Bundle. With no proxy set I get Selenium::WebDriver::Error::UnknownError: Reached error page: about:neterror?e=proxyConnectFailure..

Resources