2010-08-12

Selenium web application testing system

Selenium is a testing framework for Web Applications. With a Selenium test environment, you can programmatically create tests and run them as often as you need. This will ensure that you do not accidentally destroys any previously developed feature during later on development. 


Last year, I set up such a Selenium test environment at work. During the preparation for installing such an environment for a customer, I created a "have-to-read" documentation. It summarizes important issues for the use of Selenium. It is for developers who want to use a Selenium installation, to write their own tests. The information comes--largely--from the selenium-site http://seleniumhq.org/ and is represented here as a "short documentary"--the essentials at a glance with links to the source Selenium Documentation.


If you want to start with selenium, and the framework is set up already, then check this out. 

Commonly Used Selenium Commands

Selenium Commands

Selenium Server

Selenium Server receives Selenium commands from your test program, interprets them, and reports back to your program the results of running those tests.
The RC server bundles Selenium Core and automatically injects it into the browser. This occurs when your test program opens the browser (using a client library API function). Selenium-Core is a JavaScript program, actually a set of JavaScript functions which interprets and executes Selenese commands using the browser’s built-in JavaScript interpreter.
The Server receives the Selenese commands from your test program using simple HTTP GET/POST requests. This means you can use any programming language that can send HTTP requests to automate Selenium tests on the browser.

Client Libraries

A Selenium client library provides a programming interface (API), i.e., a set of functions, which run Selenium commands from your own program. Within each interface, there is a programming function that supports each Selenese command.
The client library takes a Selenese command and passes it to the Selenium Server for processing a specific action or test against the application under test (AUT). The client library also receives the result of that command and passes it back to your program. Your program can receive the result and store it into a program variable and report it as a success or failure, or possibly take corrective action if it was an unexpected error.

Learning the API

Starting the Browser

$this->setBrowser("*firefox");$this->setBrowserUrl("http://www.google.com/");
This example opens the browser and represents that browser by assigning a “browser instance” to a program variable. This program variable is then used to call methods from the browser. These methods execute the Selenium commands, i.e. like open or type or the verify commands.
parameters
  • host: Specifies the IP address of the computer where the server is located. Usually, this is the same machine as where the client is running, so in this case localhost is passed. In some clients this is an optional parameter.
  • port: Specifies the TCP/IP socket where the server is listening waiting for the client to establish a connection. This also is optional in some client drivers.
  • browser: The browser in which you want to run the tests. This is a required parameter.
  • url: The base url of the application under test. This is required by all the client libs and is integral information for starting up the browser-proxy-AUT communication.

Running Commands

Once you have the browser initialized and assigned to a variable (generally named “selenium”) you can make it run Selenese commands by calling the respective methods from the browser variable. For example, to call the type method of the selenium object:
selenium.type("field-id","string to type")
In the background the browser will actually perform a type operation, essentially identical to a user typing input into the browser, by using the locator and the string you specified during the method call.

Adding Some Spice to Your Tests



Troubleshooting Common Problems






Test Design Considerations

  • verify/assertText is the most specific test.
  • With Xpath and DOM you can locate an object with respect to another object on the page.
  • Locate dynamic ID: You need programming logic like:
String[] checkboxIds = selenium.getAllFields(); 
if (!GenericValidator.isBlankOrNull(checkboxIds[i])) {   
    if (checkboxIds[i].indexOF("addFOrm") > 1) { 
        selenium.click(checkboxIds[i]);   
    } 
} 
  • Waiting for AJAX Element:
// Loop 
initialization.for (int second = 0;; second++) {    

    // If loop is reached 60 seconds then break the loop.     
    if (second >= 60) break;     

    // Search for element "link=ajaxLink" and if available then break loop.        try { 
        if (selenium.isElementPresent("link=ajaxLink")) break; 
    } catch (Exception e) {
        // log
    } 

    // Pause for 1 second.
    Thread.sleep(1000); 
} 
This certainly isn’t the only solution. AJAX is a common topic in the user group and we suggest searching previous discussions to see what others have done along with the questions they have posted.
  • UI Mapping: What makes a UI map heplful? Its primary purpose for making test script management much easier. When a locator needs to be edited, there is a central location for easily finding that object.
public void testNew() throws Exception {      
    selenium.open("http://www.test.com");         
    selenium.type(admin.username, "xxxxxxxx");         
    selenium.click(admin.loginbutton);         
    selenium.click(admin.events.createnewevent);         
    selenium.waitForPageToLoad("30000");          
    selenium.click(admin.events.cancel);       
    selenium.waitForPageToLoad("30000");         
    selenium.click(admin.events.viewoldevents);      
    selenium.waitForPageToLoad("30000");
} 
    
admin.username = loginForm:tbUsername; 
admin.loginbutton = loginForm:btnLogin; 
admin.events.createnewevent = adminHomeForm:_activitynew; 
admin.events.cancel = addEditEventForm:_IDcancel; 
admin.events.viewoldevents = adminHomeForm:_activityold; 
  • Data Driven Testing: Refer to the RC wiki for examples of reading data from spread sheet or using data provider capabilities of TestNG with java client driver.

Kommentar veröffentlichen