Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20



Selenium Ruby Page Object Model Framework

Using Ruby Web Driver gem, we can develop automated test framework. When tests become more and complex, it is not a best practice to put all the code in one ruby class file. Instead, we can use Page Object Model to define in all web elements in "site_element.rb" class and use the element definitions in this class in the main test. This type of approach called Page Object Model Framework approach. In this framework below, we use Mahare ePorfolio site as test example application.

Test Case

Authorized users are able to login Mahara Demo site. Here are test steps.

Test Steps

1.Navigate to and verify that web page title "Home-Mahara" displays.
2.Enter "student2" in the Username filed.
3. Enter "Testing1" in the Password field.

4.Click on the Login button
5.Assert that Logout link is present.
6.Click on the Logout link
7.Wait for login button is displayed.

Test Implementation

We create two Ruby class files: "login_test.rb" and "site_element.rb". 

login_test.rb: is the test class for implementing the test case

site_element.rb: is the Page Object Model class that defines each element in the site to use for the test case

Write following code for the site_element.rb class

require 'selenium-webdriver'
class SiteElement

  def initialize(url)
    @driver=Selenium::WebDriver.for :firefox
    @driver.manage.window.maximize url
  def login_username()
    return @driver.find_element(:id,'login_login_username')
  def login_password()
    return @driver.find_element(:id,'login_login_password')
  def submit_button()
    return @driver.find_element(:id,'login_submit')
  def logout_link()
  def close_browser()

Write following code in the login_test.rb class. 

require_relative 'site_element.rb'
    #define new browser"")

    #input user name

    #input password

    #click on submit button

    #wait until the Logout link displays, timeout in 10 seconds
wait = => 10) # seconds
wait.until {browser.logout_link }

    #if logout link is displayed

puts isLogoutLinkDisplayed

Execute the login_test.rb code and see the result

C:\Ruby21-x64\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) C:/RubymineProjects/SeleniumRubyTest/login_test.rb

Process finished with exit code 0

The test passed. As you see in the code above, you can use Page Object Model to put all element definitions in one class, so that your code is easy to maintain for any UI changes.