Jump to content

Selenium/Ruby/Stack

From mediawiki.org

This page explains stack from Selenium/Ruby page. This tutorial will assume that you are running tests from your machine, targeting beta cluster. Code from this page is available at mediawiki-selenium-rb repository.

Examples will:

  • open browser
  • go to main page
  • check that Log in link is present
  • close browser

The stack:

Language Ruby
Browser Chrome
Selenium/WebDriver Ruby bindings (wiki, API, package)
Assertion library N/A
Testing framework N/A
Page object N/A
Shared code between MediaWiki repositories N/A
Nicer API on top of Selenium N/A

Selenium

[edit]

Advantages

[edit]
  • Minimal stack.

Disadvantages

[edit]
  • No testing framework (assertions, setup, tear-down, reporting...).

Code

[edit]
# setup
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :chrome

# test
driver.navigate.to 'https://en.wikipedia.beta.wmflabs.org/wiki/Main_Page'
displayed = driver.find_element(:link_text, 'Log in').displayed?
raise 'Could not find "Log in" element' unless displayed

# teardown
driver.quit

Save the code in file (for example selenium/main_page.rb) and run it.

Output

[edit]

Everything is fine.

$ bundle exec ruby selenium/main_page.rb

There is a problem.

$ bundle exec ruby selenium/main_page.rb 
... Unable to locate element: {"method":"link text","selector":"Log in"} (Selenium::WebDriver::Error::NoSuchElementError)
...

Watir

[edit]

Stack

[edit]

In addition to the default stack, this example additionally uses:

Language Ruby
Nicer API on top of Selenium Watir

Advantages

[edit]
  • Nicer API on top of Selenium.

Disadvantages

[edit]
  • No testing framework (assertions, setup, tear-down, reporting...).
  • Another tool to learn (Watir).

Code

[edit]
# setup
require 'watir'
driver = Watir::Browser.new :chrome

# test
driver.goto 'en.wikipedia.beta.wmflabs.org/wiki/Main_Page'
present = driver.element(text: 'Log in').present?
raise 'Could not find "Log in" element' unless present

# teardown
driver.quit

Save the code in file (for example selenium/main_page_watir.rb) and run it.

Output

[edit]

Everything is fine.

$ bundle exec ruby selenium/main_page_watir.rb

There is a problem.

$ bundle exec ruby selenium/main_page_watir.rb 
... Could not find "Log in" element (RuntimeError)

page-object

[edit]

Stack

[edit]

In addition to the default stack, this example additionally uses:

Language Ruby
Page object page-object

Advantages

[edit]
  • Very useful abstraction (page object).

Disadvantages

[edit]
  • No testing framework (assertions, setup, tear-down, reporting...).
  • Another tool to learn (page-object).

Code

[edit]
require 'page-object'
require 'page-object/page_factory'
include PageObject::PageFactory

# page object for Main Page
class MainPage
  include PageObject
  page_url 'http://en.wikipedia.beta.wmflabs.org/wiki/Main_Page'
  a(:log_in, text: 'Log in')
end

# setup
require 'selenium-webdriver'
@browser = Selenium::WebDriver.for :chrome

# test
visit MainPage
exists = on(MainPage).log_in_element.exists?
raise 'Could not find "Log in" element' unless exists

# teardown
@browser.quit

Save the code in file (for example selenium/main_page_page_object.rb) and run it.

Output

[edit]

Everything is fine.

$ bundle exec ruby selenium/main_page_page_object.rb

There is a problem.

$ bundle exec ruby selenium/main_page_page_object.rb 
... Could not find "Log in" element (RuntimeError)

RSpec

[edit]

Stack

[edit]

In addition to the default stack, this example additionally uses:

Language Ruby
Assertion library RSpec Expectations (ships with RSpec)
Testing framework RSpec

Advantages

[edit]
  • Simple stack with assertions and testing framework (setup, tear-down, reporting...).

Disadvantages

[edit]
  • Another tool to learn (testing framework).

Code

[edit]
require 'selenium-webdriver'

RSpec.describe 'Main Page' do
  before(:each) do
    @driver = Selenium::WebDriver.for :chrome
  end

  after(:each) do
    @driver.quit
  end

  it 'should have "Log in" link' do
    @driver.navigate.to 'https://en.wikipedia.beta.wmflabs.org/wiki/Main_Page'
    expect(@driver.find_element(:link_text, 'Log in').displayed?).to be true
  end
end

Save the code in file (for example spec/main_page_spec.rb) and run it.

Output

[edit]

Everything is fine.

$ bundle exec rspec spec/main_page_spec.rb
.

Finished in 4.05 seconds (files took 0.12979 seconds to load)
1 example, 0 failures

There is a problem.

$ bundle exec rspec spec/main_page_spec.rb 
F

Failures:

  1) Main Page should have "Log in" link
     Failure/Error: expect(@driver.find_element(:link_text, 'Log in').displayed?).to be true
     
     Selenium::WebDriver::Error::NoSuchElementError:
       Unable to locate element: {"method":"link text","selector":"Log in"}
...
Finished in 4.55 seconds (files took 0.12823 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/main_page_spec.rb:12 # Main Page should have "Log in" link

Cucumber

[edit]

Stack

[edit]

In addition to the default stack, this example additionally uses:

Language Ruby
Assertion library RSpec Expectations (ships with RSpec)
Testing framework Cucumber

Advantages

[edit]
  • Simple stack with assertions and testing framework (setup, tear-down, reporting...).

Disadvantages

[edit]
  • Another tool to learn (testing framework).

Code

[edit]

features/main_page.feature

Feature: Main page

  Scenario: There is "Log in" link at the main page
    Given I am at the main page
    Then "Log in" link should be there

features/step_definitions/main_page_steps.rb

require 'selenium-webdriver'

Before do
  @driver = Selenium::WebDriver.for :chrome
end

After do
  @driver.quit
end

Given(/^I am at the main page$/) do
  @driver.navigate.to 'https://en.wikipedia.beta.wmflabs.org/wiki/Main_Page'
end

Then(/^"Log in" link should be there$/) do
  expect(@driver.find_element(:link_text, 'Log in').displayed?).to be true
end

Save the code in files and run Cucumber.

Output

[edit]

Everything is fine.

$ bundle exec cucumber features/main_page.feature 
Feature: Main page

  Scenario: There is "Log in" link at the main page # features/main_page.feature:3
    Given I am at the main page                     # features/step_definitions/main_page_steps.rb:3
    Then "Log in" link should be there              # features/step_definitions/main_page_steps.rb:15

1 scenario (1 passed)
2 steps (2 passed)
0m5.523s

There is a problem.

$ bundle exec cucumber features/main_page.feature 
Feature: Main page

  Scenario: There is "Log in" link at the main page # features/main_page.feature:3
    Given I am at the main page                     # features/step_definitions/main_page_steps.rb:11
    Then "Log in" link should be there              # features/step_definitions/main_page_steps.rb:15
      Unable to locate element: {"method":"link text","selector":"Log in"} (Selenium::WebDriver::Error::NoSuchElementError)
...
Failing Scenarios:
cucumber features/main_page.feature:3 # Scenario: There is "Log in" link at the main page

1 scenario (1 failed)
2 steps (1 failed, 1 passed)
0m4.608s