This my testng class:
public class EcommerceTest
{
public static WebDriver driver;
@Test
public void addtoCartTest() throws InterruptedException
{
driver.get("https://rahulshettyacademy.com/seleniumPractise/");
driver.manage().window().maximize();
EkartPage1 oekart = new EkartPage1(driver);
oekart.AddtoCart();
}
@BeforeTest
public void beforeTest() {
System.setProperty("webdriver.chrome.driver", "E:\\selenium\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
@AfterTest
public void afterTest() {
//driver.close();
}
}
/**/This my page object class**
public class EkartPage1
{
WebDriver driver;
WebDriverWait wait;
@FindBy(xpath = "//button[contains(text(),'ADDED')]")
WebElement addedBtn;
public EkartPage1(WebDriver driver)
{
wait = new WebDriverWait(driver, 30);
PageFactory.initElements(driver, this);
this.driver = driver;
}
**//This is my method to click Add to cart button**
public void AddtoCart() throws InterruptedException /
{
String[] additems = {"Cucumber","Beans"};
List<WebElement> list = driver.findElements(By.cssSelector("h4.product-name"));
for(int i=0;i<list.size();i++)
{
String[] productname = list.get(i).getText().split("-");
String frmtdname = productname[0].trim();
List itemsneeded = Arrays.asList(additems);
if(itemsneeded.contains(frmtdname))
{
List<WebElement> list2 =driver.findElements(By.xpath("//button[text() ='ADD TO CART']"));
list2.get(i).click();
System.out.println("One product added");
}
}
}
I am trying to click on 'Add to cart' for the product 'Beans**.But the selenium webdriver clicks on 'Add to cart' button corresponding to 'Brinjal' which is the next immediate product.Kindly help me to resolve this issue.
Beans - 1 Kg
82