Saturday, December 19, 2009

5 Minute Guide To Selenium IDE and Selenium Remote Control (Java) Test Tools

Selenium is a high quality open source test automation tool for web application testing. Selenium runs in Internet Explorer, Mozilla and Firefox on Windows, Linux, and Macintosh, Safari on the Mac, with plans to support Safari on iPhone soon. Selenium test scripts are portable, can be run from browsers (using Selenium IDE) or from JUNit or NGUnit (using Selenium RC) etc.. For example, test scripts written using Selenium IDE in Firefox on Windows can run on Firefox in Mac or Linux, without changing any code. Selenium tests run directly in browsers and so matches the end-user experience closely.

There are three variants of Selenium, which can be used in isolation or in combination to create complete automation test suite for your web applications.

* Selenium Core - In Selenium Core the tests scripts (written in HTML) and the Selenium Test Runner (written in Javascript) are uploaded to the same web server that hosts the application you are trying to test. It is a simpler form of Selenium, and suitable for non-developers, but it has some inherent limitations.
* Selenium IDE - Selenium IDE is a Firefox plugin, which includes the entire Selenium Core, allows you to record, play back, edit, and debug tests in browser. It provides the simplest introduction to Selenium and is highly recommended for beginners. You can save the tests / test suite created in xml or html format. However to run them in an automated fashion you need Selenium Remote Control which is described next.
* Selenium Remote Control - The Selenium Remote Control allows you to develop test cases and test suites in Java (supports JUnit & NGUnit), PHP, Ruby, Python, Perl and even .NET. It is the most flexible setup but requires some development knowledge to set up and use.
* Selenium Grid - Selenium Grid allows several Selenium Remote Control servers to be accessed in parallel by Selenium Grid server. This is extremely useful for automated load and stress testing of web applications.

Today we will discuss on how you can easily create automatic test scripts using Selenium IDE and convert them to JUnit tests (which uses Selenium Remote Control) which can be added to your JUnit based automatic regression test suite.
How to create a test plan in Selenium IDE

Creating a test plan in Selenium IDE is very easy, so we will use it to create few simple tests to begin with.

* Install Selenium IDE 0.8.7, a Firefox plugin.
* After installing Selenium please restart your Firefox browser for the plugin to be activated.
* Now you should see a new added menu item named Selenium IDE under your Firefox Tools menu.
* Open / browse the site for which you want to prepare a test case.
* Start Selenium IDE from Firefox Tools->Selenium IDE.
* Browse some pages.
* Now click red button to stop recording.

At this point you will see Selenium automatically recording your actions. Carefully note the commands, target and value. You can create and insert your own commands/ modify or even delete them. We will show some examples below. In the next section we will see how we can modify the generated tests to suit our needs.
How to create / modify / delete Selenium commands

The default commands generated by Selenium when you are browsing the page as a normal user should be modified to make the test more robust and to add test cases to it.

* Let's replace all click commands by clickAndWait. click simply clicks the specified link and goes on to execute the next command without waiting. On the other hand clickAndWait waits for the new page to loaded before executing the next command. clickAndWait should be used to make more robust test cases.
* Insert assertTextNotPresent command after each clickAndWait command to confirm a text must not be present in the browsed page.
* Use assertTextPresent command to confirm a text must be present in the browsed page.
* Finally to test your test plan please click green arrow button to play from the begining or to play from start point.
* Export the test plan as java file by Selenium IDE File->Export Test As->Java - Selenium RC (for example the file name is SeleniumSTSanityTest.java)
* Then close your Firefox Selenium ID.

How to run above test plan (automatically generated java file from Selenium IDE) in command line?

* Download Selenium RC.
* Unzip it under the same directory where SeleniumSTSanityTest.java (exported test plan as java file from Selenium ID) was saved.
* Install junit.
* Go to directory where you unzip selenium-remote-control-1.0-beta-1-dist.zip file.
* Open a terminal and do the steps below-
o cd selenium-remote-control-1.0-beta-1/selenium-server-1.0-beta-1
o java -jar selenium-server.jar (to run the server in interactive mode execute java -jar selenium-server.jar -interactive)
o If you get an error like Error: com.thoughtworks.selenium.SeleniumException: ERROR Server Exception: sessionId should not be null; has this session been started yet? then ensure that the browser is in the PATH before running the server. For example, you want to run the test in Firefox. Then you should do next two steps.
o locate firefox-bin (for example it returns /usr/lib/firefox-1.5.0.12/firefox-bin)
o export PATH=$PATH:/usr/lib/firefox-1.5.0.12/firefox-bin;
Note: There is an alternative way to fix above error (browser is not in path). Simply replace chrome with browser PATH in SeleniumSTSanityTest.java file. For example:
line
setUp("http://blog.taragana.com", "*chrome");
becomes
setUp("http://blog.taragana.com", "*firefox /usr/lib/firefox-1.5.0.12/firefox-bin");
in SeleniumSTSanityTest.java.
To run the test in opera browser replace chrome with opera.

Now the selenium server is running and you have to run the Java client located in selenium-remote-control-1.0-beta-1/selenium-java-client-driver-1.0-beta-1.
* Open another terminal.
o export CLASSPATH=.:selenium-remote-control-1.0-beta-1/selenium-java-client-driver-1.0-beta-1/selenium-java-client-driver.jar:/usr/share/java/junit.jar
o javac SeleniumSTSanityTest.java
o java SeleniumSTSanityTest

The automatically generated java file SeleniumSTSanityTest.java is likely to have some defects. Fix it by comparing with the example below:

import com.thoughtworks.selenium.*;
import junit.framework.*;
import java.util.regex.Pattern;

public class SeleniumSTSanityTest extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("http://blog.taragana.com", "*chrome"); // to run the test in opera replace chrome with opera
}
public void testSimpleThoughts() throws Exception {
selenium.open("");
assertFalse(selenium.isTextPresent("WordPress database error: ["));
assertTrue(selenium.isTextPresent("2003-2008"));
selenium.open("/index.php/category/programming/java");
selenium.waitForPageToLoad("30000");
assertFalse(selenium.isTextPresent("WordPress database error: ["));
assertTrue(selenium.isTextPresent("2003-2008"));
selenium.click("//img[@alt='Übersetzen Sie zum Deutsch/German']");
selenium.waitForPageToLoad("30000");
assertFalse(selenium.isTextPresent("WordPress database error: ["));
assertTrue(selenium.isTextPresent("2003-"));
selenium.click("//img[@alt='Přeložit do Čech/Czech']");
selenium.waitForPageToLoad("60000");
assertFalse(selenium.isTextPresent("WordPress database error: ["));
assertTrue(selenium.isTextPresent("2003"));
}

public static Test suite() {
return new TestSuite(SeleniumSTSanityTest.class);
}

public static void main(String args[]) {
junit.textui.TestRunner.run(suite());
}
}

No comments:

Post a Comment