Wednesday, April 29, 2009

Selenium with JUnit.

At this point I am sure that you know how to install/setup selenium and JUnit. And how to record the test cases using selenium IDE. If you don't please refer Selenium

Till now only Selenium IDE was in picture. But now you will need all the things, Selenium RC, Java, Junit. So let's go step by step.


Export the test case to JAVA
Export the test cases in java language and save at your desired location.

Edit the test case.
Now as you have saved the test cases in JAVA, to make it run you need to the following changes.
  • Remove the first line starting with package or comment it with double slash ( // ).

  • Make sure the following lines are at top of the file.
    import com.thoughtworks.selenium.*;
    import java.util.regex.Pattern;
    import junit.framework.*;
  • Make sure the class name is same as your file name
  • You need to add three new extra methods in the code as follows

    public static Test suite() {
    return new TestSuite(GoogleSearch.class);
    }
    public void tearDown(){
    selenium.stop();
    }
    public static void main(String args[]) {
    junit.textui.TestRunner.run(suite());
    }
    Below is the code you will get when you will export the test case from selenium
    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/", "*chrome");
    }
    public void testNew() throws Exception {
    selenium.open("/");
    selenium.type("q", "selenium IDE");
    selenium.click("btnG");
    selenium.waitForPageToLoad("30000");
    verifyTrue(selenium.isTextPresent("selenium-ide.seleniumhq.org"));
    selenium.click("//ol[@id='rso']/li[1]/h3/a/em");
    selenium.waitForPageToLoad("30000");
    }
    }
    And the following is the modified code ready to run with Junit
    //package com.example.tests;

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

    public class GoogleSearch extends SeleneseTestCase {
    public void setUp() throws Exception {
    setUp("http://Google.com", "*chrome");

    }
    public void testNew() throws Exception {
    selenium.open("/");
    selenium.type("q", "selenium IDE");
    selenium.click("btnG");
    selenium.waitForPageToLoad("30000");
    verifyTrue(selenium.isTextPresent("selenium-ide.seleniumhq.org"));
    //selenium.click("//ol[@id='rso']/li[1]/h3/a/em");
    selenium.click("link=Selenium IDE");
    selenium.waitForPageToLoad("30000");
    }

    public static Test suite() {
    //method added
    return new TestSuite(GoogleSearch.class);
    }
    public void tearDown(){
    //Added . Will be called when the test will complete
    selenium.stop();
    }
    public static void main(String args[]) {
    // Added. Execution will started from here.
    junit.textui.TestRunner.run(suite());
    }

    }
  • Save the file bug before you compile it make sure that classpath for both selenium and Junit is set else you will end up with compile time errors.


Set Classpath

You need to set the classpath for both selenium-java-client-driver.jar and junit-4.6.jar(file name may differ depends on the version you have downloaded).
To set the classpath just do the following
  • right click on the My Computer icon on desktop and click on properties then click on Advance Tab and then click on Environment Variables.

  • Under the System Variables click on CLASSPATH and add the full path of both the above file separated by semicolon(;).



Run the TestCase
Javac Filename.java command.
If everything goes fine your file will compile successfully without any error.
Now before you run the test cases you need to start the Selenium server. To start the server go to the command prompt and run the following command

java -jar path\selenium-server-0.9.2\selenium-server.jar -interactive

Now to run your test case open new command prompt and type the following command

java filename

Now the test case will be executed in the firefox browser.
You can also execute the same test on different browser too.
To execute it on the Internet Explorer just change the line setUp("http://Google.com", "*chrome"); from setUp() method to setUp("http://Google.com", "*iexplore"); to run it on the opera change the line to
setUp("http://Google.com", "*opera");


Troubleshooting
  1. Getting the following error while compiling the test case file.
    'javac' is not recognized as an internal or external command,operable program or batch file.
    Solution: locate the bin folder inside you jdk folder and set it's path in path environment varibale

  2. Getting following error while running the test case
    java.lang.RuntimeException: Could not contact Selenium Server; have you started it?
    Solution: This means that you haven't started the selenium server. So locate the "selenium-server.jar" file and use the following command to start the server

    java -jar path\selenium-server.jar -interactive


  3. getting following error while test case is in execution.
    com.thoughtworks.selenium.SeleniumException: Permission denied to get property Location.href
    Solution:use *chrome instead of *firefox in setup


Tuesday, April 28, 2009

Selenium


Selenium Introduction.

Selenium is a open source ( Free !!! ) tool for function/regression, unit testing the web application. It has a IDE which is actually a firefox add-ons which lets you record/playback the events and it also lets you export your test cases into the other desired ( Java, php, .net, ruby ) language for further editing.

It has three main component provided by selenium and other optional provided by third party.
  1. Selenium IDE: Require to record/playback the testcase and to export it in other language
  2. Selenium Core: Central control
  3. Selenium RC: Allows you to run your test cases from any other languages on other browsers (like IE, Opera ) and on other platforms ( Mac, Windows )
  4. Junit/Nunit: Junit is framework if you choose java to write the testcases and Nunit is if you choose to write down the testcases in .Net
  5. ANT: This is the reporting tool.
The first three of the above are main components others are optional but essential.

Installation:
Now to install and setup the selenium is bit tedious task. You need to download the following things:
Now as you have downloaded the above things, you need to install/extract them to use
  • Selenium IDE will be installed automatically if you have use firefox to download it else a .xpi file would have been downloaded. Just drad and drop the file on the firefox and it will be installed.
  • Extract the Selenium Core, Selenium RC and Junit in your desired directory.
  • install the JDK
Now after this you need to set the path and classpath environment variables.
  • Find the out the "selenium-java-client-driver.jar" from the Selenium RC folder and set its full path as classpath as below
    set path=%classpath%;full path of ""selenium-java-client-driver.jar"
  • Find out the Junit-4.6jar ( file name may differ depends on the version you have downloaded) and set it full path in the classpath variable.
  • Find out the bin located inside the Java folder and set its path in the path variable.
Now you are ready to use the selenium with Java. But before we use it with Java we will see how to record and playback the testcases using selenium IDE


Record and playback using Selenium IDE:

video


Open the Firefox and open the Selenium IDE from the tool menu. Make sure the record (red button ) is pressed
  • Now you can start recording. Navigate to the Google.com and in the search box enter "Selenium IDE" or any other text. Now click on the first text present. Now go back to the IDE you will find the recorded steps in there.
  • You need to change all the "click" command with "clickandWait". and now you can playback to see whether the recorded test cases passes or not. Just click on the green arrow button and the testcase will be loaded in browser and will be executed.