Cucumber
Cucumber
Step 1:
Step 2:
We need to configure our jar files by click Add External jars and then choose our location of the
jar, then select the jars we want to add and click ok.
And the enter the file name it must ends with ”.feature” and Click Finish.
Step 7: Configure in two ways:
1. Now , we need configure Gherkin plugin in eclipse through, Go to Market place Install
New Software
On that window click add .
Package: testrunner
@RunWith(Cucumber.class)
@CucumberOptions(features = "Features", glue = "Step")
public class TestRunner {
}
After, we run the test case, we get console output use that steps for step package.
We create LoginSteps under the steps package and use that console output codes
@Given("^User is on FacebookPage$")
public void user_is_on_FacebookPage() throws Throwable {
System.out.println("User is on FacebookPage");
}
}
@When("^user enter Details$")
public void user_enter_Details() {
System.out.println("user enter Details");
}
Output:
Feature file:
}
Output:
Feature file:
@RunWith(Cucumber.class)
@CucumberOptions(features = "Features", glue = "Step", plugin= {"pretty","junit:Folder
name/report/cucumber.xml"})
public class TestRunner {
}
Output:
@RunWith(Cucumber.class)
@CucumberOptions(features = "Features", glue = "Step", plugin= {"pretty","json:Folder
name/report/cucumber.json"})
public class TestRunner {
}
Output:
DRY RUN:
@RunWith(Cucumber.class)
@CucumberOptions(features = "Features", glue = "Step", dryRun=True)
public class TestRunner {
}
Output:
MONOCHROME:
Feature file:
Package: Test runner
@RunWith(Cucumber.class)
@CucumberOptions(features = "Features", glue = "Step", dryRun=True)
public class TestRunner {
Output:
BACKGROUND:
It’s a keyword used in featured class for declare the common step as once.
Output:
Here “Given” is common for all scenarios …so we declare under the background keyword.
TAGS:
Feature file:
Package: Test runner
@RunWith(Cucumber.class)
@CucumberOptions(features = "Features", glue = "Step", dryRun=True,
tags={"@tags1"})
public class TestRunner {
Output:
FB LOGIN USING CUCUMBER:
Feature file:
Package: testrunner
@RunWith(Cucumber.class)
@CucumberOptions(features = "Feature", glue = "steps")
public class TestRunner {
Package: steps
}
Output:
ENTERING VALUE IN FEATURE FILE:
Feature file:
Step definition:
@RunWith(Cucumber.class)
@CucumberOptions(features = "Feature", glue =
"step",plugin={"pretty","html:Report/cucumber"})
public class TestRunner {
}
Output:
To check multiple values:
Using this method we can check the login function with multiple values without using
iteration
This is the easy way to check multiple values
In this method we use “Examples” one keyword in feature file.
If we use “Examples” , we must give “Scenario Outline” instead of “Scenario”
Feature file:
driver.findElement(By.id("password")).sendKeys(Password);
driver.findElement(By.id("login")).click();
}
Output:
If we enter more than one value it will automatically execute one by one.
By using List:
Feature file:
Step definition:
driver.findElement(By.id("password")).sendKeys(dataList.get(1).get(1));
driver.findElement(By.id("login")).click();
}
@RunWith(Cucumber.class)
@CucumberOptions(features = "Feature", glue =
"step",plugin={"pretty","html:Report/cucumber"})
public class TestRunner {
}
Output:
Feature file:
Step definition :
driver.findElement(By.id("password")).sendKeys(dataMap.get(0).get("Pass
word"));
driver.findElement(By.id("login")).click();
}
}
Test runner class:
@RunWith(Cucumber.class)
@CucumberOptions(features = "Feature", glue =
"step",plugin={"pretty","html:Report/cucumber"})
public class TestRunner {
Output :
Here we using map, so we pass the key and it will enter relevent values of the particular
index
Here for example , one unique username is there, using this user name we need to check
multiple times. That time we get error(i.e) “user name already registered”, these kind of
error will show.
To avoid these, we use RandomStringUtils class and append the value in the user name
So we get the random input
Syntax :
To append alphabet value
String s = RandomStringUtils.randomAlphabetic(4);
Here, 4 means 4 alphabet letters will add randomly
Feature file:
Step definition :
driver.findElement(By.id("password")).sendKeys(dataMap.get(0).get("Pass
word"));
driver.findElement(By.id("login")).click();
}
@RunWith(Cucumber.class)
@CucumberOptions(features = "Feature", glue =
"step",plugin={"pretty","html:Report/cucumber"})
public class TestRunner {
Output :
Here 4 alphanumeric letters added in the user name
It will randomly change for the next iteration
Cucumber with maven and POM:
Framework structure:
POM class:
@FindBy(id = "email")
private WebElement txtUserName;
@FindBy(id = "pass")
private WebElement txtPassword;
public LoginPage() {
PageFactory.initElements(driver, this);
Step definition:
setText(loginPage.getTxtUserName(), userName);
setText(loginPage.getTxtPassword(), Password);
clickBtn(loginPage.getBtnLogin());
driver.quit();
Feature file:
Utility package:
Base:
} else if (browser.equals("firefox")) {
System.setProperty("webdriver.gecko.driver",
f.getAbsolutePath()
+ "/geckodriver.exe");
driver = new FirefoxDriver();
} else if (browser.equals("ie")) {
System.setProperty("webdriver.ie.driver",
f.getAbsolutePath()
+ "/IEDriverServer.exe");
driver = new InternetExplorerDriver();
driver.manage().window().maximize();
driver.get((String) jsonObject.get("url"));
return driver;
}
wait.until(ExpectedConditions.elementToBeClickable(element));
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
res = element.isDisplayed();
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
return res;
}
}
public static String getText(WebElement element) {
String name = null;
if (elementFound(element)) {
name = element.getAttribute("value");
}
return name;
}
} catch (Exception e) {
e.printStackTrace();
}
return jsonObject;
}
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
switch (currentCell.getCellType()) {
case Cell.CELL_TYPE_STRING:
mapDatas.put(headerRow.getCell(j).getStringCellValue(),
currentCell.getStringCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
mapDatas.put(headerRow.getCell(j).getStringCellValue(),
String.valueOf(currentCell
.getNumericCellValue()));
break;
}
}
mapDatasList.add(mapDatas);
}
} catch (Throwable e) {
e.printStackTrace();
}
return mapDatasList;
rs = ps.executeQuery();
while (rs.next()) {
Employee e = new Employee();
e.setName(rs.getString("name"));
e.setPassword(rs.getString("password"));
emp.add(e);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return emp;}}
Test runner class:
@RunWith(Cucumber.class)
@CucumberOptions(features = "Features", glue = "steps")
public class TestRunner {
Output:
Cucumber with POM and data base:
In this method, we taken all the input data’s from data base
Framework structure:
Step definition:
setText(loginPage.getTxtUserName(), emp.get(0).getName());
setText(loginPage.getTxtPassword(), emp.get(0).getPassword());
clickBtn(loginPage.getBtnLogin());
}
driver.quit();
POM class:
@FindBy(id = "email")
private WebElement txtUserName;
@FindBy(id = "pass")
private WebElement txtPassword;
public LoginPage() {
PageFactory.initElements(driver, this);
}
public WebElement getTxtUserName() {
return txtUserName;
}
Utility package:
Base:
} else if (browser.equals("firefox")) {
System.setProperty("webdriver.gecko.driver",
f.getAbsolutePath()
+ "/geckodriver.exe");
driver = new FirefoxDriver();
} else if (browser.equals("ie")) {
System.setProperty("webdriver.ie.driver",
f.getAbsolutePath()
+ "/IEDriverServer.exe");
driver = new InternetExplorerDriver();
driver.manage().window().maximize();
driver.get((String) jsonObject.get("url"));
return driver;
}
wait.until(ExpectedConditions.elementToBeClickable(element));
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
public static boolean elementFound(WebDriver driver, int time,
WebElement element) {
boolean res = false;
driver.manage().timeouts().implicitlyWait(time,
TimeUnit.SECONDS);
try {
res = element.isDisplayed();
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
return res;
}
} catch (Exception e) {
e.printStackTrace();
}
return jsonObject;
}
switch (currentCell.getCellType()) {
case Cell.CELL_TYPE_STRING:
mapDatas.put(headerRow.getCell(j).getStringCellValue(),
currentCell.getStringCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
mapDatas.put(headerRow.getCell(j).getStringCellValue(),
String.valueOf(currentCell
.getNumericCellValue()));
break;
}
}
mapDatasList.add(mapDatas);
}
} catch (Throwable e) {
e.printStackTrace();
}
return mapDatasList;
rs = ps.executeQuery();
while (rs.next()) {
Employee e = new Employee();
e.setName(rs.getString("name"));
e.setPassword(rs.getString("password"));
emp.add(e);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return emp;
Employee:
@RunWith(Cucumber.class)
@CucumberOptions(features = "Features", glue = "steps")
public class TestRunner {
Feature file:
Examples:
| userName | password |
| ganesh | Java65 |
Input data:
Output :
Cucumber with POM and Data driven:
Here we use data driven to read the input data’s from excel sheet
Framework structure:
Here one excel folder will added and in this folder have one excel sheet contains all the
input datas
POM class:
@FindBy(id = "email")
private WebElement txtUserName;
@FindBy(id = "pass")
private WebElement txtPassword;
public LoginPage() {
PageFactory.initElements(driver, this);
Base class:
} else if (browser.equals("firefox")) {
System.setProperty("webdriver.gecko.driver",
f.getAbsolutePath()
+ "/geckodriver.exe");
driver = new FirefoxDriver();
} else if (browser.equals("ie")) {
System.setProperty("webdriver.ie.driver",
f.getAbsolutePath()
+ "/IEDriverServer.exe");
driver = new InternetExplorerDriver();
driver.manage().window().maximize();
driver.get((String) jsonObject.get("url"));
return driver;
}
wait.until(ExpectedConditions.elementToBeClickable(element));
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
res = element.isDisplayed();
} catch (Exception e) {
e.printStackTrace();
}
return res;
} catch (Exception e) {
e.printStackTrace();
}
return jsonObject;
}
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
switch (currentCell.getCellType()) {
case Cell.CELL_TYPE_STRING:
mapDatas.put(headerRow.getCell(j).getStringCellValue(),
currentCell.getStringCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
mapDatas.put(headerRow.getCell(j).getStringCellValue(),
String.valueOf(currentCell
.getNumericCellValue()));
break;
}
}
mapDatasList.add(mapDatas);
}
} catch (Throwable e) {
e.printStackTrace();
}
return mapDatasList;
rs = ps.executeQuery();
while (rs.next()) {
Employee e = new Employee();
e.setName(rs.getString("name"));
e.setPassword(rs.getString("password"));
emp.add(e);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return emp;
@RunWith(Cucumber.class)
@CucumberOptions(features = "Features", glue = "steps")
public class TestRunner {
Feature file:
Examples:
| userName | password |
| ganesh | Java65 |
}
@When("^User enters \"([^\"]*)\",\"([^\"]*)\" and click the login
button$")
public void user_enters_and_click_the_login_button(String userName,
String Password) {
loginPage = new LoginPage();
setText(loginPage.getTxtUserName(),readValueFromExcelSheet().get(1)
.get("Username"));
setText(loginPage.getTxtPassword(),readValueFromExcelSheet().get(1)
.get("Password"));
clickBtn(loginPage.getBtnLogin());
driver.quit();
Excel input:
Output :