Monday 2 September 2013

Selenium Commands

Logging the Selenese Commands
·         Logging Selenium can be used to generate a report of all the Selenese commands in your test along with the success or failure of each. Logging Selenium extends the Java client driver to add this Selenese logging ability. Please refer to Logging selenium.
Test Reports for Python
·         When using Python Client Driver then HTMLTestRunner can be used to generate a Test Report. See HTML TestRunner.
Test Reports for Ruby
·         If RSpec framework is used for writing Selenium Test Cases in Ruby then its HTML report can be used to generate a test report. Refer to Rspec sport for more.
Note
If you are interested in a language independent log of what’s going on, take a look at selenium server logging.
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 C# and 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 sameGoogle search engine we used earlier, let’s check the Selenium search results. This test could use the Selenese:
open
/

type
q
selenium rc
clickAndWait
btnG

assertTextPresent
Results * for selenium rc

type
q
selenium ide
clickAndWait
btnG

assertTextPresent
Results * for selenium ide

type
q
selenium grid
clickAndWait
btnG

assertTextPresent
Results * for selenium grid

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));
 }
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 desirable as your test script has many other subsequent tests to perform.
A better approach is to first validate whether 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 advantage 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 an application which is not directly supported by selenium. The getEval method of selenium API can be used to execute JavaScript from selenium RC.
Consider an application having check boxes with no static identifiers. In this case one could evaluate JavaScript 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');"; // Collect input elements.
             script += "for(var i=0; i<inputFields.length; i++) {"; // Loop through the collected elements.
             script += "if(inputFields[i].id !=null " +
             "&& inputFields[i].id !='undefined' " +
             "&& inputFields[i].getAttribute('type') == 'checkbox') {"; // If input field is of type check box and input id is not null.
             script += "inputId[cnt]=inputFields[i].id ;" + // Save check box id to inputId array.
             "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 to, not the test window.

No comments:

Post a Comment