简体   繁体   中英

How to check if the product is removed from the cart in selenium java?

I want to test the "REMOVE" button for removing the product from cart inside of cart page. The website is https://www.saucedemo.com/ . I have RemoveItemFromCartTest class for running the test, PurchasePage class for calling the methods for interaction on website, and RemoveItemMethod class for do stuff on the webpage and I have VerificationPage class where I write verification methods for test assertion.

RemoveItemFromCartTest class:

package tests_with_login;
import org.junit.Assert;
import org.junit.Test;
import pages.PurchasePage;
import pages.VerificationPage;

public class RemoveItemFromCartTest extends BaseTestWithLogin {

    public PurchasePage purchasePage;
    public VerificationPage verificationPage;



    @Test
    public void removeItemFromCartPage() throws InterruptedException {
        purchasePage = new PurchasePage(driver);

        purchasePage.removeItemFromCartPage();

        // Test assertion
        try {
            verificationPage.verifyItemRemoveFromCartPage();
            System.out.print("The item is removed.");
        } catch (Exception e) {
            Assert.fail("Something went wrong.");
        }
    }
}

PurchasePage class:

package pages;
import methods.RemoveItemMethod;
import org.openqa.selenium.WebDriver;



public class PurchasePage extends BasePage {

    public PurchasePage(WebDriver driver) {
        super(driver);
    }


    public RemoveItemMethod removeItemMethod;



    public PurchasePage removeItemFromCartPage() throws InterruptedException {
        removeItemMethod = new RemoveItemMethod(driver);
        removeItemMethod.removeItemFromCart();
        return this;
    }
}

RemoveItemMethod class:

package methods;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import pages.BasePage;

public class RemoveItemMethod extends BasePage {

    public RemoveItemMethod(WebDriver driver) {
        super(driver);
    }

    By addToCartBy = By.id("add-to-cart-sauce-labs-backpack");
    By removeButtonBy = By.id("remove-sauce-labs-backpack");
    By shoppingCartBy = By.className("shopping_cart_link");



    // Remove Item from Cart page
    public RemoveItemMethod removeItemFromCart() throws InterruptedException {
        click(addToCartBy);
        click(shoppingCartBy);
        Thread.sleep(1000);
        click(removeButtonBy);
        Thread.sleep(1000);
        return this;
    }
}

VerificationPage class: package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.util.List;

public class VerificationPage extends BasePage {

By inventoryItemBy = By.className("inventory_item_name");

public VerificationPage verifyItemRemoveFromCartPage() {
    String elementTitle = readTextFromElement(inventoryItemBy);
    List<WebElement> cartList = driver.findElements(inventoryItemBy);

    for (WebElement cart : cartList) {
        if (cart.getText().contains(elementTitle)) {
            System.out.print("The product is not removed.");
        }

        System.out.print("The product is removed.");
    }


    if(cartList.contains(elementTitle)) {
        System.out.print("The product is not removed.");
    } else {
        System.out.print("The product is removed.");
    }

    return this;
}
}

So, what I need is to check (assert) if the product has been removed from cart. I tried Assert with various methods and still nothing. Tried trough if statement and for loop, nothing. Tried to make a list of items who are in cart and check if item still in this list after click on remove button, doesn't work. In most of the scenarios, the test go to catch block.

I tried to find solution everywhere and for some reason nothing is work. I don't have a clue what to do because I tried everything. If you have any questions for me please ask me.

What am I doing wrong? Thank you in advance.

You can validate no more products are in the cart with this method:

public boolean waitForElementToDisappear(By element){
    try {
        wait.until((ExpectedConditions.invisibilityOfElementLocated(element)));
        return true;
    }catch (Throwable t){
        return false;
    }
}

So while inside the shopping cart and items are removed the validation assertion may be something like this:

By itemInShoppingCartBy = By.className("cart_item");
Assert.assertTrue(waitForElementToDisappear(itemInShoppingCartBy));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM