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. 

Print

Selenium Ruby Web Driver Unit Test

Using Ruby WebDriver gem and unit test, we can develop automated unit test framework.  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

Open a Ruby Programming IDE and write the code below.

require 'test/unit'
require 'selenium-webdriver'

class MyTest < Test::Unit::TestCase

  # Called before every test method runs. Can be used
  # to set up fixture information.
  def setup

    #define driver for firefox webdriver
    @driver=Selenium::WebDriver.for :firefox
    #maximize the window
    @driver.manage.window.maximize

    #navigate to the test site
    @driver.navigate.to "http://demo.mahara.org"
  end

  def test_login
    #define username field element
    loginUserName=@driver.find_element(:id,'login_login_username')
    #input user name
    loginUserName.send_keys('Student2')

    #define password field element
    loginPassword=@driver.find_element(:id,'login_login_password')
    #input password
    loginPassword.send_keys('Testing1')

    #define submit button element
    loginSubmitButton=@driver.find_element(:id,'login_submit')
    #click on submit button
    loginSubmitButton.click

    #wait until the Logout link displays, timeout in 10 seconds
    wait = Selenium::WebDriver::Wait.new(:timeout => 10) # seconds
    wait.until {@driver.find_element(:link_text => "Logout") }

    #define logout link
    logoutLink=@driver.find_element(:link_text,'Logout')
    #if logout link is displayed
    isLogoutLinkDisplayed=logoutLink.displayed?

    puts isLogoutLinkDisplayed

    #do assertion on logout link
    assert_equal(true,isLogoutLinkDisplayed,'logout button display')

  end

  def teardown
    #quit the driver
    @driver.quit
  end


end

Execute the unit test and see the result below

Run options: --name=test_login

# Running tests:

true
.

Finished tests in 12.808733s, 0.0781 tests/s, 0.0781 assertions/s.

1 tests, 1 assertions, 0 failures, 0 errors, 0 skips

ruby -v: ruby 2.1.3p242 (2014-09-19 revision 47630) [x64-mingw32]

Process finished with exit code 0

Test Passed. You can follow the unit test example "test_login" to add other tests.

Print

Selenium Ruby Web Driver Example

Using Ruby Web Driver gem, we can develop automated tests.  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

Open a Ruby Programming IDE and write the code below.

require 'selenium-webdriver'
    #define driver for firefox webdriver
driver=Selenium::WebDriver.for :firefox
    #maximize the window
driver.manage.window.maximize

    #navigate to the test site
driver.navigate.to "http://demo.mahara.org"

    #define username field element
loginUserName=driver.find_element(:id,'login_login_username')
    #input user name
loginUserName.send_keys('Student2')

    #define password field element
loginPassword=driver.find_element(:id,'login_login_password')
    #input password
loginPassword.send_keys('Testing1')

    #define submit button element
loginSubmitButton=driver.find_element(:id,'login_submit')
    #click on submit button
loginSubmitButton.click

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

    #define logout link
logoutLink=driver.find_element(:link_text,'Logout')
    #if logout link is displayed
isLogoutLinkDisplayed=logoutLink.displayed?

puts isLogoutLinkDisplayed

    #quit the driver
driver.quit

Execute the unit test and see the result below

true

Process finished with exit code 0

Logout link display is true. The test passed. By following the pattern, you can add steps to run many tests sequentially without using unit test framework

Print

Install Ruby and Web Driver for Selenium Test Automation

The Ruby bindings for Selenium/WebDriver are available as the selenium-webdriver gem. To run Selenium tests with Ruby, we need to install Ruby parser (Ruby environment and executable file) and Selenium Web Driver. 

Installing Ruby

Step 1. visit the Ruby site "https://www.ruby-lang.org/en/documentation/installation/" and choose one of the installation methods on the site depending on your OS and preferences.

Step 2: verify installation with the command below.

C:\>ruby --version

If Ruby was installed correctly, you will see the version information as shown below

ruby 2.1.3p242 (2014-09-19 revision 47630) [x64-mingw32]

In this example above, I installed Ruby 64 bit on Windows. During the installation you need to make sure Ruby installer updates some environment path to include Ruby and Gem executable file.

 Installing Selenium WebDriver for Ruby

Assuming that you have already installed Ruby on your machine. Let's now install the Ruby WebDriver gem file.
Open command prompt on your operating system and execute the command below:

gem install selenium-webdriver

Ruby and Selenium web driver is now ready for developing test automation.