October 26, 2010

How to do Data Driven Testing using JUnit

There are many ways to do Data driven tests.I used Excel sheet for reading data and FileInputStream method.

Below script explains you how to Read data from excel sheet and use the data to search google.

I am using Eclipse, selenium RC, Junit and Excel sheet for this script.


--->Create a Java project in eclipse.
--->Create a new class DatadrivenJUnit.
--->Paste the below code in Eclipse.
--->Chnage the path of excel file according to your requirement.







Please note that selenium will support only .xls format plese do not forget to change the excel file to .xls if you are using MS-office2007.

Below is the code


import java.io.FileInputStream;
import jxl.Sheet;
import jxl.Workbook;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import com.thoughtworks.selenium.*;


public class DatadrivenJUnit extends SeleneseTestCase{

public Selenium selenium;
public SeleniumServer seleniumserver;

@BeforeClass
public void setUp() throws Exception {

RemoteControlConfiguration rc = new RemoteControlConfiguration();
rc.setSingleWindow(true);
seleniumserver = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://");
seleniumserver.start();
selenium.start();

}

@Test
public void testDatadrivenJUnit() throws Exception
{
FileInputStream fi=new FileInputStream("F:\\Framework\\testdata\\search.xls");
Workbook w=Workbook.getWorkbook(fi);
Sheet s=w.getSheet(0);
selenium.open("http://www.google.com");
selenium.windowMaximize();
for (int i = 1; i < s.getRows(); i++)
 { //Read data from excel sheet
 selenium.type("name=q",s.getCell(0,i).getContents());
 selenium.click("btnG");
 Thread.sleep(1000); } }
@AfterClass
 public void tearDown() throws InterruptedException{
selenium.stop();
 seleniumserver.stop();
}

 }


 Please let me know if you need more information regarding Data driven testing....


An Introduction to Testing Web Applications with twill and Selenium

37 comments:

  1. Error is coming here ---> import jxl.Sheet
    Which jar file need 2 be added?

    ReplyDelete
  2. Replies
    1. This comment has been removed by the author.

      Delete
  3. Dear NagaRaju,
    Thanks for your post. I have been searching for this since long. It worked perfectly for me. Thank you once again! Keep it up!

    ReplyDelete
  4. Hi ,
    i try datadriven testing in junit as above but im getting error :
    com.thoughtworks.selenium.SeleniumException: ERROR: Element name=q not foundat

    plz help me ... im new to selenium

    ReplyDelete
  5. Pleas send your code to me.. that may be syncornisation problem...pls add waitforpagetoload command before selenium.type("name=q",s.getCell(0,i).getContents());

    ReplyDelete
  6. thanks for ur post.. still im getting exceptions.
    same code as u mention above i.e google example just i changed the required fields as per my settings.

    ReplyDelete
  7. Send u r code to nagaselenium@gmail.com or dnr5dnr@gmail.com

    ReplyDelete
  8. I don´t understand where is information about selenium.type("name=q",s.getCell(0,i).getContents());
    I´m trying pass string on type method and fails. After export from selenium i have that line

    selenium.type("userName", "${AccountNumber1}${AccountNumber2}${AccountNumber3}");

    Why doesn´t run??

    ReplyDelete
  9. Hi Eterrer2,

    Did you see a sample excel file in the post...

    selenium.type("name=q",s.getCell(0,i).getContents());

    name=q -- is the name of the google search box.

    s.getCell(0,i).getContents()) --- reading data from excel sheet...cell 0,1..

    Please let me know if you have any difficulty in executing that script..

    Thanks,
    Naga

    ReplyDelete
  10. Hi,
    I copy pasted the same code and i am getting 2 syntax errors

    1. The method setSingleWindow(boolean) is undefined for the type RemoteControlConfiguration

    2. The constructor SeleniumServer(RemoteControlConfiguration) is undefined

    ReplyDelete
  11. You can comment this line
    rc.setSingleWindow(true);

    ReplyDelete
  12. Hi NagaRaju,
    Will jxl.jar support .ods files.

    ReplyDelete
  13. Hi Naga..

    I have used the above example and How do I capture the seprte screen shots suppose I have 5 URL's and I want to capture the all the 5 screen shots currently its overriding other screen shots.

    Please help.

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. This comment has been removed by the author.

    ReplyDelete
  16. Hi Im getting the following error:

    ---------------
    jxl.read.biff.BiffException: Unable to recognize OLE stream
    at jxl.read.biff.CompoundFile.(CompoundFile.java:116)
    at jxl.read.biff.File.(File.java:127)
    at jxl.Workbook.getWorkbook(Workbook.java:268)
    at jxl.Workbook.getWorkbook(Workbook.java:253)
    at DatadrivenJUnit.testDatadrivenJUnit(DatadrivenJUnit.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at junit.framework.TestCase.runTest(TestCase.java:154)
    at junit.framework.TestCase.runBare(TestCase.java:127)
    at com.thoughtworks.selenium.SeleneseTestCase.runBare(SeleneseTestCase.java:212)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:208)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

    -------------------

    Can yo help me out

    ReplyDelete
  17. Hi,

    How can you write back some value that you're getting from the application, into the Excel File¡??. I've tried s.getCell(x,i).getContents().replace("", strAmt) and some other options but no success

    strAmt is the variable that contains the value that i want to write into the excel file

    ReplyDelete
  18. Hi
    I am facing one problem while its in loop it wont moves to other function and though i have used one login only then also i am not able to execute other test cases. It performs everything but not redirecting to other page

    ReplyDelete
  19. hi,
    i have been copied and paste it i am getting error at this below ...
    jxl.read.biff.BiffException: Unable to recognize OLE stream
    at jxl.read.biff.CompoundFile.(CompoundFile.java:116)
    at jxl.read.biff.File.(File.java:127)
    at jxl.Workbook.getWorkbook(Workbook.java:268)
    at jxl.Workbook.getWorkbook(Workbook.java:253)
    at DataDriven.DatadrivenJUnit.testDatadrivenJUnit(DatadrivenJUnit.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at junit.framework.TestCase.runTest(TestCase.java:168)
    at junit.framework.TestCase.runBare(TestCase.java:134)
    at com.thoughtworks.selenium.SeleneseTestCase.runBare(SeleneseTestCase.java:230)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:243)
    at junit.framework.TestSuite.run(TestSuite.java:238)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

    ReplyDelete
  20. Hi

    thnxz for the post

    i want to get the id (name=q) from the XLS sheet
    can please suggest me the logic

    ReplyDelete
  21. Hi Naga,

    I 'm also getting error on the name = q not found find below error message.. Please help me out...

    ERROR:
    com.thoughtworks.selenium.SeleniumException: ERROR: Element name=q not found

    ReplyDelete
  22. Hi Naga,

    I am getting the error import org.openqa.selenium.server.RemoteControlConfiguration cannot be resolved.

    I have added "selenium-server-coreless-1.0.1-sources " to the build path.

    Thanks in advance for you help.

    Regards,
    Kuldeep

    ReplyDelete
  23. The error got resolved by adding "selenium-embedded-rc-driver-1.2-jar-with-dependencies" to the build path.

    Cheers,
    Kuldeep

    ReplyDelete
  24. Testing is an important phase in the process of software development which involves testing of each small module of the software program for its validity and accuracy.STC Technologies

    ReplyDelete
  25. Hey this code is not working,i got the exception OLE stream can you provide correct code

    ReplyDelete
  26. Thanks for the code, its working fine and running the scripts very accurately....

    ReplyDelete
  27. Really very helpful.Thanks Naga!!!!
    Cheers!!!!!!!!

    ReplyDelete
  28. Really veryhelpful. Thanks Naga!!!
    -Shahadat Khan

    ReplyDelete
  29. Hi I have pasted the same script and m getting these errors. Please help..!!
    java.lang.NoSuchMethodError: org.openqa.selenium.remote.DesiredCapabilities.phantomjs()Lorg/openqa/selenium/remote/DesiredCapabilities;
    at org.openqa.selenium.remote.server.DefaultDriverSessions$1.(DefaultDriverSessions.java:47)
    at org.openqa.selenium.remote.server.DefaultDriverSessions.(DefaultDriverSessions.java:38)
    at org.openqa.selenium.server.SeleniumServer.assembleHandlers(SeleniumServer.java:342)
    at org.openqa.selenium.server.SeleniumServer.createJettyServer(SeleniumServer.java:302)
    at org.openqa.selenium.server.SeleniumServer.(SeleniumServer.java:275)
    at org.openqa.selenium.server.SeleniumServer.(SeleniumServer.java:251)
    at new1.newcls.setUp(newcls.java:24)
    at junit.framework.TestCase.runBare(TestCase.java:132)
    at com.thoughtworks.selenium.SeleneseTestCase.runBare(SeleneseTestCase.java:248)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:243)
    at junit.framework.TestSuite.run(TestSuite.java:238)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

    ReplyDelete
  30. It worked by added the jar selenium-server-standalone-2.25.0.jar..
    Thanks...very helpful script..:)

    ReplyDelete

  31. This is very informative blog and nice article , I really like your technique of writing a blog. More screenshots gives the clear representation.. I book marked it to my bookmark site list and will be checking back in the near future Best Selenium Training in Velachery gives you the entire stuff about all the Selenium Components.

    ReplyDelete
  32. Good and interesting article... your blog is more informative and really useful to me.. as a fresher your article is more shape me to be a testing professional...

    Software Testing Training in chennai

    ReplyDelete