Execution of Cucumber test using TestNg


Execution of Cucumber test using TestNg 

In one of my previous post I discussed how we can use Cucumber to execute the specific features with Selenium WebDriver. In this blog post I will discuss how we can call a Cucumber TestRunner class within a testng file and then run that TestNG test with Jenkins. Lets take the test runner class, if we use Cucumber to execute the test, what we do is we run the test from this class.


package Runner;

import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;



//import org.testng.annotations.*;

@CucumberOptions(
   
        glue = {"stepdefs"},//Your step definitions package.
        features = {"/home/shavantha/eclipse-workspace/insurance/src/main/java/features/"})

public class TestRunner extends AbstractTestNGCucumberTests {

   
}


However, if we want to run the test using TestNG, we need to first update the pom.xml with the relevant dependencies and plugins as shown below.

<dependency>
   <groupId>org.testng</groupId>
   <artifactId>testng</artifactId>
   <version>6.9.10</version>
</dependency>
</dependencies>
<properties>
    <suiteXmlFile>TestNG.xml</suiteXmlFile>
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
  </properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<compilerVersion>1.8</compilerVersion>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>


Once the pom.xml is updated we create our TestNg.xml  and then we need to call the package name and the TestRunner class name as below.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
 <test thread-count="5" name="Test">
 <classes>
     <class name="Runner.TestRunner"/>
   </classes>
 </test> <!-- Test -->
</suite> <!-- Suite -->


Our final step will be to create a new project on Jenkins and call the location of the pom.xml file and specify the goal as "test". Upon executing "Build Now" we would be able to see the Selenium Webdriver being executed.










The console output for our build would have an output as similar to below

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
Starting ChromeDriver 2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706) on port 26921
Only local connections are allowed.
Sep 02, 2018 2:15:42 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Sep 02, 2018 2:15:57 PM com.relevantcodes.extentreports.ExtentReports loadConfig
WARNING: Unable to perform report configuration. The file /home/shavantha/eclipse-workspace/insurancetest-output/extent-config.xml was not found.
Starting ChromeDriver 2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706) on port 26442
Only local connections are allowed.
Sep 02, 2018 2:16:02 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Sep 02, 2018 2:16:09 PM com.relevantcodes.extentreports.ExtentReports loadConfig
WARNING: Unable to perform report configuration. The file /home/shavantha/eclipse-workspace/insurancetest-output/extent-config.xml was not found.
Starting ChromeDriver 2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706) on port 5017
Only local connections are allowed.
Sep 02, 2018 2:16:15 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Sep 02, 2018 2:16:26 PM com.relevantcodes.extentreports.ExtentReports loadConfig
WARNING: Unable to perform report configuration. The file /home/shavantha/eclipse-workspace/insurancetest-output/extent-config.xml was not found.
Starting ChromeDriver 2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706) on port 16667
Only local connections are allowed.
Sep 02, 2018 2:16:28 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Sep 02, 2018 2:16:37 PM com.relevantcodes.extentreports.ExtentReports loadConfig
WARNING: Unable to perform report configuration. The file /home/shavantha/eclipse-workspace/insurancetest-output/extent-config.xml was not found.

4 Scenarios ( [32m4 passed [0m)
12 Steps ( [32m12 passed [0m)
1m0.027s

Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 60.5 sec - in TestSuite

Results :

Tests run: 4, Failures: 0, Errors: 0, Skipped: 0

[JENKINS] Recording test results
[WARNING] Attempt to (de-)serialize anonymous class hudson.maven.reporters.SurefireArchiver$2; see: https://jenkins.io/redirect/serialization-of-anonymous-classes/
[WARNING] Attempt to (de-)serialize anonymous class hudson.maven.reporters.MavenArtifactArchiver$2; see: https://jenkins.io/redirect/serialization-of-anonymous-classes/
[WARNING] Attempt to (de-)serialize anonymous class hudson.maven.reporters.MavenFingerprinter$1; see: https://jenkins.io/redirect/serialization-of-anonymous-classes/
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:07 min
[INFO] Finished at: 2018-09-02T14:16:43+05:30
[INFO] ------------------------------------------------------------------------
Waiting for Jenkins to finish collecting data
[JENKINS] Archiving /home/shavantha/eclipse-workspace/insurance/pom.xml to bdd/test/0.0.1-SNAPSHOT/test-0.0.1-SNAPSHOT.pom
/home/shavantha/eclipse-workspace/insurance/pom.xml is not inside /home/shavantha/.jenkins/workspace/InsuranceProject/home/shavantha/eclipse-workspace/insurance/; will archive in a separate pass
channel stopped
Finished: SUCCESS


Comments