DEV Community

radin reth
radin reth

Posted on

cucumber with selenium-webdriver

First you have to install web driver that you prefer

For me, I'm using chromedriver

Install selenium-webdriver gem

gem install selenium-webdriver
Enter fullscreen mode Exit fullscreen mode

Then install chromedriver, currently I am using the latest chrome browser version 93

  1. Download chromedriver from chromedriver's website
  2. Choose the driver that support with your browser, eg: ChromeDriver 93.0.4577.15
  3. Extract the zip file and install the package
  4. Move the executable to under /opt/WebDriver/bin (create if the does not exist).

For more detail visit

I get an example from cucumber official website but have a few tweak to get it works in my local machine

# features/search_cheese_on_google.feature
Feature: Visit google using web driver
  Everybody wants to see the result when we search something on google

  Scenario: search for cheese
    Given I am on the google search page
    When I search for "cheese"
    Then the page title will start with "cheese"
Enter fullscreen mode Exit fullscreen mode

In step definition,

# features/step_definitions/webdriver_step.rb
require 'rubygems'
require 'selenium-webdriver'

Selenium::WebDriver::Chrome::Service.driver_path = "/opt/WebDriver/bin/chromedriver"

Given('I am on the google search page') do
  @driver = Selenium::WebDriver.for :chrome
  @driver.get "http://google.com"
end

When('I search for {string}') do |string|
  element = @driver.find_element name: "q"
  element.send_keys "cheese"
  element.submit
end

Then('the page title will start with {string}') do |expected_result|
  wait = Selenium::WebDriver::Wait.new timeout: 10
  wait.until { @driver.title.downcase.start_with? "cheese" }
  puts "Page title is #{@driver.title}"
  @driver.close
  expect(@driver.title).to include expected_result
end
Enter fullscreen mode Exit fullscreen mode

run

cucumber
Enter fullscreen mode Exit fullscreen mode

You would the result something like this

image

Well done!

Top comments (0)