Integrated with Selenum Driver

 Feature: Navigate to advanced search page


@SeleniumRun

Scenario: Navigate to Advanced Search Page Items

Given I am on Ebay URL Page

When I click on Advanced Link

Then I navigated to advanced search page


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


package Steps;


import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;


import io.cucumber.java.en.Given;

import io.cucumber.java.en.When;

import io.cucumber.java.en.Then;


public class Ebay_homeSteps {

WebDriver driver;

@Given("I am on Ebay URL Page")

public void I_am_on_EbayURLPage() throws InterruptedException {

driver=new ChromeDriver();

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

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

Thread.sleep(3000);

}


@When("I click on Advanced Link")

public void I_clickon_AdvancedLink() {

driver.findElement(By.linkText("Advanced")).click();

}


@Then("I navigated to advanced search page")

public void I_navigated_to_advanced_searchpage() {

String expurl="https://www.ebay.com/sch/ebayadvsearch";

String orgurl=driver.getCurrentUrl();

if(expurl.equals(orgurl)) {

System.out.println("page navigated to expected URL");

}else

{

System.out.println("pade did not navigated as expected");

}

driver.quit();

}

}



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


package testRunner;


import io.cucumber.testng.AbstractTestNGCucumberTests;

import io.cucumber.testng.CucumberOptions;


@CucumberOptions(features = "src/test/java/Features", glue = "Steps", monochrome = true, tags = "@SeleniumRun",

plugin = {"pretty","html:target/seleniumreport.html"} )

public class seleniumTestRunner extends AbstractTestNGCucumberTests {


}


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


The user has provided three code snippets: a Gherkin feature file, a Java Cucumber step definition file, and a Java TestNG Cucumber runner file. I will explain each part.

Explanation of the Provided Code

The provided code sets up an automated test using Selenium, Cucumber, and TestNG to verify navigation to eBay's advanced search page.

Here's a breakdown of each component:

1. Feature: Navigate to advanced search page (Gherkin Feature File)

This is a Gherkin feature file (.feature extension), which describes the functionality in a human-readable format. It's the core of Behavior-Driven Development (BDD).

  • Feature: Navigate to advanced search page: This is the overall title of the feature being tested.
  • @SeleniumRun: This is a tag. Tags are used to group scenarios and selectively run them. In this case, the seleniumTestRunner is configured to run scenarios with this tag.
  • Scenario: Navigate to Advanced Search Page Items: This is a specific test scenario within the feature. It describes a single flow or behavior.
  • Given I am on Ebay URL Page: This is a Given step. It sets up the initial state or preconditions for the scenario.
  • When I click on Advanced Link: This is a When step. It describes the action or event that triggers the behavior being tested.
  • Then I navigated to advanced search page: This is a Then step. It describes the expected outcome or result after the action has been performed.

2. Steps/Ebay_homeSteps.java (Java Cucumber Step Definition File)

This Java file contains the step definitions that link the Gherkin steps to actual executable code. It uses Selenium WebDriver to interact with the web browser.

package Steps;: Declares the package for this class. import org.openqa.selenium.By;, import org.openqa.selenium.WebDriver;, import org.openqa.selenium.chrome.ChromeDriver;: These lines import necessary Selenium classes for browser automation. import io.cucumber.java.en.Given;, import io.cucumber.java.en.When;, import io.cucumber.java.en.Then;: These lines import Cucumber annotations that link methods to Gherkin steps. public class Ebay_homeSteps {: Defines the step definition class. WebDriver driver;: Declares a WebDriver instance, which will be used to control the web browser.


@Given("I am on Ebay URL Page")

driver = new ChromeDriver()Initializes a new Chrome browser instance. 

(Note: You'll need the ChromeDriver executable set up in your system's PATH or specified via System.setProperty.)

driver.manage().window().maximize();: Maximizes the browser window.

driver.get("https://www.ebay.com/");: Navigates the browser to the eBay homepage.

Thread.sleep(3000);: Pauses the execution for 3 seconds.
This is generally not recommended in robust automation due to its static nature; explicit waits (e.g., WebDriverWait) are preferred for better reliability.


@When("I click on Advanced Link")

driver.findElement(By.linkText("Advanced")).click();: 

Locates the web element with the link text "Advanced" and clicks on it. This is expected to navigate to the advanced search page.


@Then("I navigated to advanced search page")

String expurl="https://www.ebay.com/sch/ebayadvsearch";: Defines the expected URL of the advanced search page.

String orgurl=driver.getCurrentUrl();: Gets the current URL from the browser.

if(expurl.equals(orgurl)) { ... } else { ... }: Compares the expected URL with the actual current URL.

If they match, it prints "page navigated to expected URL".

If they don't match, it prints "pade did not navigated as expected".

driver.quit();: Closes the browser and all associated windows. This is crucial for releasing resources after the test.


3. testRunner/seleniumTestRunner.java (Java TestNG Cucumber Runner File)

package testRunner;: Declares the package for this class.

import io.cucumber.testng.AbstractTestNGCucumberTests;: 

Imports the base class from Cucumber-TestNG integration, which provides TestNG capabilities for running Cucumber.

import io.cucumber.testng.CucumberOptions;: Imports the annotation used to configure Cucumber.


@CucumberOptions(...): This annotation configures how Cucumber will run the tests.

  1. features = "src/test/java/Features": Specifies the path to the directory where Cucumber should look for Gherkin feature files.
  2. glue = "Steps": Specifies the package where Cucumber should look for step definition files (i.e., the Ebay_homeSteps class).
  3. monochrome = true: Makes the console output more readable by removing unnecessary characters.
  4. tags = "@SeleniumRun": Tells Cucumber to only execute scenarios (or features) that are tagged with @SeleniumRun. This matches the tag in the feature file.
  5. plugin = {"pretty","html:target/seleniumreport.html"}: Configures the reporting options.
  6. "pretty": Generates a more readable console output.
  7. "html:target/seleniumreport.html": Generates an HTML test report in the target directory named seleniumreport.html.
  8. public class seleniumTestRunner extends AbstractTestNGCucumberTests {:
This class extends AbstractTestNGCucumberTests, making it a TestNG test class that can execute Cucumber features.