Skip to content

Writing front end tests with the RobotFramework and Selenium

Sterling Baldwin edited this page Jun 8, 2015 · 19 revisions

The RobotFramework is a generalized testing framework that can work with many languages, and by using the Selenium2Library, allows us to exercise the front end as though a user were sitting there clicking their way through it. The setup is fairly easy, simply pip install -r requirements.txt and both the robotframework as well as the Selenium2Library will be installed.

Each test suite should exist in its own directory under tests/robot. Inside each suites directory, there should be a common_resources.txt file to specify any variables the suite will need, and the each test type will exist in its own file. Each test file can have an arbitrary amount of tests, but they should all be related to each other. For example a log in test would be something like:

test/
---login_tests/
------common_resources.txt
------valid_login.txt
------invalid_login.txt

valid_login.txt would then hold several tests of different ways to log in successfully, and invalid_login.txt would hold all the many ways to fail to log in.

The resource file has three sections, the Settings, Variables, and Keywords. Settings holds the documentation string for the test suite, and imports the selenium library. The Variables section holds any variables that will be common throughout the suite, marked as ${VARIABLE_NAME} value

The Keyword section is the largest and most complex. Here you create any common functions that you may need to do several times throughout the suite. Generally anything that takes more then one or two of the default selenium keywords should be made into its own. A good resource for the built in Selenium keywords can be found here. An example keyword would be:

This Is An Example Keyword  [Arguments]  ${url}   ${element}
   Open Browser  ${ADDRESS}  ${BROWSER}
   Go To  ${url}
   Page Should Contain Element  ${element}

For a working example, the follow is my simple test to log in and check that the dashboard page is available.

common_resources.txt

*** Settings ***

Documentation  Resources needed by the dashboard test suite.
Library        Selenium2Library


*** Variables ***

${SERVER}			localhost:8000
${DASHBOARD}		http://${SERVER}/acme/grid/
${LOGIN URL}		http://${SERVER}/acme/login?next=/acme/
${BROWSER}			ff
${VALID_USER}		testuser
${VALID_PASS}		testpass


*** Keywords ***

Open Browser And Log In
    Open Browser  ${LOGIN URL}  ${BROWSER}
    Maximize Browser Window
    Title Should Be	 login
    Input Text  username  testuser
    Input Text  password  testpass
    Click Button  Sign In
    Page Should Contain  successfully loged in

open_dashboard.txt

*** Settings ***

Documentation   A test suite for the dashboard
Resource        common_resources.txt
Suite Setup     Open Browser And Log In
Suite Teardown  Close Browser

*** Testcases ***
Test The Dashboard Is Running
  Click Element  xpath=/html/body/div[1]/div/div[2]/ul/li[3]/a
  Page Should Contain Element  xpath=/html/body/div[1]/div/div[2]/ul/li[1]/a

The tests are then actually run by going to the test/ folder and either running the command pybot test_dir

or using the run_all script. python run_tests.py someserver:PORT or python run_tests.py to run your tests against your localhost:8000

Clone this wiki locally