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
Error is coming here ---> import jxl.Sheet
ReplyDeleteWhich jar file need 2 be added?
jxl.jar
Deleteyou need to import jxl.jar
ReplyDeleteThis comment has been removed by the author.
DeleteDear NagaRaju,
ReplyDeleteThanks for your post. I have been searching for this since long. It worked perfectly for me. Thank you once again! Keep it up!
Hi ,
ReplyDeletei 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
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());
ReplyDeletethanks for ur post.. still im getting exceptions.
ReplyDeletesame code as u mention above i.e google example just i changed the required fields as per my settings.
Send u r code to nagaselenium@gmail.com or dnr5dnr@gmail.com
ReplyDeleteI don´t understand where is information about selenium.type("name=q",s.getCell(0,i).getContents());
ReplyDeleteI´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??
Hi Eterrer2,
ReplyDeleteDid 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
no i did't see the excel file
DeleteHi,
ReplyDeleteI 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
You can comment this line
ReplyDeleterc.setSingleWindow(true);
Hi NagaRaju,
ReplyDeleteWill jxl.jar support .ods files.
Hi Naga..
ReplyDeleteI 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.
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHi Im getting the following error:
ReplyDelete---------------
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
Hi,
ReplyDeleteHow 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
Hi
ReplyDeleteI 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
hi,
ReplyDeletei 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)
Hi
ReplyDeletethnxz for the post
i want to get the id (name=q) from the XLS sheet
can please suggest me the logic
Hi Naga,
ReplyDeleteI '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
Hi Naga,
ReplyDeleteI 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
The error got resolved by adding "selenium-embedded-rc-driver-1.2-jar-with-dependencies" to the build path.
ReplyDeleteCheers,
Kuldeep
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
ReplyDeleteHey this code is not working,i got the exception OLE stream can you provide correct code
ReplyDeleteThanks for the code, its working fine and running the scripts very accurately....
ReplyDeleteReally very helpful.Thanks Naga!!!!
ReplyDeleteCheers!!!!!!!!
Really veryhelpful. Thanks Naga!!!
ReplyDelete-Shahadat Khan
Hi I have pasted the same script and m getting these errors. Please help..!!
ReplyDeletejava.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)
It worked by added the jar selenium-server-standalone-2.25.0.jar..
ReplyDeleteThanks...very helpful script..:)
ReplyDeleteThis 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.
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...
ReplyDeleteSoftware Testing Training in chennai
Pandora Jewelry
ReplyDeleteAir Jordan Retro 9
Pandora Jewelry
Air Jordan 9
Jordan 11
Adidas Yeezy
Kyrie Irving Shoes
Red Bottom Shoes
Jordan 11
Pandora Jewelry
Rodney20180911
kyrie irving shoes
ReplyDeleteadidas yeezy
nike basketball shoes
christian louboutin outlet
balenciaga trainers
adidas yeezy
golden goose francy
adidas nmd r1
curry 6
michael kors handbags