From Selenese to a Program
The primary task for using Selenium RC is to convert Selenese into a programming language. In this section, we provide several different language-specific examples.
Sample Test Script of Selenium RC
Let’s start with an example Selenese test script. Imagine recording the following test with Selenium-IDE.
open | / | |
type | Q | selenium rc |
clickAndWait | btnG | |
assertTextPresent | Results * for selenium rc |
This example would work with the Google search page http://www.google.com
Selenese as Programming Code
Here is the test script exported (via Selenium-IDE) to each of the supported programming languages. If you have at least basic knowledge of an object- oriented programming language, you will understand how Selenium runs Selenese commands by reading one of these examples. To see an example in a specific language, select one of these buttons.
/** Add JUnit framework to your classpath if not already there
* for this example to work
*/
package com.example.tests;
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
public class NewTest extends SeleneseTestCase {
public void setUp() throws Exception {
setUp(“http://www.google.com/”, “*firefox”);
}
public void testNew() throws Exception {
selenium.open(“/”);
selenium.type(“q”, “selenium rc”);
selenium.click(“btnG”);
selenium.waitForPageToLoad(“30000”);
assertTrue(selenium.isTextPresent(“Results * for selenium rc”));
}
}
Programming Test of Selenium RC
Now we’ll illustrate how to program own tests using examples in each of the supported programming languages. There are essentially two tasks:
- Generate script into a programming language from Selenium-IDE, optionally modifying the result.
- Write a very simple main program that executes the generated code.
Optionally, you can adopt a test engine platform like JUnit or TestNG for Java, or NUnit for .NET if you are using one of those languages.
Here, we show language-specific examples. The language-specific APIs tend to differ from one to another, so you’ll find a separate explanation for each programming language of Java, C#, Python, Ruby , Perl and PHP
Java
For Java, people use either JUnit or TestNG as the test engine. Some development environments like Eclipse have direct support for these via plug-ins. This makes it even easier. Teaching JUnit or TestNG is beyond the scope of this document however materials may be found online and there are publications available. If you are already a “java-shop” chances are developers will already have some experience with one of these test frameworks.
You will probably want to rename the test class from “NewTest” to something of own choosing. Also, you will need to change the browser-open parameters in the statement:
selenium = new DefaultSelenium(“localhost”, 4444, “*iehta”, “http://www.google.com/”);
The Selenium-IDE generated code will look like this. This example has comments added manually for additional clarity.
package com.example.tests;
// We specify the package of our tests
import com.thoughtworks.selenium.*;
// This is the driver’s import. You’ll use this for instantiating a
// browser and making it do what you need.
import java.util.regex.Pattern;
// Selenium-IDE add the Pattern module because it’s sometimes used for
// regex validations. You can remove the module if it’s not used in your
// script.
public class NewTest extends SeleneseTestCase {
// We create our Selenium test case
public void setUp() throws Exception {
setUp(“http://www.google.com/”, “*firefox”);
// We instantiate and start the browser
}
public void testNew() throws Exception {
selenium.open(“/”);
selenium.type(“q”, “selenium rc”);
selenium.click(“btnG”);
selenium.waitForPageToLoad(“30000”);
assertTrue(selenium.isTextPresent(“Results * for selenium rc”));
// These are the real test steps
}