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


11 comments:

martoch said...

There is any way to run the test case at the same time in firefox and iexplore?

martoch said...

Hi, I've a question:

There is any way to run at the same time a test case on different browser?

Gaurang Shah said...

Yeah, you can run the test on multiple browser in parallel using Selenium grid. Check out this
http://selenium-grid.seleniumhq.org/

Anonymous said...

Gaurang, I wanted to thank you so very, very much! I have been studying Selenium RC for some time now, even through the Selenium site's documentation, but it did not really make things clear enough how to accomplish this final step - your tutorial and explanation DID!

Thank you so much for sharing your knowledge and kindness

Bryan C.

kish said...

hi gurang thanks a lot its good but i given
verifyTrue(selenium.isTextPresent("Get a started with qtp"));

but here not showing error what is the problem pls explain it

Linkesh said...

hi . i have a requirement to execute same test script parallely using junit and Grid. As junit doesnt support parallel execution. It may be achieved by ant. But how to pass two different browser parameter value[*iexplore, *firefox] from ant to same junit tes script simultaneously

Priyanka said...

Hey Gaurang..

Can you Please help me
I am new to selenium and wanna run same test case in different browser simultaneously( cross browser).
Currently I am using Eclipse+Junit+Ant but not able to perform cross browser testing...

Priyanka

yami said...

Hi Gaurang,
I was following your http://qtp-help.blogspot.com/2011/04/selenium-grid-with-junit.html#comment-form example, it really helped me to get the start with. However, i have a requirement where i have to run the same class on different browser located on different machine. How should be Grid/RC and testcase setup and how to invoke them. If you can provide any help that would be great.
Thanks

Jay Desai said...

Hey Gaurang !
I am not sure if you're still keeping a tab on your Blog, but had a quick question about Selenium.
selenium.waitForText("some text"); // Does not work

I have selenium 2.20 and using web-driver

Any help will be appreciated.

Jay

vidhi said...

Can you help me out I have one doubt regarding can we used Selenium IDE with JUnit framework or just Selenium RC can verify it.

Please reply me back.

sambasiva rao said...

SeleneseTestCase class is deprecated what is alternative way wich class i can extend.
If i am using SeleniumTestBase. I am unable to run as junit.

Post a Comment