Friday, April 17, 2009

Using Selenium

Recently i have worked on two open source testing tools , and i found selenium as one of the best open source web testing tool available .. here below i m listing down how we can use selenium IDE..

How to create a test plan in Selenium IDECreating a test plan in Selenium IDE is very easy, so we will use it to create few simple tests to begin with.
1. Install Selenium IDE 0.8.7, a Firefox plugin.
2. After installing Selenium please restart your Firefox browser for the plugin to be activated.
3. Now you should see a new added menu item named Selenium IDE under your Firefox Tools menu.
4. Open / browse the site for which you want to prepare a test case.
5. Start Selenium IDE from Firefox Tools->Selenium IDE.
6. Browse some pages.
7. 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.

1. 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.

2. Insert assertTextNotPresent command after each clickAndWait command to confirm a text must not be present in the browsed page.

3. Use assertTextPresent command to confirm a text must be present in the browsed page.

4. Finally to test your test plan please click green arrow button to play from the begining or to play from start point.

5. 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)

6. Then close your Firefox Selenium ID.

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

1. Download Selenium RC.

2. Unzip it under the same directory where SeleniumSTSanityTest.java (exported test plan as java file from Selenium ID) was saved.

3. Install junit.

4. Go to directory where you unzip selenium-remote-control-1.0-beta-1-dist.zip file.

5. Open a terminal and do the steps below-
a. cd selenium-remote-control-1.0-beta-1/selenium-server-1.0-beta-1
b. java -jar selenium-server.jar (to run the server in interactive mode execute java -jar selenium-server.jar -interactive)
c. 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.

d. locate firefox-bin (for example it returns /usr/lib/firefox-1.5.0.12/firefox-bin)

e. 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://floratec.blogspot.com", "*chrome");
becomes
setUp("http://floratec.blogspot.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.

6. 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.

7. Open another terminal.

a. 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
b. javac SeleniumSTSanityTest.java
c. java SeleniumSTSanityTest

8.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://floratec.blogspot.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: