Maven project needs to be created
Use test ng [for test cases] and selenium [for automation]
Jenkins needs to configured and browse test cases there
Maven Project needs to be created. pom.xml should be proper.
Testing classes should be written with testing.xml
Jenkins should be installed and integrate with maven
Maven Project needs to created pom.xml should be proper
Go to “pom.xml”
Add dependencies for required task.
Let’s say we have excel sheet operation then we need to go for jxl dependency.
In our case it is selenium jars.
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.45.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
<scope>test</scope>
</dependency>
</dependencies>
Plugin needs to be added for testng integration.
Add path of “testng.xml” in “pom.xml” under “surefire” plugin
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins>/groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>;2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<inherited>true</inherited>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
After adding above we can observe below.
Complete pom.xml will be look like as below.
<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>JbkSampleTesting</groupId>
<artifactId>JbkSampleTesting</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<inherited>true</inherited>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.45.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Observe the below folder structure:
Testng classes should be written with “testng.xml”.
Create test-ng test cases for any website, in our example we have taken ‘www.javabykiran.com’.
Right-click on test folder and create testng class as shown below:
Open “LoginTestCase.java”.
Write “test cases” code as shown below:
package com.jbk.testcases;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class LoginTestCase {
private WebDriver driver;
@Test(priority=0)
public void testJbkTitle() {
driver.get(“http://www.javabykiran.com”);
String title = driver.getTitle();
System.out.println(title);
Assert.assertTrue(title.equals(“Best Industrial Java Classes |
Selenium Training in Pune”));
}
@Test(priority=1)
public void testJbkTitleInvalid() {
driver.get(“http://www.javabykiran.com”);
String title = driver.getTitle();
System.out.println(title);
Assert.assertTrue(title.equals(“Best Industrial Java Classes |
Selenium Training in Pune invalid”));
}
@BeforeTest
publicvoid beforeTest() {
driver = new FirefoxDriver();
}
@AfterTest
publicvoid afterTest() {
//driver.quit();
}
}
Run and test if it is running without maven integration
You will see the below shown output:
Output:
[TestNG] Running:
[INFO] Scanning for projects... [INFO]
[INFO] ------------------------------------------------------
[INFO] Building JbkSampleTesting 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ JbkSampleTesting ---
[debug] execute contextualize
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource [INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ JbkSampleTesting ---
[INFO] Nothing to compile - all classes are up to date [INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ JbkSampleTesting ---
[debug] execute contextualize
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource [INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-test-Compile) @ JbkSampleTesting ---
[INFO] Nothing to compile - all classes are up to date [INFO]
[INFO] --- maven-surefire-plugin:2.12:test (default-test) @ JbkSampleTesting ---
[INFO] Surefire report directory: E:\batch\selNote\JbkSampleTesting\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
Best Industrial Java Classes | Selenium Training in Pune Best Industrial Java Classes | Selenium Training in Pune
Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed:
17.358 sec <<< FAILURE!
Run and test if it is running with maven integration
You will see the below shown output:
Output:
[INFO] Scanning for projects... [INFO]
[INFO] ------------------------------------------------------
[INFO] Building JbkSampleTesting 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ JbkSampleTesting ---
[debug] execute contextualize
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource [INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ JbkSampleTesting ---
[INFO] Nothing to compile - all classes are up to date [INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ JbkSampleTesting ---
[debug] execute contextualize
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource [INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-test- Compile) @ JbkSampleTesting ---
[INFO] Nothing to compile - all classes are up to date [INFO]
[INFO] --- maven-surefire-plugin:2.12:test (default-test) @ Jbk- SampleTesting ---
[INFO] Surefire report directory: E:\batch\selNote\JbkSampleTesting\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
Best Industrial Java Classes | Selenium Training in Pune
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed:
23.195 sec
Jenkins should be installed and integrated with maven.
Jenkins should be installed and opened in browser as per proper url.
Start jenkins from command prompt then open in browser.
Create new item
Observe tabs after creating project.
Save all this configuration by clicking ‘save’.
After clicking build now you will see the below screen:
By clicking on this build now we will get to see what is happening inside this build in console.
After this you will see the following results:
How to see reports in Jenkins and why this is required?
While executing our tests on Jenkins many times test fails on particular build. Now if we want to check when a particular test-case, test-class or a test-package has failed we may need to go to each and every build report and check for the result.
This process is very cumbersome and may take a lot of time to get the report. So this plugin solves the above issue by showing the build result history of test-class, test-class and test-package in a tabular tree format. The plugin can be used for enabling the “Publish junit results” or “Publish TestNG results”(in case of TestNG) feature of Jenkins.
It allows users to filter the results based on passed, failed and skipped status.
There are many plugins available of which one is explained below.
Once installed you can just click on the “Test Results Analyzer” link on the left hand side of your job, as shown in the following image:
Explore options on same page you will see many features.