Selenium/Ruby/Write

From mediawiki.org
< Selenium‎ | Ruby

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. More details about the stack are available at Selenium/Ruby/Stack page. mediawiki-selenium-cucumber repository has more code examples.

Examples will:

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

Stack[edit]

The stack:

Language Ruby
Browser Chrome
Selenium/WebDriver Ruby bindings (wiki, API, package)
Assertion library RSpec Expectations (ships with RSpec)
Testing framework Cucumber
Page object page-object
Shared code between MediaWiki repositories mediawiki_selenium
Nicer API on top of Selenium N/A

Advantages[edit]

  • Good and relatively simple stack with assertions, testing framework, page object library and shared code between MediaWiki repositories.

Disadvantages[edit]

  • Several new tools to learn.

Code[edit]

tests/browser/environments.yml

beta: &default
  mediawiki_url: http://en.wikipedia.beta.wmflabs.org/wiki/

default: *default

tests/browser/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

tests/browser/features/step_definitions/main_page_steps.rb

require 'page-object'
# page object for Main Page
class MainPage
  include PageObject
  page_url 'Main_Page'
  a(:log_in, text: 'Log in')
end

require 'mediawiki_selenium/cucumber'
Given(/^I am at the main page$/) do
  visit(MainPage)
end

Then(/^"Log in" link should be there$/) do
  expect(on(MainPage).log_in_element).to exist
end

Save the code in files and run Cucumber.

Output[edit]

Everything is fine.

$ bundle exec cucumber
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:10
    Then "Log in" link should be there              # features/step_definitions/main_page_steps.rb:14

1 scenario (1 passed)
2 steps (2 passed)
0m9.113s

There is a problem.

$ bundle exec cucumber
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:10
    Then "Log in" link should be there              # features/step_definitions/main_page_steps.rb:14
      expected #<Watir::Anchor:0x1e1f25d3500cd5d0 located=false selector={:text=>"Log in", :tag_name=>"a"}> to exist (RSpec::Expectations::ExpectationNotMetError)
      features/main_page.feature:5:in `Then "Log in" link should be there'

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)
0m5.701s