Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

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 demo.mahara.org 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
    @driver.navigate.to url
  end
  def login_username()
    return @driver.find_element(:id,'login_login_username')
  end
  def login_password()
    return @driver.find_element(:id,'login_login_password')
  end
  def submit_button()
    return @driver.find_element(:id,'login_submit')
  end
  def logout_link()
    @driver.find_element(:link_text,'Logout')
  end
  def close_browser()
    @driver.quit
  end
end

Write following code in the login_test.rb class. 

require_relative 'site_element.rb'
    #define new browser
browser=SiteElement.new("http://demo.mahara.org")

    #input user name
browser.login_username.send_keys('Student1')

    #input password
browser.login_password.send_keys('Testing1')

    #click on submit button
browser.submit_button.click

    #wait until the Logout link displays, timeout in 10 seconds
wait = Selenium::WebDriver::Wait.new(:timeout => 10) # seconds
wait.until {browser.logout_link }

    #if logout link is displayed
isLogoutLinkDisplayed=browser.logout_link.displayed?

puts isLogoutLinkDisplayed
browser.close_browser

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
true

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.