This Tutorial Provides a Complete Overview of Cucumber with Jenkins and Excel sheet to Explain the Cucumber Jenkins Integration:
In this tutorial, we will be discussing Cucumber with Jenkins and Excel sheet. We hope this tutorial will be helpful to you.
=> Visit Here To See The Jenkins Training Series For All
Table of Contents:
Cucumber With Jenkins
Test Script Automation
The following configuration needs to be done.
Feature file
Feature:
As a user
I want to be able to add new clients in the system.
So that I can add accounting data for that client.
Background:
Given I am on Github home page.
When I specify Username as “xxxxxxxxxx” and Password as “xxxxxxx”.
And Click on SignIn button.
@Smoke
Scenario: Editing the profile.
Given I click on Your Profile option.
When I click on edit profile button.
And Uploaded a new picture.
Then I should be seeing new profile picture.
@Regression @Everytime
Scenario: Create new gist.
Given I click on Your Gists option.
When I provide the filename, description.
And click on Create public gist method.
Then I should be seeing the new gist.
Stepdef file
package com.Multi; import cucumber.api.PendingException; import cucumber.api.java.en.Given; import cucumber.api.java.en.Then; import cucumber.api.java.en.When; import cucumber.api.java.After; import cucumber.api.java.Before; import cucumber.api.java.en.But; import java.util.List; import cucumber.api.PendingException; import cucumber.api.java.it.Data; import cucumber.runtime.ScenarioImpl; import gherkin.formatter.model.Scenario; import gherkin.formatter.model.ScenarioOutline; import cucumber.api.DataTable; import org.junit.Assert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; import cucumber.api.cli.Main; public class Step2 { static public String sb; static public String sa; static WebDriver driver = null; @Before("@Smoke,@Regression") public void beforeScenario(){ System.out.println("New scenrio begins"); } @After("@Smoke,@Regression") public void afterScenario(){ System.out.println("Scenrio ends"); } @MyAnnotation public static void myanno() { System.out.println("my annot gets executed"); } @Given("^I am on Github home page$") public void i_am_on_Github_home_page(){ String site = "https://www.github.com/login"; System.setProperty("webdriver.chrome.driver", "Executables\\chromedriver.exe"); driver = new ChromeDriver(); driver.navigate().to(site); //driver.findElement(By.cssSelector("a[contains[@href, "login"]]").click(); // Write code here that turns the phrase above into concrete actions } @When("^I specify Username as \"([^\"]*)\" and Password as \"([^\"]*)\"$") public void i_specify_Username_as_and_Password_as(String arg1, String arg2){ driver.findElement(By.cssSelector("input#login_field")).sendKeys(arg1); driver.findElement(By.cssSelector("input#password")).sendKeys(arg2); // Write code here that turns the phrase above into concrete actions } @When("^Click on SignIn button$") public void click_on_SignIn_button(){ driver.findElement(By.cssSelector("input.btn")).click(); // Write code here that turns the phrase above into concrete actions } @Given("^I click on Your Profile option$") public void i_click_on_Your_Profile_option(){ driver.findElement(By.xpath("//*[@id='user- links']/li[3]/details/summary/img")).click(); List<WebElement> olist = driver.findElements(By.xpath("//*[@id='user- links']/li[3]/details/ul/li/a[@class='dropdown-item']")); for(WebElement o:olist) { if(o.getText().equals("Your profile")) { o.click(); break; } } // Write code here that turns the phrase above into concrete actions } @When("^I click on edit profile button$") public void i_click_on_edit_profile_button(){ driver.findElement(By.xpath("//*[@id='js-pjax- container']/div/div[2]/div[1]/a")).click(); // Write code here that turns the phrase above into concrete actions } @When("^Uploaded new picture$") public void uploaded_new_picture() throws InterruptedException{ WebElement s1 = driver.findElement(By.xpath("//*[@class='avatar-upload- container clearfix']/Img")); sb=s1.getAttribute("src"); System.out.println(s1.getAttribute("src")); driver.findElement(By.id("upload-profile- picture")).sendKeys("D://cucumberFinal//multiple//Files//images.jpg"); Thread.sleep(10000); String wh = driver.getWindowHandle(); driver.switchTo().window(wh); Actions actions = new Actions(driver); WebElement element = driver.findElement(By.xpath("//div[@class='facebox -content']/form/div[3]/button")); Thread.sleep(10000); actions.moveToElement(element); //Thread.sleep(10000); actions.click(); //actions.sendKeys("GIST1 Description"); actions.build().perform(); // driver.findElement(By.xpath("//div[@class='facebox- content']/form/div[3]/button")).click(); Thread.sleep(3000); // Write code here that turns the phrase above into concrete actions } @Then("^I should be seeing new profile picture$") public void i_should_be_seeing_new_profile_picture(){ WebElement s1 = driver.findElement(By.xpath("//*[@class='avatar-upload- container clearfix']/Img")); sb=s1.getAttribute("src"); System.out.println(s1.getAttribute("src")); if(!(sb.equals(sa))) { Assert.assertTrue("File Upload successful", true); } // Write code here that turns the phrase above into concrete actions } @Given("^I click on Your Gists option$") public void i_click_on_Your_Gists_option(){ driver.findElement(By.xpath("//*[@id='user- links']/li[3]/details/summary/img")).click(); List<WebElement> olist = driver.findElements(By.xpath("//*[@id='user- links']/li[3]/details/ul/li/a[@class='dropdown-item']")); for(WebElement o:olist) { if(o.getText().equals("Your Gists")) { o.click(); break; } } // Write code here that turns the phrase above into concrete actions } @When("^I provide filename, description$") public void i_provide_filename_description() throws InterruptedException { // Write code here that turns the phrase above into concrete actions driver.findElement(By.xpath("//div[@class='edit container']/div[@id='gists']/input")).sendKeys("Gist1"); Thread.sleep(2000); Actions actions = new Actions(driver); WebElement element = driver.findElement(By.xpath("//*[@id='gists']/div[2]/div/div[2]/div/div[5]/div[1] /div/div/div/div[5]/div/pre/span")); actions.moveToElement(element); actions.click(); actions.sendKeys("GIST1 Description"); actions.build().perform(); // driver.findElement(By.xpath("//*[@id='gists']/div[2]/div/div[2]/div/div[5]/div[1] /div/div/div/div[5]/div/pre/span")).sendKeys("GIST1 Description"); Thread.sleep(2000); } @When("^click on Create public gist method$") public void click_on_Create_public_gist_method() { driver.findElement(By.xpath("//*[@id='new_gist']/div[2]/div[2]/button[1]")).click(); // Write code here that turns the phrase above into concrete actions } @Then("^i should be seeing the new gist$") public void i_should_be_seeing_the_new_gist(){ List<WebElement> glist = driver.findElements(By.xpath("//div[@class='container repohead-details -container']/ul[1]/li[@class='flex-auto']/div/a")); for(WebElement o:glist) { if(o.getText().equals("Gist1")) { System.out.println("Gist created successfully"); } } // Write code here that turns the phrase above into concrete actions } }
TestNG file
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test name="TestNG"> <packages> <package name="runner"/> </packages> </test> </suite> <!-- Suite -->
Runner File
package runner; import org.junit.runner.RunWith; import cucumber.api.CucumberOptions; import cucumber.api.junit.Cucumber; import cucumber.api.testng.AbstractTestNGCucumberTests; import cucumber.api.SnippetType; @RunWith(Cucumber.class) @CucumberOptions( features="Features", glue="com.Multi", plugin={"html:target/cucumber-html-report", "json:target/cucumber.json", "pretty:target/cucumber-pretty.txt","usage:target/cucumber-usage.json", "junit:target/cucumber-results.xml"}, dryRun = false, strict = true, tags={"@Smoke,@Regression"}, monochrome = true, snippets=SnippetType.CAMELCASE ) public class Runner extends AbstractTestNGCucumberTests{ }
Jenkins Configuration
Access Jenkins URL.
Go to Manage Jenkins > Manage Plugin.
Install the following plugins:
- Cucumber Test Results plugin
- Cucumber Reports
- Cucumber perf plugin
Make sure while installing all the dependent plugins are successfully installed or else the reporting plugins might not work.
Now navigate to Manage -> Global Tool Configuration.
Set the path for JDK.
Set the path for Maven.
Create a new item as a Maven Project.
Once the job got created, click on the configure link present on the left-hand panel.
Scroll down the page.
Set the full path of pom.xml, goal as ‘test’ and save the configuration.
Once the above steps are done, click on the Build Now link that is present in the left-hand panel of the Maven Project.
The build will be executed and the corresponding testing.xml file (which is mentioned over the pom.xml) will get executed.
Here is the output that is displayed on the Console.
Console Output
[INFO] T E S T S
[INFO] ——————————————————-
[INFO] Running TestSuite
New scenario begins
Starting ChromeDriver 2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f) on port 27348
Only local connections are allowed.
log4j:WARN No appenders could be found for logger
(org.apache.http.client.protocol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Nov 25, 2017 12:56:41 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
https://avatars3.githubusercontent.com/u/31874017?s=400&u=c41bfae0fa6b9325fb4f209885b51bd02c7d897d&v=4
https://avatars3.githubusercontent.com/u/31874017?s=400&u=c41bfae0fa6b9325fb4f209885b51bd02c7d897d&v=4
Scenario ends
New scenario begins
Starting ChromeDriver 2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f) on port 24866
Only local connections are allowed.
Nov 25, 2017 12:57:45 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Scenario ends
2 Scenarios (2 passed)
14 Steps (14 passed)
2m2.677s
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 128.513 s – in TestSuite
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[JENKINS] Recording test results
[INFO] ————————————————————————
[INFO] BUILD SUCCESS
[INFO] ————————————————————————
[INFO] Total time: 02:51 min
[INFO] Finished at: 2017-11-25T12:58:45+05:30
[INFO] Final Memory: 12M/28M
[INFO] ————————————————————————
Waiting for Jenkins to finish collecting data
[JENKINS] Archiving D:\cucumberFinal\multiple\pom.xml to com/cucumber.example/0.0.1-SNAPSHOT/cucumber.example-0.0.1-SNAPSHOT.pom
channel stopped
Finished: SUCCESS
Cucumber Jenkins Report
Cucumber Excel
Test Script Automation
The following configuration needs to be done.
Test Design Strategy:
- Create an excel reader utility, which has the ability to take an excel sheet’s ‘columnname’ as input parameter and in return, it will give the excel sheet’s ‘columnvalue’ for that ‘columnanme’.
- Create a feature file, which has the excel sheet’s columnname specified in double-quotes in any of its steps.
- Now create a corresponding Stepdef file for the feature file and use the parameters(arg1, arg2 etc..generated if we specified the word in double quote over the feature file) as input for the excel reader(created in step1).
- Now we have the excel sheet’s columnvalue which can be used as an input to the Stepdef file.
- Execute the feature file as created in the above step.
Feature file
Feature: Title of your feature
I want to use this template for my feature file
Background:
Given I am on Gmail and accessing screen “screen”.
Given I specify Username as “Username” and Password as “Password”.
Given Click on SignIn button.
Scenario: Create new message from data table
When I am accessing the popup “popup”.
And I specify as “To” and subject as “Subject” details and click on the submit button.
Then I should be seeing the success message.
Excel file
package com.cuexcel; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import com.google.common.collect.Maps; import gherkin.formatter.model.Row; public class Excel { private static String sheet = "Sheet1"; private static String worksheet = "D:\\cucumberFinal\\cuexcel\\Files\\tdata.xls"; static HashMap<String, String> excelinfo = new HashMap<String, String>(); public static String get(String ColName) throws IOException { FileInputStream fis; int k =0; String ColValue=null; fis = new FileInputStream(worksheet); HSSFWorkbook wb = new HSSFWorkbook(fis); HSSFSheet ws = wb.getSheet(sheet); int rows = ws.getPhysicalNumberOfRows(); HSSFRow HeaderRow = ws.getRow(0); for(Integer i=1;i<rows;i++){ int cols = ws.getRow(0).getPhysicalNumberOfCells(); for (Integer j = 0; j < cols; j++) { // ColName=HeaderRow.getCell(j).getStringCellValue(); //ColValue=ws.getRow(i).getCell(j).getStringCellValue(); excelinfo.put(HeaderRow.getCell(j).getStringCellValue(),ws.getRow(i). getCell(j).get StringCellValue()); } for(Map.Entry<String, String> m : excelinfo.entrySet()) { if(m.getKey().equals(ColName)) { ColValue= m.getValue(); } } } return ColValue; } } // return m.getValue();;
Stepdef file
package com.cuexcel; import java.io.IOException; import java.util.ArrayList; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; import cucumber.api.Scenario; import cucumber.api.java.Before; import cucumber.api.java.en.Given; import cucumber.api.java.en.Then; import cucumber.api.java.en.When; import cucumber.api.Scenario; import com.cuexcel.Excel; public class CExcel { static public WebDriver driver; Excel e; @Before public static void callexcel() throws IOException, InterruptedException { System.setProperty("webdriver.chrome.driver", "Executables/chromedriver.exe"); driver= new ChromeDriver(); driver.manage().window().maximize(); driver.navigate().to("https://www.google.com/gmail/about"); } @Given("^I am on Gmail and accessing screen \"([^\"]*)\"$") public void i_am_on_Gmail_and_accessing_screen(String arg1) throws Throwable { // Write code here that turns the phrase above into concrete actions driver.findElement(By.xpath("//nav/div/a[2]")).click(); //arg1=Excel.get().get(0).toString(); System.out.println(e.get(arg1) + " - is clicked"); Thread.sleep(3000); } @Given("^I specify Username as \"([^\"]*)\" and Password as \"([^\"]*)\"$") public void i_specify_Username_as_and_Password_as(String arg1, String arg2) throws Throwable { //arg1=Excel.get().get(1).toString(); //arg2=Excel.get().get(2).toString(); driver.findElement(By.xpath("//input[@type='email']")).sendKeys(e.get(arg1). toString()); driver.findElement(By.xpath("//*[@id='identifierNext']/content/span")).click(); Thread.sleep(3000); driver.findElement(By.xpath("//input[@type='password']")). sendKeys (e.get(arg2). toString()); } @When("^Click on SignIn button$") public void click_on_SignIn_button() throws InterruptedException{ driver.findElement(By.xpath("//*[@id='passwordNext']/content/span")).click(); Thread.sleep(5000); } @When("^I am accessing the popup \"([^\"]*)\"$") public void i_am_accessing_the_popup(String arg1) throws Throwable { // Write code here that turns the phrase above into concrete actions //arg1=Excel.get().get(3).toString(); driver.findElement(By.xpath("//*[@id=':x4']/div/div")).click(); System.out.println(e.get(arg1) + "- being clicked"); } @When("^I specify to as \"([^\"]*)\" and subject as \"([^\"]*)\" details and click on submit button$") public void i_specify_to_as_and_subject_as_details_and_click_on_submit_button(String arg1, String arg2) throws Throwable { // Write code here that turns the phrase above into concrete actions String whandle = driver.getWindowHandle(); driver.switchTo().window(whandle); //arg1=Excel.get().get(4).toString(); //arg2=Excel.get().get(5).toString(); driver.findElement(By.xpath("//td[@class='eV']/div[1]/div/textarea")).sendKeys (e.get(arg1)); driver.findElement(By.xpath("//table[@class='aoP aoC bvf']/tbody/tr/td/form/div[3]/input")).sendKeys(e.get(arg2)); driver.findElement(By.xpath("//table[@class='IZ']/tbody/tr/td/div")).click(); Thread.sleep(3000); } @Then("^i should be seeing the success message$") public void i_should_be_seeing_the_success_message() throws Throwable { // Write code here that turns the phrase above into concrete actions WebElement sent = driver.findElement(By.xpath("//*[@id='link_vsm']")); if(sent.isDisplayed()) { System.out.println("Message sent successfully"); } } }
POM file
<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</groupId> <artifactId>cuexcel</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>cuexcel</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.6.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-chrome-driver</artifactId> <version>3.6.0</version> </dependency> <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-core --> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-core</artifactId> <version>1.2.5</version> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-java</artifactId> <version>1.2.5</version> <scope>test</scope> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-junit</artifactId> <version>1.2.5</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-testng</artifactId> <version>1.2.0</version> </dependency> <!-- https://mvnrepository.com/artifact/info.cukes/gherkin --> <dependency> <groupId>info.cukes</groupId> <artifactId>gherkin</artifactId> <version>2.12.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.11-beta3</version> </dependency> <dependency> <groupId>xml-apis</groupId> <artifactId>xml-apis</artifactId> <version>2.0.2</version> </dependency> <dependency> <groupId>xerces</groupId> <artifactId>xercesImpl</artifactId> <version>2.8.0</version> </dependency> <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-jvm-deps --> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-jvm-deps</artifactId> <version>1.0.5</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/net.masterthought/cucumber-reporting --> <dependency> <groupId>net.masterthought</groupId> <artifactId>cucumber-reporting</artifactId> <version>3.11.0</version> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.18</version> <dependencies> <dependency> <groupId>org.apache.maven.surefire</groupId> <artifactId>surefire-junit47</artifactId> <version>2.18</version> </dependency> </dependencies> </plugin> </plugins> </pluginManagement> </build> </project>
Once the above configuration is complete, just execute the feature file.
Observation
The Stepdef file takes the data from the excel sheet and executes the test steps mentioned in the feature file.
Here is the test case execution result as mentioned over the Eclipse console.
Conclusion
BDD is the future of test automation as it enables all the stakeholders to participate in the Test Script creation, especially to provide their inputs over the Feature file.
Cucumber is a widely used BDD tool and it has a lot of integrations and features which are yet to be implemented in test automation. There are several groups/communities actively working on Cucumber.
If you are well versed with Cucumber then you can work on any BDD tool available in the market as most of the tools work similarly to Cucumber.
We hope you learned about Cucumber with Jenkins through this in-depth tutorial.
=> Take A Look At The Jenkins Beginners Guide Here