Five-Minute Programming Guide

The test framework (Selenium-WebDriver or Watir) is using Ruby programming language, which provides powerful scripting and expressive syntax. If you know Ruby, Congratulations, you can skip this. If you don't know Ruby, Congratulations!!, as you are going to learn a fast growing popular programming language, and it is not hard to learn, instead it is fun to learn and use.

We here list concepts and syntax (with examples) you need to get started on writing your own test cases, and you will learn more as you go. Good Ruby programming skills certainly will help you to write more creative test scripts. But we assure you that you don't need much of that to get started, don't be scared. TestWise also helps you on the way too.

Warning: This is intended to be 100% correct or a formal Ruby tutorial (and here is one by Chris Pine). Its purpose is for non-programmers to get some programming concepts

  • Comments

    Comments are used to document the scripts and are ignored during test execution. In ruby script, Comments start with "#".

    In the following example, The first line is a comment, while the second is not.

    #James, the following line return error, but I can't see why. Please help.
    expert(driver.page_source).to include("Functional testing is fun")

    Here is an example of appending a comment at the end of test step, and this step will be executed.

    driver.find_element(:id, 'loginBtn1').click #two buttons with same caption 'Login', use ID instead.
  • String

    When we enter some text in text box on a web page, these text are treated as Strings, represent in script as quoted, such as "TestWise IDE", "399.00".

    "James" + " Bond" # => "James Bond"
    1 + 2 # => 3
    "1" + "2" # => "12" as it is string adding
  • Scope

    Any programming language has certain structures (so that it can deliver intended instruction to computers). A scope can be see a container of scripts, the variable defined in the scope is only available to the scope. A scope starts with 'do' and ends with 'end'.

    Our test case definition uses scopes, so that one test case does not mix with another one. Here is an example.

    it "my test case" do
      # steps in the scope
      if true do 
        #in the inner scope
  • Variable and Assignment

    In programming, the process of storing a value in computer's memory for late use is called Assignment. The name we give to that value is called a *variable*. The syntax is as below.

    variable_name = some_value

    Here are some examples.

    app_id = driver.find_element(:id, 'appId').text # get application id from tag div
    driver.find_element(:id, 'application' + app_id) # try to click specific application

    The following scripts assign a TransferPage to variable 'transfer_page', and call page functions (see below) on it.

    transfer_page = expect_page TransferPage
    transfer_page.select_from_account 'Savings'
    trasnfer_page.enter_amount "123"
  • Compare Equal

    As you see, '=' is used for assigning value to variables. How can I check the values are the same? Using '==' for equal, '!=' for not being equal.

    expect(driver.find_element(:id, 'receipt').text).to eq('123')
    div(:is, 'receiptDate').text.should == today #today is special function created in helper
  • Logical operations

    Now we quickly move to more advanced topics. You are not very likely to use it in your first batch of test cases. But it is good concept to know. We will show some (quite readable) examples here.

    raise('page has error') if page_text.include?('Oops')
    if receipt_number > 100 then
    if page_text.include?('Success') then 
      driver.find_element(:link_text, 'Ok').click
      driver.find_element(:link_text, 'Cancel').click
  • Function

    A function (or called 'Method') is a collection of piece of scripts may be reused.

    Here is definition of function 'login_as' that takes two parameters (with one optional).

    def login_as(login, password='password')
      driver.find_element(:name, "username").send_keys(login)
      driver.find_element(:id, "pwd").send_keys(password)
      driver.find_elmment(:id, "login-btn").click

    Here are two usages of the function.

    login_as('bob', 'thebuilder') # will try to login using username 'bob', password 'thebuilder'
    login_as('john') # same as login_as('john', 'password')