Integrated with Selenum Driver(Example2)

 Feature: Advanced Search


@Logo

Scenario: Advanced Search Logo Check

Given Iam on advance seach page

When click on Ebay Logo

Then I am navigated to Ebay Homepage Again

@Itemscount

Scenario: search items count

Given Iam on Ebay Homepage

When I search for iphone11

Then I validate atleast 1000 search items present



==========


package testRunner;


import org.junit.runner.RunWith;

import io.cucumber.junit.Cucumber;

import io.cucumber.junit.CucumberOptions;


@RunWith(Cucumber.class)

@CucumberOptions(

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

glue = "Steps",

tags ="@Logo or @Itemscount",

plugin = {

"pretty",

"html:target/cucumber-reports.html",

"json:target/cucumber.json"

},

monochrome = true

)

public class AdvanceSearchRunner {

}



package Steps;


import static org.testng.Assert.fail;


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.Then;

import io.cucumber.java.en.When;


public class Ebay_AdvanceSearch {


WebDriver driver;

@Given("Iam on advance seach page")

public void iam_on_advance_seach_page() throws InterruptedException {

driver = new ChromeDriver();

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

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

Thread.sleep(2000);

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

Thread.sleep(2000);

}

@When("click on Ebay Logo")

public void click_on_ebay_logo() {

driver.findElement(By.id("gh-logo")).click();

}

@Then("I am navigated to Ebay Homepage Again")

public void i_am_navigated_to_ebay_homepage_again() {

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

String orgurl=driver.getCurrentUrl();

if(expurl.equals(orgurl)) {

System.out.println("iam in ebay homepage");

}else {

System.out.println("iam not in ebay homepage");

}

driver.quit();

}

@Given("Iam on Ebay Homepage")

public void IamonEbayHomepage() throws InterruptedException {

driver = new ChromeDriver();

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

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

Thread.sleep(2000);

}

@When("I search for iphone11")

public void Isearchforiphone11() {

driver.findElement(By.id("gh-ac")).sendKeys("iphone11");

driver.findElement(By.className("gh-search-button__label")).click();

}

@Then("I validate atleast 1000 search items present")

public void Ivalidateatleast1000searchitemspresent() {

String itemcount = driver.findElement(By.cssSelector("div[class='srp-controls__control srp-controls__count'] span:nth-child(1)")).getText().trim();

String itemcount2 = itemcount.replace(",", "");

int itemcountInt = Integer.parseInt(itemcount2);

if((itemcountInt<=1000)) {

fail("item count less than 1000 shown");

}


/*

* String itemcount = driver.findElement(By.

* cssSelector("div[class='srp-controls__control srp-controls__count'] span:nth-child(1)"

* )).getText(); int itemcountInt = Integer.parseInt(itemcount);

*

* if((itemcountInt>=1000)) { fail("item count less than 1000 shown");

*

* }

*

*

* Error--->java.lang.NumberFormatException: For input string: "1,700"

*/

driver.quit();

}

}



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


String itemcount = driver.findElement(By.cssSelector("div[class='srp-controls__control srp-controls__count'] span:nth-child(1)")).getText().trim(); This line uses the WebDriver to find an HTML element that matches the specified CSS selector. It retrieves the text content of that element, which represents the item count, and trims any leading or trailing whitespace. String itemcount2 = itemcount.replace(",", ""); Here, we remove any commas from the item count string. This is crucial because the presence of commas would prevent successful conversion to an integer. int itemcountInt = Integer.parseInt(itemcount2); The cleaned string is then converted into an integer. This step is essential for performing numerical comparisons. /* * Error--->java.lang.NumberFormatException: For input string: "1,700" */ This error occurs when attempting to parse a string that contains commas, which is not a valid integer format. The code effectively addresses this issue by cleaning the string before conversion.