【问题标题】:How to check if the product is removed from the cart in selenium java?如何检查产品是否从 selenium java 中的购物车中删除?
【发布时间】:2021-09-24 06:01:32
【问题描述】:

我想测试从购物车页面内的购物车中删除产品的“删除”按钮。该网站是https://www.saucedemo.com/。我有用于运行测试的 RemoveItemFromCartTest 类,用于调用网站交互方法的 PurchasePage 类,以及用于在网页上执行操作的 RemoveItemMethod 类,并且我有 VerificationPage 类,用于编写测试断言的验证方法。

RemoveItemFromCartTest 类:

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 类:

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 类:

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 类: 打包页面;

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;
}
}

所以,我需要检查(断言)产品是否已从购物车中删除。我用各种方法尝试了 Assert,但仍然没有。尝试通过 if 语句和 for 循环,没有。试图列出购物车中的物品并在单击删除按钮后检查物品是否仍在此列表中,但不起作用。在大多数情况下,测试都会进入 catch 块。

我试图到处寻找解决方案,但由于某种原因,没有任何工作。我不知道该怎么做,因为我什么都试过了。如果你有什么问题可以问我。

我做错了什么?提前谢谢你。

【问题讨论】:

    标签: java selenium maven pom.xml


    【解决方案1】:

    您可以使用此方法验证购物车中没有更多产品:

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

    因此,当购物车和商品被移除时,验证断言可能是这样的:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-17
      • 2022-08-23
      • 1970-01-01
      • 2022-06-16
      • 2019-10-12
      • 2021-05-15
      相关资源
      最近更新 更多