Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

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.