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());
}
}

9 Important Tips for Selenium Remote Control (Java client) - Test Tool

Selenium Remote Control (RC) is a test tool that allows you to write automated web application UI tests in many programming languages against any HTTP website using any mainstream JavaScript-enabled browser. Selenium RC is a powerful and simple framework for running (scheduled or manually) automated UI centric regression tests for web applications / services. Here are few simple tips for Selenium RC java client. I used JUnit for tests. You can use NGUnit too.

1. To run the Java client you need to only have selenium-java-client-driver.jar (located in selenium-remote-control-1.0-beta-1/selenium-java-client-driver-1.0-beta-1 ) in your classpath.

2. The automatically generated java file (from Selenium Core) is likely to be defective. Ensure at least you have the following import statements:

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

3. Ensure that the saved file name matches the generated class file name.

4. Remove the package statement or replace it with your own package statement. Initially just remove it.

5. Error: java.lang.UnsupportedOperationException: Catch body broken: IOException from cmd=setContext&1=SeleniumSTSanityTest.testSimpleThoughts -> java.net.ConnectException: Connection refused

Have you run the Selenium server?
Run the Selenium server like this:
java -jar selenium-server.jar

The selenium server is located in:
selenium-remote-control-1.0-beta-1/selenium-server-1.0-beta-1

6. Error: com.thoughtworks.selenium.SeleniumException: ERROR Server Exception: sessionId should not be null; has this session been started yet?

Ensure that the browser is in the PATH before running the server. On my linux box I did:
export PATH=$PATH:/usr/lib/firefox-2.0.0.6/

7. Error: com.thoughtworks.selenium.SeleniumException: Permission denied to get property Location.href

This happens on Firefox when a previous page wasn't fully loaded before the next page was invoked (due to timeout or click() was used). The solution is to use *chrome instead of *firefox in setup. I use for firefox:
setUp("http://blog.taragana.com/", "*chrome");

8. Timeout error
Increase the time in selenium.waitForPageToLoad() to 60000 (1 minute) or more.

9. How to run the generated java Test file?
This is really a JUnit question but in short you can add the following code in the generated file to get it running:

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

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

Note: Replace SeleniumSTSanityTest with the name of your Java source file (without the .java extension)

Thursday, December 17, 2009

Client Libraries in Selenium

The client libraries provide the programming support that allows you to run Selenium commands from a program of your own design. There is a different client library for each supported language. 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 reporting it as a success or failure, or possibly taking corrective action if it was an unexpected error.

So to create a test program, you simply write a program that runs a set of Selenium commands using a client library API. And, optionally, if you already have a Selenese test script created in the Selenium-IDE, you can generate the Selenium-RC code. The Selenium-IDE can translate (using its Export menu item) its Selenium commands into a client-driver’s API function calls. See the Selenium-IDE chapter for specifics on exporting RC code from Selenium-IDE.

Imp Question--- "The RC server also bundles Selenium Core, and automatically loads it into the browser."
Selenium Core (Core) is a DHTML test execution framework.

It is the engine of both, Selenium IDE and Selenium RC (driven mode), but it also can be deployed on the desired application server.

Selenium Interview Questions - Part 2

1. Is it possible to connect to db using Selenium, how you are doing and what is
the command you are using
2. looping/entering multiple data is possible in IDE/RC
3. The process followed to test in Selenium RC
4. Difference between IDE and RC?
5. What is selenium Core. How can we use that.
6. which flavor of selenium you are using
7. Diff between IDE,RC., and flavours of Selenium
8. Do you know about Selenium core?
9. Is selenium core pre loaded in RC?
10. What type of frame work you are using?
11. How u will prepare test report based on the selenium
12. How you will run test suite with java?
13. How you use selenium to prepare test the application?
14. Do you have heard about the Default selenium class?
15. What is the test engine you use to test the RC?