Cucumber Project Setup



Install Below 5 Softwares 













Copy and paste below POM file 

POM File - > 

  <properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<java.version>1.8</java.version>

</properties>


<dependencies>

<dependency>

<groupId>org.testng</groupId>

<artifactId>testng</artifactId>

<version>6.9.8</version>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi</artifactId>

<version>4.0.1</version>

</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-ooxml</artifactId>

<version>4.0.1</version>

</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-ooxml-schemas</artifactId>

<version>4.0.1</version>

</dependency>


<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->

<dependency>

<groupId>io.rest-assured</groupId>

<artifactId>rest-assured</artifactId>

<version>4.1.2</version>

</dependency>

<!-- https://mvnrepository.com/artifact/com.aventstack/extentreports -->

<dependency>

<groupId>com.aventstack</groupId>

<artifactId>extentreports</artifactId>

<version>4.1.3</version>

</dependency>


<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->

<dependency>

<groupId>org.apache.commons</groupId>

<artifactId>commons-collections4</artifactId>

<version>4.4</version>

</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress -->

<dependency>

<groupId>org.apache.commons</groupId>

<artifactId>commons-compress</artifactId>

<version>1.19</version>

</dependency>

<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->

<dependency>

<groupId>commons-io</groupId>

<artifactId>commons-io</artifactId>

<version>2.5</version>

</dependency>

<!-- https://mvnrepository.com/artifact/dom4j/dom4j -->

<dependency>

<groupId>dom4j</groupId>

<artifactId>dom4j</artifactId>

<version>1.6.1</version>

</dependency>

<!-- https://mvnrepository.com/artifact/com.beust/jcommander -->

<dependency>

<groupId>com.beust</groupId>

<artifactId>jcommander</artifactId>

<version>1.78</version>

</dependency>

<!-- https://mvnrepository.com/artifact/com.jcraft/jsch -->

<dependency>

<groupId>com.jcraft</groupId>

<artifactId>jsch</artifactId>

<version>0.1.42</version>

</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans -->

<dependency>

<groupId>org.apache.xmlbeans</groupId>

<artifactId>xmlbeans</artifactId>

<version>3.0.2</version>

</dependency>

<dependency>

<groupId>com.google.guava</groupId>

<artifactId>guava</artifactId>

<version>25.1-jre</version>

</dependency>


<dependency>

<groupId>info.cukes</groupId>

<artifactId>cucumber-testng</artifactId>

<version>1.2.4</version>

</dependency>

<dependency>

<groupId>org.testng</groupId>

<artifactId>testng</artifactId>

<version>6.9.9</version>

</dependency>

<dependency>

<groupId>net.masterthought</groupId>

<artifactId>cucumber-reporting</artifactId>

<version>4.0.0</version>

</dependency>


<dependency>

<groupId>info.cukes</groupId>

<artifactId>cucumber-picocontainer</artifactId>

<version>1.2.5</version>

</dependency>

</dependencies>


Runner File - >

package com.API.runner;

import java.io.IOException;

import org.testng.ITestResult;

import org.testng.annotations.AfterClass;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.AfterSuite;

import org.testng.annotations.BeforeClass;

import org.testng.annotations.BeforeSuite;

import com.API.report.reportHelp;

import cucumber.api.CucumberOptions;

import cucumber.api.testng.AbstractTestNGCucumberTests;

import reusableFiles.Utilities;

@CucumberOptions(strict = true, monochrome = true, features = "src/test/resources/features", glue = "com.API.StepDef", format = {

"pretty", "json:target/cucumber.json" })

public class CucumberRunner extends AbstractTestNGCucumberTests {

@BeforeSuite(alwaysRun = true)

public void setUp() throws Exception {

}


@BeforeClass(alwaysRun = true)

public void beforeClass() throws Exception {

Utilities.openExcelWorkBook();

}

@AfterClass(alwaysRun = true)

public void takeScreenshot() throws Exception {

Utilities.Close();

}


@AfterMethod(alwaysRun = true)

public void tearDownr(ITestResult result) throws IOException {


}

@AfterSuite(alwaysRun = true)

public void generateHTMLReports() {

reportHelp.generateCucumberReport();

}

}


Feature File - >

Feature: API Testing
  I want to do API Testing using RestAssured

  Scenario: Execute Module A API's
    Given Initialize Module A
    Then Execute Module A APIs

ReportHelp - >

package com.API.report;


import java.io.File;

import java.util.ArrayList;

import java.util.List;

import net.masterthought.cucumber.*;


public class reportHelp {

    public static void generateCucumberReport() {

        File reportOutputDirectory = new File("target");

        ArrayList<String> jsonFiles = new ArrayList<String>();

        jsonFiles.add("target/cucumber.json");

        String projectName = "My API Project";

        Configuration configuration = new Configuration(reportOutputDirectory, projectName);

        configuration.addClassifications("Platform", System.getProperty("os.name"));

        //configuration.addClassifications("Browser", "Chrome");

        List<String> classificationFiles = new ArrayList<String>();

        classificationFiles.add("src/test/resources/config/config.properties");

        configuration.addClassificationFiles(classificationFiles);

        ReportBuilder reportBuilder = new ReportBuilder(jsonFiles, configuration);

        reportBuilder.generateReports();

    }


}

APIStepDef - >

package com.API.StepDef;


import org.testng.AssertJUnit;

import cucumber.api.java.en.Given;

import cucumber.api.java.en.Then;

import reusableFiles.Utilities;


public class APIStepDef {


boolean verification;

@Given("Initialize Module A")

public void mdAInitialization() throws Exception{

System.out.println("@Given");

System.out.println(Utilities.GetCellData("Testing"));

}


@Then("Execute Module A APIs")

public void mdAAPIexecute() throws Throwable {

System.out.println("@Then");

verification = true;

AssertJUnit.assertEquals(true, verification);

}

}


Utilities File - >

package reusableFiles;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Utilities {

public static String sheetName ="TestDataSheet";
private String propertyFile = "config.properties";
public FileWriter objwriter;
static boolean htmlheader = true;
public int iRow = 0;
public long startTime;
public String newStartTime = null;
public static int iPassCount;
public static int iFailCount;
public static int tPassCount;
public static int tFailCount;
// public String excelFilePath=null;
public static File fileName;
public static FileInputStream fileInputStream;
public String keyname = null;
public static XSSFWorkbook excelWorkbook;
Properties prop;
FileInputStream input = null;

// Open Excel File
public static void openExcelWorkBook() throws Exception {

fileName = new File(".\\Test Data\\MyTestdata.xlsx");
// fileName = new File((excelFilePath));
fileInputStream = new FileInputStream(fileName);
excelWorkbook = new XSSFWorkbook(fileInputStream);

}

/************
* Reading values from Properties file
* @param Filename
* @param KeyName
* @return KeyValue
* @throws IOException
*/
public String ReadPropertiesFile(String Filename, String KeyName) throws IOException {
prop=new Properties();
  String KeyValue = null;
  input = new FileInputStream("config//"+ Filename +".properties");
 
  // load a properties file
  prop.load(input);
 
  // get the property value and print it out
  KeyValue = prop.getProperty(KeyName);

  return KeyValue;
}

public static String GetTestData(String KeyValue, String MyColumnName, String SheetName) throws IOException {
String MyFinalvalue1 = null;
String MyRowFlag1 = KeyValue;
XSSFSheet MySheet1 = excelWorkbook.getSheet(SheetName);
int RowCount = MySheet1.getLastRowNum() + 1;
int ColumnCount = MySheet1.getRow(0).getLastCellNum();
int TempValue = 0;
for (int iRow = 1; iRow < RowCount; iRow++) {
XSSFRow row = MySheet1.getRow(iRow);
int flag = 0;
for (int cellValue = 0; cellValue < row.getPhysicalNumberOfCells(); cellValue++) {
row.getCell(cellValue, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
XSSFCell cellValue1 = MySheet1.getRow(iRow).getCell(cellValue);
// cellValue1.setCellType(CellType.STRING);
if (MyRowFlag1.equals(cellValue1.getStringCellValue())) {
//
for (int iColumn = 0; iColumn < ColumnCount; iColumn++) {
XSSFRow row2 = MySheet1.getRow(0);
XSSFCell cell1 = row2.getCell(iColumn);
if (MyColumnName.equals(cell1.getStringCellValue())) {
//
TempValue = iColumn;
flag = 1;
break;
}
}
XSSFRow row1 = MySheet1.getRow(iRow);
MyFinalvalue1 = row1.getCell(TempValue).getStringCellValue();
//
break;
}
if (flag == 1) {
break;
}
}
if (flag == 1) {
break;
}
}

return MyFinalvalue1;
}

public void WriteExpectedResults(String keyvalue, String sheetName, String MyColumnName, String value)
throws IOException {
FileInputStream MyInputStream1 = new FileInputStream(fileName);
FileOutputStream fos = null;
excelWorkbook = new XSSFWorkbook(MyInputStream1);
String MyRowFlag1 = keyvalue;
XSSFSheet MySheet1 = excelWorkbook.getSheet(sheetName);
int RowCount = MySheet1.getLastRowNum() + 1;
int ColumnCount = MySheet1.getRow(0).getLastCellNum();
int TempValue = 0;
for (int iRow = 1; iRow < RowCount; iRow++) {
XSSFRow row = MySheet1.getRow(iRow);
int flag = 0;
for (int cellValue = 0; cellValue < row.getPhysicalNumberOfCells(); cellValue++) {

row.getCell(cellValue, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
XSSFCell cellValue1 = MySheet1.getRow(iRow).getCell(cellValue);
// cellValue1.setCellType(CellType.STRING);
if (MyRowFlag1.equals(cellValue1.getStringCellValue())) {
//
for (int iColumn = 0; iColumn < ColumnCount; iColumn++) {
XSSFRow row2 = MySheet1.getRow(0);
XSSFCell cell1 = row2.getCell(iColumn);
if (MyColumnName.equals(cell1.getStringCellValue())) {
//
TempValue = iColumn;
flag = 1;
break;
}
}
XSSFCell cell1 = row.getCell(TempValue);
cell1.setCellValue(value);
fos = new FileOutputStream(fileName);
excelWorkbook.write(fos);
fos.close();
break;
}
if (flag == 1) {
break;
}
}
if (flag == 1) {
break;
}
}

}

/********
* Fetching the row count excel sheet
* @param Sheetname
* @return
* @throws IOException
*/
public static int GetRowCount(String sheetName) throws IOException {
System.out.println("row Count sheet name " + sheetName);
XSSFSheet MySheet1 = excelWorkbook.getSheet(sheetName);
int RowCount = MySheet1.getLastRowNum() + 1;

return RowCount;
}

public static String ActualDataConversion(ArrayList<String> actualData2) throws Exception {
String FianlActual = actualData2.toString(); // Converting Array to
// String
FianlActual = FianlActual.replace(", ", "|");
FianlActual = FianlActual.replace("[", "");
FianlActual = FianlActual.replace("]", "");
return FianlActual;
}
public static String GetCellData(String Scenario_Key) throws Exception {
String ExpectedData = null;
int rowCount = GetRowCount(sheetName);
for (int i = 1; i < rowCount; i++) {
if ((GetTestData(Scenario_Key, "Run", sheetName)).equals("YES"))
{
ExpectedData = GetTestData(Scenario_Key, "Expected", sheetName);
}
}
return ExpectedData;
}

public static void Close() throws Exception {
excelWorkbook.close();
fileInputStream.close();
}

public String TestScenPropertyFile(String ScenarioName) throws IOException {
String SheetName = ReadPropertiesFile(propertyFile, ScenarioName);
return SheetName;
}

public static String DynamicValue()
{
Date currentdatetime = new Date();
String NewDatetime = currentdatetime.toString();
NewDatetime = NewDatetime.replace ("/", "");
NewDatetime = NewDatetime.replace (" ", "");
NewDatetime = NewDatetime.replace ("AM", "");
NewDatetime = NewDatetime.replace ("PM", "");
NewDatetime = NewDatetime.replace (":", "");
NewDatetime = NewDatetime.replace ("IST", "");
return NewDatetime;
}
}



No comments:

Post a Comment