Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,11 @@ public WebElement getProductByName(String productName)

public void addProductToCart(String productName)
{
WebElement prod = getProductByName(productName);
prod.findElement(addToCart).click();
By productAddToCart = By.xpath("//div[contains(@class,'mb-3') and .//b[text()='" + productName + "']]//button[last()]");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Escape product names in XPath locator

Building the XPath with "...text()='" + productName + "'..." makes addProductToCart fail for valid product names containing a single quote (for example, Levi's), because the resulting XPath becomes syntactically invalid and Selenium throws before the click can happen. This is a regression from the previous Java string comparison path, which handled quoted names correctly, so the locator should escape XPath literals (or avoid direct XPath string interpolation).

Useful? React with πŸ‘Β / πŸ‘Ž.

waitForElementToAppear(productAddToCart);
driver.findElement(productAddToCart).click();
waitForElementToAppear(toastMessage);
waitForElementToDisappear(spinner);


}

}
6 changes: 1 addition & 5 deletions src/test/java/org/miteshdandade/tests/StandAloneTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ public static void main(String[] args) {
driver.findElement(By.id("login")).click();
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(5));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".mb-3")));
List<WebElement> products = driver.findElements(By.cssSelector(".mb-3"));

WebElement prod = products.stream().filter(product->
product.findElement(By.cssSelector("b")).getText().equals(productName)).findFirst().orElse(null);
prod.findElement(By.cssSelector(".card-body button:last-of-type")).click();
driver.findElement(By.xpath("//div[contains(@class,'mb-3') and .//b[text()='" + productName + "']]//button[last()]")).click();


wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#toast-container")));
Expand Down