Selenium Java Tutorial 38 Cucumber Framework in Java Selenium TestNG


What is Cucumber?

is an open-source testing framework

user to write automation tests in simple english language

it supports BDD(Behaviour Driven Development)

writing test cases based on application behavior that everyone can understand

developer
tester
PO
clients

cucumber uses a language called gherkin

syn:

Feature: login functionality

Scenario: valid login

Given user is on login page
When user enter username and password
Then user should navigate to homepage

Who introduced?

aslak - 2008

ruby

Scenario: in-valid login

why use cucumber?

is used becuase it improves communication between techincal and non-techincal teams

driver.findelement(by.xpath("")).sendkeys("");

then verify homepage is displyed.

Beter than other tools

Readable tests - yes - no

Business - yes - no

Collaboration - yes - limited

Advantages:
----------
Easy to understand

non-techincal people can read tests

improves collaboration

supports multiple languages

reusable step definitions

good reports

Dis-Advantages:

initial setup is complex

Not suitable for very small projects

Required maintaining features files

execution may be slower compared to pure automation framework

----------------------

project:
Fetures
login.feature
StepDefinitions
loginStep.java
Runner
TestRunner.java
Utilities

---------------------
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.34.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>7.34.3</version>
<scope>compile</scope>
</dependency>
--------------------
Feature: Login Functionality

Scenario: valid login

Given User Launches Browser
When User Enters username and password
Then User should see homepage
-------------
package stepDefinitions;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import io.cucumber.java.en.*;

public class LoginSteps {

WebDriver driver;
@Given("User Launches Browser")
public void launch_browser() {
driver = new ChromeDriver();
driver.get("https://demo.guru99.com/test/newtours/");
}

@When("User Enters username and password")
public void enterUserNameAndPassword() {
driver.findElement(By.name("userName")).sendKeys("mercury");
driver.findElement(By.name("password")).sendKeys("mercury");
driver.findElement(By.name("submit")).click();
}

@Then("User should see homepage")
public void verify_homepage() {
System.out.println("login Success");
driver.quit();
}
}

------------------
package demo.testng;

import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;


@CucumberOptions(
features="src/test/resources/Features",
glue="stepDefinitions",
plugin= {"pretty", "html:target/cucumber-report.html"}
)
public class TestRunner extends AbstractTestNGCucumberTests {

}
Report abuse