[Android General] Ravi H Basawa:
6. DataDriven Testing using POI jar files and TestNG XSLT + ANT

6. DataDriven Testing using POI jar files​

1.We go for data driven testing when we have some modules that need to be tested for multiple values. For example in a application which has a login page with username and password field we have to test these edit text boxes for multiple inputs, It can be a number or it also can be a string or both together.

2. Here I will take Google.com as example and show you how to extract data from a excel file and push the same to Google website.

3. First we will create an excel file with some data present in it to test our application. (An Excel file for testing our app is attached here)

4. To extract data from Excel we use “FileInputStream” Class by which we can create/delete and modify a file.

5. Now add POI.jar files to Java Build Path same as how we added Selenium Jar file.

6. After adding of POI jar files to Java Build Path you will see them added as shown below:

7. After this is done we will create a new java class and will give the name as "DataDriveTest" and extend it from "TestWebDriver" Class.

8. Now what we will do is we will open google.com enter "search" value as "RHBROMS" ( 1st value from the excel sheet ) click on the first link and then we will click on back button of the browser and then we will clear the search box and enter "xda developers" ( 2nd value from the excel sheet ) and click on first link that will be xda developers website.

9. The code for this is as written below with explanation.

10. Now to execute this we have to do some editing at Main.java file as:

public class Main extends DataDriveTest {
public static void main(String args[]) throws FileNotFoundException, InvalidFormatException, IOException, Exception{

setup();
test();

}

11. And also in TestNGSuit1.java file as:

@Test
public void testngtest() throws FileNotFoundException, InvalidFormatException, IOException, Exception {

Main m = new Main();
m.setup();
m.test();
}

12. Now as usual convert the TestNGSuit1 file to TestNG and execute. :victory:

13. To see your TestNG results go to your eclipse workspace and open selenium project inside that you will find test-output folder. As shown in below Screen Shot:

14. For Generation of Graphical and more clean Results we use TestNG xslt with ANT. lets see how to do it in Next Chapter :laugh::laugh:

6. TestNG xslt with ANT ​

1. Download Ant from here: http://ant.apache.org/

2. Unzip it and rename the folder as ant.

3. Set ANT_HOME to environmental variables.( In windows 7 Right click on Computer -> properties -> “Advance system setting”

-> Choose Advanced Tab

-> Press Environment Variables Button

-> In the System Variables, click New Button

Give the Variable Name:ANT_HOME

Give the Value: E:\ant

Click OK )

as shown in Below Screen Shot.

4. Set ANT_HOME path,

go to path

Give the Value C:\ANT\bin

Click OK

5. To check ANT works properly or not

In the command prompt, type:

ant -version

you will see as below Screen Shot:

6. Now download testng-xslt from HERE

7. After this extract the downloaded zip file and go to testNG-xslt -> src -> main -> resources and copy the file testng-results.xsl and also copy this file from testNG-xslt -> lib that is saxon-8.7.jar and keep them at any folder for time being.

8. Now go to your project workspace and goto SeleniumTest -> test-output and paste testing-results.xsl that you copied.

9. and now goto eclipse and add saxon-8.7.jar to buildpath.

NOTE: the thing is you have to keep all your jar files in a same folder as I have kept at jar folder as shown below in my setup:

10. Now after doing all this create new xml file and call it as Build.xml

11. After creating this just copy paste the below code:

<?xml version="1.0" encoding="UTF-8"?>
<project name="SeleniumTest" default="compile" basedir=".">

<path id="cp">
<pathelement path="bin" />
<fileset dir="jars"/>
</path>

<!-- for compiling -->
<target name="compile">
<javac classpathref="cp" srcdir="src" destdir="bin"/>
</target>

<!-- for running -->
<target name="run" depends="compile">
<java classpathref="cp" classname="org.testng.TestNG" args="testng.xml"/>
</target>

<!-- for report generation -->
<target name="report" depends="run">
<xslt in="./test-output/testng-results.xml" style="./test-output/testng-results.xsl" out="./test-output/testng-xslt.html">
<param expression="${basedir}/test-output/" name="testNgXslt.outputDir" />
<classpath refid="cp"/>
</xslt>
</target>
</project>

12. After this save the file.

13. Now right click on the project and do as below and select TestNG:

14. Now Run the Build.xml file. and your results will be stored at Index.html at test-output folder.

To be continued .. NEXT will be Maven with TestNG!! :D:D