Example1

Feature: Flipkart Shopping


@Tc01

Scenario: Flipkart homepage title Validation

Given I am on Flipkart Homepage

When I am validating the current URL


@Tc02

Scenario: Login Page Title Validation

Given I am on Flipkart Homepage

When I click on Login button

Then I validating the Navigated page url and Title of the Page





==================================


package TestNGRunnerFiles;


import org.junit.runner.RunWith;


import io.cucumber.junit.Cucumber;

import io.cucumber.junit.CucumberOptions;


@RunWith(Cucumber.class)

@CucumberOptions(

features = "src/test/java/FeatureFiles",

glue = {"StepDefinations"},

monochrome = true,

tags = "@Tc02 or @Tc01",

dryRun = false,

plugin = {"pretty", "html:target/cucumber-report.html"}

)

public class FlipKartTestRunner {


}


Uses JUnit to run Cucumber tests. Searches feature files in src/test/java/FeatureFiles. Step definitions are in StepDefinations package. Runs scenarios tagged as @Tc01 or @Tc02. Generates an HTML report in target/cucumber-report.html.


==================================

package StepDefinations;


import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.Assert;


import io.cucumber.java.en.Given;

import io.cucumber.java.en.Then;

import io.cucumber.java.en.When;


public class FlipKartSteps {


WebDriver driver;


@Given("I am on Flipkart Homepage")

public void i_am_on_flipkart_homepage() throws InterruptedException {

driver = new ChromeDriver();

driver.manage().window().maximize();

Thread.sleep(2000);

driver.get("https://www.flipkart.com/");

Thread.sleep(2000);

}


@When("I am validating the current URL")

public void i_am_validating_the_current_URL() {

String actualurl = driver.getCurrentUrl();

String expectedurl = "https://www.flipkart.com/";

if (actualurl.equals(expectedurl)) {

System.out.println("I am in Flipkart Homepage: Url is-->" + expectedurl);

} else

System.out.println("I am not in Flipkart Homepage");

driver.quit();


}


@When("I click on Login button")

public void i_click_on_login_button() throws InterruptedException {

driver.findElement(By.xpath("//span[normalize-space()='Login']")).click();

Thread.sleep(2000);

}


@Then("I validating the Navigated page url and Title of the Page")

public void i_validating_the_navigated_page_url_and_title_of_the_page() {

String actualurl = driver.getCurrentUrl();

String expectedurl = "https://www.flipkart.com/account/login?ret=/";

if (actualurl.equals(expectedurl)) {

System.out.println("I am in Flipkart Homepage: Url is-->" + expectedurl);

} else

System.out.println("I am not in Flipkart Homepage");


String expectedTitle = "Here's the amazing journey that you've had with Flipkart";

String actualTitle = driver.getTitle();

Assert.assertEquals(expectedTitle, actualTitle,

"title not matched ! Expected " + expectedTitle + ", but found : " + actualTitle);

driver.quit();


}

}



Verifies: URL of the login page. Title of the login page (asserted using Assert.assertEquals() from TestNG). Quits browser at the end.

====================================================================