Vote up!

0

Testing and browser automation, with Selenium and Rspec: Initial Setup

You can download the Selenium remote grid jar here:

Get the google chrome driver: `wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb `

 java -jar selenium-server-4.26.0.jar standalone

 export TEST_PASSWD=<>
 export SELENIUM_HOST=192.168.86.21
 be rspec spec/features/<some-spec>.rb

https://googlechromelabs.github.io/chrome-for-testing/#stable

export CHROME_VERSION=131.0.6778.69
wget --no-verbose -O /tmp/chrome.deb https://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_${CHROME_VERSION}_amd64.deb \
 && apt install -y /tmp/chrome.deb \
 && rm /tmp/chrome.deb

 CHROMEDRIVER_VERSION=120.0.6099.199 npm install -g chromedriver

From: https://www.selenium.dev/blog/2023/headless-is-going-away/

 options = Selenium::WebDriver::Options.chrome(args: ['--headless=new'])
 driver = Selenium::WebDriver.for :chrome, options: options
 driver.get('https://selenium.dev')
 driver.quit

My rspec configuration that (finally, actually) works:

  before :all do
    ## Works, non-remote : )
    # Capybara.default_driver = :local_selenium_headless
    # options = Selenium::WebDriver::Options.chrome(args: ['--headless=new'])
    # @driver = Selenium::WebDriver.for :chrome, options: options

    ## works, remote : )
    Capybara.default_driver = :remote_selenium_headless
    options = Selenium::WebDriver::Options.chrome(args: ['--headless=new'])
    options.add_argument("--remote-debugging-port=9222")
    options.add_argument("--window-size=1400,1400")
    @driver = Selenium::WebDriver.for( :remote, {
      url: "http://#{SELENIUM_HOST}:4444/wd/hub",
      options: options,
    })
  end

Yes, I've sometimes run it locally, and sometimes on a remote selenium grid. Both ways must work for me.

Just for reference, `require 'capybara_helper'` file contains the following:

Capybara.register_driver :local_selenium do |app|
  options = Selenium::WebDriver::Chrome::Options.new
  options.add_argument("--window-size=1400,700")
  Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end

Capybara.register_driver :local_selenium_headless do |app|
  options = Selenium::WebDriver::Chrome::Options.new
  options.add_argument("--headless")
  options.add_argument("--window-size=1400,1400")
  options.add_argument("--disable-dev-shm-usage")
  Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end

Capybara.register_driver :remote_selenium do |app|
  options = Selenium::WebDriver::Chrome::Options.new
  options.add_argument("--window-size=1400,700")
  options.add_argument("--no-sandbox")
  options.add_argument("--disable-dev-shm-usage")
  options.add_argument("--remote-debugging-port=9222")
  Capybara::Selenium::Driver.new(
    app,
    browser: :remote,
    url: "http://#{SELENIUM_HOST}:4444/wd/hub",
    options: options,
  )
end

Capybara.register_driver :remote_selenium_headless do |app|
  options = Selenium::WebDriver::Chrome::Options.new
  options.add_argument("--headless")
  options.add_argument("--window-size=1400,1400")
  options.add_argument("--no-sandbox")
  options.add_argument("--disable-dev-shm-usage")
  options.add_argument("--remote-debugging-port=9222")
  Capybara::Selenium::Driver.new(app, {
    browser: :remote,
    url: "http://#{SELENIUM_HOST}:4444/wd/hub",
    options: options,
  })
end
Install chrome on Ubuntu

From Linuxcapable.

sudo apt install curl software-properties-common apt-transport-https ca-certificates -y

curl -fSsL https://dl.google.com/linux/linux_signing_key.pub | gpg --dearmor | sudo tee /usr/share/keyrings/google-chrome.gpg > /dev/null

echo deb [arch=amd64 signed-by=/usr/share/keyrings/google-chrome.gpg] http://dl.google.com/linux/chrome/deb/ stable main | sudo tee /etc/apt/sources.list.d/google-chrome.list

sudo apt update
sudo apt install google-chrome-stable

.

Please login or register to post a comment.