Adding Some Spice to Your Tests
Now we’ll get to the whole reason for using Selenium-RC, adding programming logic to your tests. It’s the same as for any program. Program flow is controlled using condition statements and iteration. In addition you can report progress information using I/O. In this section we’ll show some examples of how programming language constructs can be combined with Selenium to solve common testing problems.
You will find as you transition from the simple tests of the existence of page elements to tests of dynamic functionality involving multiple web-pages and varying data that you will require programming logic for verifying expected results. Basically, the Selenium-IDE does not support iteration and standard condition statements. You can do some conditions by embedding javascript in Selenese parameters, however iteration is impossible, and most conditions will be much easier in a programming language. In addition, you may need exception-handling for error recovery. For these reasons and others, we have written this section to illustrate the use of common programming techniques to give you greater ‘verification power’ in your automated testing.
The examples in this section are written in Java, although the code is simple and can be easily adapted to the other supported languages. If you have some basic knowledge of an object-oriented programming language you shouldn’t have difficulty understanding this section.
The examples in this section are written in Java, although the code is simple and can be easily adapted to the other supported languages. If you have some basic knowledge of an object-oriented programming language you shouldn’t have difficulty understanding this section.
=> Iteration
Iteration is one of the most common things people need to do in their tests. For example, you may want to to execute a search multiple times. Or, perhaps for verifying your test results you need to process a “result set” returned from a database.
Using the same Google search example we used earlier, let’s check the Selenium the search results. This test could use the Selenese:
Using the same Google search example we used earlier, let’s check the Selenium the search results. This test could use the Selenese:
The code has been repeated to run the same steps 3 times. But multiple copies of the same code is not good program practice because it’s more work to maintain. By using a programming language, we can iterate over the search results for a more flexible and maintainable solution.
In C#:
// Collection of String values.
String[] arr = { "ide" , "rc" , "grid" };
// Execute loop for each String in array ’arr’.
foreach (String s in arr) {
sel.open( "/" );
sel.type( "q" , "selenium " +s);
sel.click( "btnG" );
sel.waitForPageToLoad( "30000" );
assertTrue( "Expected text: " +s+ " is missing on page."
, sel.isTextPresent( "Results * for selenium " + s));
}
In C#:
// Collection of String values.
String[] arr = { "ide" , "rc" , "grid" };
// Execute loop for each String in array ’arr’.
foreach (String s in arr) {
sel.open( "/" );
sel.type( "q" , "selenium " +s);
sel.click( "btnG" );
sel.waitForPageToLoad( "30000" );
assertTrue( "Expected text: " +s+ " is missing on page."
, sel.isTextPresent( "Results * for selenium " + s));
}
=> Condition Statements
To illustrate using conditions in tests we’ll start with an example. A common problem encountered while running Selenium tests occurs when an expected element is not available on page. For example, when running the following line:
selenium.type( "q" , "selenium " +s);
If element ‘q’ is not on the page then an exception is thrown:
com.thoughtworks.selenium.SeleniumException: ERROR: Element q not found
This can cause your test to abort. For some tests that’s what you want. But often that is not desireable as your test script has many other subsequent tests to perform.
A better approach is to first validate if the element is really present and then take alternatives when it it is not. Let’s look at this using Java.
// If element is available on page then perform type operation.
if(selenium.isElementPresent( "q" )) {
selenium.type( "q" , "Selenium rc" );
} else {
System.out.printf( "Element: " +q+ " is not available on page." )
}
The aadvantage of this approach is to continue with test execution even if some UI elements are not available on page.
selenium.type( "q" , "selenium " +s);
If element ‘q’ is not on the page then an exception is thrown:
com.thoughtworks.selenium.SeleniumException: ERROR: Element q not found
This can cause your test to abort. For some tests that’s what you want. But often that is not desireable as your test script has many other subsequent tests to perform.
A better approach is to first validate if the element is really present and then take alternatives when it it is not. Let’s look at this using Java.
// If element is available on page then perform type operation.
if(selenium.isElementPresent( "q" )) {
selenium.type( "q" , "Selenium rc" );
} else {
System.out.printf( "Element: " +q+ " is not available on page." )
}
The aadvantage of this approach is to continue with test execution even if some UI elements are not available on page.
=> Executing Javascript from Your Test
Javascript comes very handy in exercising application which is not directly supported by selenium. getEval method of selenium API can be used to execute java script from selenium RC.
Consider an application having check boxes with no static identifiers. In this case one could evaluate js from selenium RC to get ids of all check boxes and then exercise them.
public static String[] getAllCheckboxIds () {
String script = "var inputId = new Array();" ;// Create array in java script.
script += "var cnt = 0;" ; // Counter for check box ids.
script += "var inputFields = new Array();" ; // Create array in java script.
script += "inputFields = window.document.getElementsByTagName(’input’);" ; script += "for(var i=0; i<inputFields.length; i++) {" ; // Loop through the script += "if(inputFields[i].id !=null " +
"&& inputFields[i].id !=’undefined’ " +
"&& inputFields[i].getAttribute(’type’) == ’checkbox’) {" ; // If input field script += "inputId[cnt]=inputFields[i].id ;" + // Save check box id to inputId "cnt++;" + // increment the counter.
"}" + // end of if.
"}" ; // end of for.
script += "inputId.toString();" ;// Convert array in to string.
String[] checkboxIds = selenium.getEval(script).split( "," ); // Split the string.
return checkboxIds;
}
To count number of images on a page:
selenium.getEval( "window.document.images.length;" );
Remember to use window object in case of dom expressions as by default selenium window is referred and not the test window.
Consider an application having check boxes with no static identifiers. In this case one could evaluate js from selenium RC to get ids of all check boxes and then exercise them.
public static String[] getAllCheckboxIds () {
String script = "var inputId = new Array();" ;// Create array in java script.
script += "var cnt = 0;" ; // Counter for check box ids.
script += "var inputFields = new Array();" ; // Create array in java script.
script += "inputFields = window.document.getElementsByTagName(’input’);" ; script += "for(var i=0; i<inputFields.length; i++) {" ; // Loop through the script += "if(inputFields[i].id !=null " +
"&& inputFields[i].id !=’undefined’ " +
"&& inputFields[i].getAttribute(’type’) == ’checkbox’) {" ; // If input field script += "inputId[cnt]=inputFields[i].id ;" + // Save check box id to inputId "cnt++;" + // increment the counter.
"}" + // end of if.
"}" ; // end of for.
script += "inputId.toString();" ;// Convert array in to string.
String[] checkboxIds = selenium.getEval(script).split( "," ); // Split the string.
return checkboxIds;
}
To count number of images on a page:
selenium.getEval( "window.document.images.length;" );
Remember to use window object in case of dom expressions as by default selenium window is referred and not the test window.

No comments:
Post a Comment