簡體   English   中英

如何檢查產品是否從 selenium java 中的購物車中移除?

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

我想測試用於從購物車頁面內的購物車中刪除產品的“刪除”按鈕。 該網站是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.");
        }
    }
}

購買頁面類:

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

所以,我需要的是檢查(斷言)產品是否已從購物車中移除。 我用各種方法嘗試了斷言,但仍然沒有。 嘗試過 if 語句和 for 循環,什么都沒有。 試圖列出購物車中的商品列表,並在單擊刪除按鈕后檢查商品是否仍在此列表中,但不起作用。 在大多數情況下,測試會轉到 catch 塊。

我試圖到處尋找解決方案,但出於某種原因,沒有任何效果。 我不知道該怎么做,因為我嘗試了所有方法。 如果你對我有任何問題,請問我。

我究竟做錯了什么? 先感謝您。

您可以使用此方法驗證購物車中沒有更多產品:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM