SeleniumPlus

SeleniumPlus creates a very thin layer between Selenium and your applications, it gives your a simple and quick way to describe your web site using the Page Object Model.

Installation

$ gem install selenium_plus

Key benefits

  • Native Selenium API
  • Quickly build Page Objects
  • Includes new element class e.g. Table, Select
  • Cucumber tags support

Usage

Using page object model and page factory


# define pages
class LoginPage < SeleniumPlus::Page
  element(:username_input, :id, 'username')
  element(:password_input, :id, 'password')
  element(:submit_btn, :css, 'input[name=commit]')

  def 
    username_input.type('Spark')
    password_input.type('12345')
    submit_btn.click
  end
end

class HomePage < SeleniumPlus::Page

  iframe(:outer_iframe, OuterIframe, :id, 'outer_iframe')
  section(:info_section, InfoSection, :id, 'info')
  element(:show_btn, :css, 'input[value=Show]')

  def show_first_table
    show_btn.click
  end

  def titles
    find_all(:css, 'div.title').map{ |cell| cell.text }
  end

end

# define iframe

class OuterIframe < SeleniumPlus::Iframe

  iframe(:inner_iframe, InnerIframe, :id, 'inner_iframe')
  element(:h_table, :id, 'iframe_h_table')
  element(:search_btn, :css, 'input[name=search]')

  def h_table_hashes
    h_table.table_hashes
  end

  def search_table
    search_btn.click
  end
end

# define sections

class InfoSection < SeleniumPlus::Section
  element(:h_table, :css, 'table.horizontal_headers')
  element(:v_table, :css, 'table.vertical_headers')
  elements(:title_divs, :css, 'div.title')
end

# define site

require 'selenium_plus'
Dir.glob("#{File.dirname(__FILE__)}/sections/*.rb").each{ |file| require file }
Dir.glob("#{File.dirname(__FILE__)}/pages/*.rb").each{ |file| require file }

class TestSite
  def 
    Test::LoginPage.new
  end

  def home_page
    Test::HomePage.new
  end
end

Using SeleniumPlus with Cucumber

In env.rb, you need to require 'test_site'

You can select different browser using cucumber Tag, such as @firefox, @chrome and @ie, by default cucumber will use firefox browser.


@chrome
Scenario: Verify HTML Table with horizontal header
  When I login to home page
  And I show first table
  Then Horizontal table text should be:
    | ID: | Name: | Company: |
    | 1   | Smith | VMware   |
    | 2   | James | EMC      |

Steps:

When /^I login to home page$/ do
  @site = TestSite.new
  @site.login_page.goto('login.html')
  @site.login_page.login
end

Then /^Horizontal table text should be:$/ do |h_table|
  @site.home_page.info_section.h_table.headers_text.should == h_table.headers
  @site.home_page.info_section.h_table.rows_text.should == h_table.rows
end

Then /^Horizontal table inside outer iframe should be:$/ do |h_table|
  @site.home_page.outer_iframe.h_table_hashes.should == h_table.hashes
end

Then /^Horizontal table inside inner iframe should be:$/ do |h_table|
  @site.home_page.outer_iframe.inner_iframe.h_table_hashes.should == h_table.hashes
end

Without using page object model


SeleniumPlus.register_driver :firefox_driver do
  SeleniumPlus::Driver.new(:browser => :firefox)
end

driver = SeleniumPlus.using_driver(:firefox_driver)

driver.visit('http://www.google.com.hk')
driver.find(:id, 'lst-ib').type('Selenium')
driver.find(:css, 'input[name=btnK]').click
driver.find_all(:css, 'li.g')