To Generate HTML reports using maven follow the below steps
1.Firstly we have to create a Maven Project using eclipse
2.Inorder to create a Maven Project in eclipse your eclipse should have maven plugin
3.To Download maven pulgin see below steps
4 . Open Eclipse and go to Help. Select Install New Software 5.Click on add button. Now addsite dialog gets opened.
6.Provide the Name and Location and then click on ok button as shown in below snapshot
6. Wait for few seconds until the maven plugin is displayed in the list (refer below snap shot)
7. Select the checkbox option "Maven Integration for Eclipse" and click on Next Button8. Select the plug in and click on Next Button as shown below
10.The download and installation happens in background as shown below
11.On Successful installation you would be prompted for eclipse restart. Accept the restart confirmation to restart your eclipse
12. Your eclipse is now plugged in with Maven plugin with which you can create Maven Projects
13. Ignore the console messages where eclipse tries to download maven plugins
14. Maven will create a .m2 (maven directory) folder in user directory where all the dependencies (jars are saved)
15. Go to user directory ex: C:\Documents and Settings\kiran\.m2.
16. Under .m2 directory you should see repository folder
17. Now your Eclipse is ready to create Maven Project
18. From here we will see creating Maven Projects in eclipse
19. In Eclipse navigate to File >> New Project >> Maven >> Maven Project20. Click on Next again Next and wait for a while until the Group Ids are populated
21. Select the default Group ID as shown in below fig and click on next
22. Provide groupid, artifactid and package as shown in below figure and click on Finish button
23. Now in you eclipse you should be able to see the Project in you eclipse package explorer
24. This project will have 2 src folders namely src/main/java for writing your AUT code and src/main/test for maintaining you Junit Tests
25 . Also you can see Maven Dependencies and a pom.xml
26. You Pom.xml is used to specify the required dependencies for your project (ex: selenium,junit ....)
27. By Default, Pom.xml will have a dependency of Junit 3.8.2 version. Change the version from 3.8.2 to Junit 4.0 and save the pom.xml. Now observe that you Maven dependencies will be updated to Junit 4 jar. So what ever dependencies you wish, you can provide the dependency in pom.xml. Maven will download the specified dependency from central repository and saves them in local repository C:\Documents and Settings\kiran\.m2\repository
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
28. From here we will create some Selenium Tests. So In your eclipse, under src/main/test directory, com.company package, create a Junit test class with name GoogleSearchTest.java. Delete or the default AppTest.java
package com.company;
import org.junit.AfterClass;
import org.junit.BeforeClass;
public class GoogleSearchTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
}
29. Now add some testcases to the above testClass . We will add two test methods(testcases) namely testGoogleSearch() and testGoogleAdvancedSeach(). Your test class looks like this
package com.company;
import org.junit.AfterClass;
import org.junit.BeforeClass;
public class GoogleSearchTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Test
public void testGoogleSearch(){
//To implement Selenium Tests here
}
@Test
public goid testGoogleAdvancedSearch(){
//To implement Selenium Tests here
}
}
Note** : Maven requires JDK1.5 and above . Configure eclipse with JDK 1.5 conpliance
30. Now we have to implement selenium tests in the two testmethods. For that we require Selenium RC Jars.
31. Open the pom.xml and ad the following dependency to Dependencies tag <dependencies></dependencies>
add the below dependency tag to your dependencies tag
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-control</artifactId>
<version>1.0.1</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.5</version>
<type>maven-plugin</type>
</dependency>
</dependencies>
32 . also add plug in for html report generation
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.5</version>
<configuration>
<outputName>RadaptiveTestResults</outputName>
<includes>
<include>SmokeTest.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</reporting>
33. On a whole your pom.xml should look like this (replace your pom.xml content with below xml content)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>MyMavenProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MyMavenProject</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-control</artifactId>
<version>1.0.1</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.5</version>
<type>maven-plugin</type>
</dependency>
</dependencies>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.5</version>
<configuration>
<outputName>GoogleSearchTestResults</outputName>
</configuration>
</plugin>
</plugins>
</reporting>
</project>
34 . Save the pom.xml. Now in your eclipse,under Project Explorer you should be able to see the selenium dependencies added as shown below
35. Now we have selenium dependencies and fill in the test methods with selenium implementations
36. Now we are ready with tests. How to generate HTML reports??
We will generate HTML reports from command prompt using maven commands. Before that to run maven commands from command promopt we should have maven jars.
37. Now go to the following link and download maven jars
http://www.apache.org/dyn/closer.cgi/maven/binaries/apache-maven-2.2.1-bin.zip
38.Extract the maven.zip on to you local directory
39.set Maven/bin path in system environment variables path
Open SystemProperties, click on advanced tab, edit the path and set the maven/bin in the path. click on ok button. See below
40.Maven requires JDK to execute the maven tasks. Set the JAVA_HOME USER variable
See below on how to set JAVA_HOME user variable
41. Apply the changes
42. Now to ensure that maven works, open the command prompt and type mvn -version
you should see the reponse some thing like this
Apache Maven 2.2.1 (r801777; 2009-08-07 00:46:01+0530)
Java version: 1.6.0_18
Java home: C:\Program Files\Java\jdk1.6.0_18\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows xp" version: "5.1" arch: "x86" Family: "windows"
43.Now maven setup for command prompt is ready and from the command prompt change the directory where your Maven project pom.xml is located
44. Now type the command mvn site. This will run your tests before running tests, maven will download the required maven plugin.
45.On running the above command you should be able to see the below response in the command line console
46. Go to your project target/site directory ex: E:\eclipse\myworkspace\MyMavenProject\target\site and check for the html report generated with the name GoogleSearchTestResults.html. this is the one we have specified in pom.xml
47 Open GoogleSearchTestResults.html.You should see a beautiful HTML Test Report with Pass Faill Results
Very nice and helpful article.
ReplyDeleteThanks
Shreehari