Cucumber with Selenium Automation

Partha Sai Guttikonda
3 min readOct 7, 2022

--

Project setup for cucumber Selenium automation using page object modal

Photo by Kaleidico on Unsplash

Steps to be followed →

  1. Create a maven project (I will be using IntelliJ).🛠
  2. Add the dependences.🧞‍♂️
  3. Create the Runner JAVA file.💡
  4. Create as per the Runner file structure.⏳
  5. Run tests using feature files ✅

Create a maven project in IntelliJ without any dependencies in it

  • Just provide the project name and location to be stored in the process of creation maven project.
Photo by Jeremy Bezanger on Unsplash

Adding Dependences to pom.xml → Given pom.xml below

<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>amazon-automation</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-gherkin</artifactId>
<version>7.6.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>7.6.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-gherkin</artifactId>
<version>7.6.0</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.6.0</version>
</dependency>
</dependencies>

</project>

As per the given pom.xml file add the dependencies.

Add Runner.JAVA to the project →

Runner.java

@CucumberOptions(
features = "src/test/resources/features",
glue = "StepDefinitions",
plugin = {
"pretty",
"html:target/cucumber-reports/cucumber-pretty",
"json:target/cucumber-reports/CucumberTestReport.json",
"timeline:target/test-output-thread/"
}
)
public class Runner extends AbstractTestNGCucumberTests {



@Override
@DataProvider(parallel = true)
public Object[][] scenarios() {
return super.scenarios();
}

@BeforeSuite
public void beforeSuite() {
System.out.println("================ BEFORE SUITE==========");
}

@AfterSuite
public void afterSuite() {
System.out.println("================ AFTER SUITE ==========");
}
}
  • In the above file we need to specify the path there the feature files are located in the project.(src/test/resources/features)
  • Also we have to mention the package name there the steps will be writen in the project.(StepDefinitions)

Create page object modals →

  • Make BasePage class to place common methods which can be used to all the page classes.
public class BasePage {
private static WebDriver driver;
public static final int TIMEOUT_PERIOD_LONG = 30;

public BasePage(WebDriver driver) {
this.driver = driver;
}

public WebElement waitForElement(By element, long timeout) {
WebElement myElement = null;
try {
myElement = new WebDriverWait(driver, Duration.ofSeconds(timeout)).until(ExpectedConditions.visibilityOfElementLocated(element));
} catch (TimeoutException toe) {
System.out.println(toe);
} finally {
if (myElement == null) {
String str = "Unable to find the WebElement in the web page by using its locator";
System.out.println(str);
}
}
return myElement;
}

public void waitForElementToBeVisible(ById element){
WebElement myElement = new WebDriverWait(driver, Duration.ofSeconds(5)).until(ExpectedConditions.visibilityOfElementLocated(element));
WebDriverWait wait= new WebDriverWait(driver,Duration.ofSeconds(5));
wait.until(ExpectedConditions.visibilityOf(myElement));
}

public void waitForElementToBeVisible(ByXPath element){
WebElement myElement = new WebDriverWait(driver, Duration.ofSeconds(5)).until(ExpectedConditions.visibilityOfElementLocated(element));
WebDriverWait wait= new WebDriverWait(driver,Duration.ofSeconds(5));
wait.until(ExpectedConditions.visibilityOf(myElement));
}
}
  • Creating page class like shown below. all the page classes will be extending the BasePage class.
public class HomePage extends BasePage {
public HomePage(WebDriver driver) {
super(driver);
PageFactory.initElements(driver, this);
}

public final ById searchInput = new ById("twotabsearchtextbox");

public WebElement getSearchInput() {
return waitForElement(searchInput,30);
}

public void waitTillSearchVisible() {
waitForElementToBeVisible(searchInput);
}
}

SearchResultPage.class

public class SearchResultPage extends BasePage {public SearchResultPage(WebDriver driver) {
super(driver);
PageFactory.initElements(driver, this);
}
public final ById logoBtn = new ById("nav-logo-sprites");public final ByXPath item = new ByXPath("/html/body/div[1]/div[2]/div[1]/div[1]/div/span[3]/div[2]/div[4]/div/div/div/div/div[2]/div[2]/h2/a");public WebElement getItem() {
return waitForElement(item, 30);
}
public WebElement getLogoBtn() {
return waitForElement(logoBtn, 30);
}
public void waitTillLogoVisible() {
waitForElementToBeVisible(logoBtn);
}
}

Now we need to create a package stepdefinition and write all the steps which will be used in feature file.

Hooks.java

public class Hooks {
public static WebDriver driver;
@Before
public void OpenDriver() {
System.setProperty("webdriver.chrome.driver", "src/test/resources/drivers/mac/chromedriver");
driver = new ChromeDriver();
}
@After
public void closeDriver() {
driver.close();
}
}

HomePageSteps.java

public class HomePageSteps {private final HomePage homePage;public HomePageSteps() {
this.homePage = PageObjectManager.pageFactory().getHomePage(Hooks.driver);
}
@Given("I'm on amazon")
public void onAmazon() {
Hooks.driver.get("https://www.amazon.com/");
this.homePage.waitTillSearchVisible();
}
@When("I select Search for {string}")
public void enterSearch(String enterText) {
WebElement searchInput = this.homePage.getSearchInput();
searchInput.clear();
searchInput.sendKeys(enterText);
searchInput.sendKeys(Keys.ENTER);
}
}

SearchPageSteps.java

public class SearchPageSteps {
public final SearchResultPage searchResultPage;
public SearchPageSteps() {
this.searchResultPage = PageObjectManager.pageFactory().getSearchResultPage(Hooks.driver);
}
@Then("I'm on result page")
public void onResultPage() {
this.searchResultPage.waitTillLogoVisible();
}
@And("I click on item")
public void clickOnItem() throws InterruptedException {
WebElement btn = this.searchResultPage.getItem();
btn.click();
Hooks.driver.wait(300);
}
}

Features file

Feature: Search item 
Scenario: Search for given item
Given I'm on amazon
When I select Search for "jackets"
Then I'm on result page
And I click on item

Now according to the statements in feature file Steps will be executed.

complete code is available at https://github.com/propardhu/cucumber-selenium …!

--

--

Partha Sai Guttikonda
Partha Sai Guttikonda

Written by Partha Sai Guttikonda

Full Stack Developer | Medical Imaging & ML | Certified AWS Cloud Practitioner | Open to Work | JAVA, C++