Cucumber Scenario data table and Scenario Outline data table
1.Cucumber Scenario data table
Sample feature file contents as below:
Feature: Login and Logout Action
Scenario: Successful Login with Valid Credentials
Given User is on Application Home Page
When User Navigates to Application LogIn Page
And User enters UserName and Password and click submit button
|UserName|Pwd |
|abc | 123 |
Then Message displayed as Login Successfully
Corresponding Selenium script contents as below:
@When(“^User enters UserName and Password and click submit button$”)
public void user_enters_UserName_and_Password_and_click_submit_button(DataTable table) throws Throwable {
// Write code here that turns the phrase above into concrete actions
List<List<String>> data = table.raw();
driver.findElement(By.id(“IDTokenA1”)).sendKeys(data.get(1).get(0));
driver.findElement(By.id(“IDTokenB1”)).sendKeys(data.get(1).get(1));
driver.findElement(By.name(“Login.Submit”)).click();
// throw new PendingException();
}
2.Cucumber Scenario Outline data table
Sample feature file contents as below:
Scenario Outline: Login functionality for a website.
Given User navigates to Login page
When user enters Username as “<username>” and Password as “<password>”
Then login should be unsuccessful
Examples:
| username | password |
| ABC | 123 |
Corresponding Selenium script contents as below:
@When(“^user enters Username as \“([^\”]*)\” and Password as \”([^\”]*)\”$”)
public void user_enters_Username_as_and_Password_as(String arg1, String arg2) {
driver.findElement(By.id(“IDTokenA1“)).sendKeys(arg1);
driver.findElement(By.id(“IDTokenB1“)).sendKeys(arg2);
driver.findElement(By.id(“Login.Submit“)).click();
}
3.TestRunner sample code
package cucumberTestRunnerTest;
/*
* cucumberTestRunner sample code below
*
*/
import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;
import cucumber.api.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions(format={“pretty”}
,features = “Feature” // refer the above feature file sample
,glue={“stepDefinition”} // note: need to create a package stepDefinition and create a regression scriptclass
,dryRun = false)
public class TestRunnerTest {
}
Note: JUnit runner uses the JUnitframework to run Cucumber using annotation – you could see in the above code that single empty class with annotation.
4.Maven pom.xml file sample needs the following dependencies if you want to use Maven for build automation.
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.0.14</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.0.14</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-testng</artifactId>
<version>1.1.5</version>
</dependency>
</dependencies>
5.If you do not want to use Maven for build automation, you can attach the following jars to your project Java Build path.
cucumber-java
cucumber-core
cucumber-junit
cucumber-jvm-deps
cucumber-reporting
gherkin
junit
mockito-all
cobertura
6.Regression test case script will be like this (refer the Test runner sample code point #3 above)
package stepDefinition;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
importorg.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.WebDriver;
import cucumber.api.DataTable;
import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class RegressionTest { // refer the Test runner sample code point #3 to see how the this code is mapped
WebDriver driver;
// run TestRunner.java file and get the out put from Console and copy/paste below:
@Given(“^User is on Application Login Page$”)
public void user_is_on_Application_Home_Page() throws Throwable {
// Write code here that turns the phrase above into concrete actions
driver = new InternetExplorerDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//throw new PendingException();
}
7. If you do not want to use the DataTable from feature file, always you can pull the data from XLS file using the logic below.
package yourpagename;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
importjava.util.List;
importjava.util.concurrent.TimeUnit;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebElement;
importorg.openqa.selenium.ie.InternetExplorerDriver;
importorg.openqa.selenium.support.ui.Select;
importorg.openqa.selenium.WebDriver;
importorg.testng.annotations.AfterClass;
importorg.testng.annotations.AfterTest;
importorg.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import Package.AnyotherClass; // if needed
public class AnyotherClassPage extends InitialClass
{
//WebDriver driver;
AnyotherClass ObjectName;
@Test(priority = 1)
public void Excel() throws Exception
{
ObjectName = new AnyotherClass (driver);
ObjectName.objectclick();
System.out.println(“Started executing Test Cases”);
// load excel file
File filePath = new File(“C:\\xxxxx\\ExcelFileName.xls”);
FileInputStream FS = new FileInputStream(filePath);
HSSFWorkbook WB = new HSSFWorkbook(FS);
HSSFSheet sheet = WB.getSheetAt(0);
int rowcount = sheet.getLastRowNum() – sheet.getFirstRowNum();
int colcount = sheet.getRow(0).getLastCellNum();
int a =0 ;
System.out.println(“Number of rows” + rowcount);
System.out.println(“Number of columns” +colcount);
for(int b=1;b<rowcount;b++)
{
HSSFRow row = sheet.getRow(b);
//for(int j=0;j<colcount;j++)
//{
//HSSFCell cell = row.getCell(j);
String excelCell1 = row.getCell(a).getStringCellValue();
String excelCell2 = row.getCell(a+1).getStringCellValue();
System.out.println(“XLS Values are — “ + excelCell1 + “—“ + excelCell2);
//}
ObjectName.NewMethod1(excelCell1);
ObjectName.NewMethod2(excelCell2); // code continues … …
TestRunner is a program used in Cucumber to access Feature file as copied in the picture below.
Feature file is something that has user requirement scenarios written in English which gives more readability and understandability of the requirement which is called Gherkin language.
So any roles in the company like Tester, developer, end user, and techno functional person can read it and understand it. Also this will reduce the complication in terms of missing requirement or misrepresenting or misunderstanding requirements between different roles like Developer , tester, and end user,
Write test runner code and execute the same . Please refer BDD testing details on Selenium Cucumber