簡體   English   中英

我使用 selenium web 驅動程序 JAVA 將產品添加到手推車,然后將其從購物車中刪除。 我如何斷言產品已被移除?

[英]I have added product to the trolley by using selenium web driver JAVA and I removed it from the cart. How can I assert the product is removed or not?

這是我從手推車或手推車中取出的產品。

public class TrolleyPage(){
     public void removeFromTrolley() {
        List<WebElement> removeProductBtnList = driver.findElements(By.cssSelector("button[data-test='basket-removeproduct']"));
        int size = removeProductBtnList.size();
        System.out.println("Number of size of Added product in trolley " + size);
        WebElement removedWebElement = removeProductBtnList.get(0);
        removedWebElement.click();
    }
}

這是工作。 我已將“產品名稱”存儲在手推車的列表中以驗證手推車中的產品在移除后是否可用,但出現斷言錯誤。

public class TrolleyPage(){
    public List<String> getAllProductsInTrolley() {
        List<String> actualList = new ArrayList<>();
        List<WebElement> productWebElements = driver.findElements(By.cssSelector("a[data-e2e='product-name']"));
        for (WebElement product : productWebElements) {
            String productName = product.getText();
            if (!productName.isEmpty()) {
                actualList.add(productName);
                System.out.println("Product :" + productName);
            }
        }
        return actualList;
    }
}

這是我的實際列表,我想與預期進行比較,我該如何斷言請幫助我

public class RemoveTheProductDefs {

    private TrolleyPage trolleyPage = new TrolleyPage();
    private String expected;


  @When("^I remove a product$")
    public void i_remove_a_product()  {
        trolleyPage.removeFromTrolley();

    }
--------- This is failing-------
    @Then("^I should see the the trolley is empty$")
    public void i_should_see_the_the_trolley_is_empty()  {
        List<String> actualList = trolleyPage.getAllProductsInTrolley();
  
        assertThat(actualList,contains(expected));    }
}

快速而簡單的修復可能是比較 List 的大小。

sizeAfter = sizeBefore - 1

private int sizeBefore;

@Given("^I added products to trolley$")
public void i_added_products_to_trolley()  {
    ...
    sizeBefore = trolleyPage.getAllProductsInTrolley().size();
}

...

@Then("^I should see the number of product in trolley decrease one$")
public void i_should_see_the_number_of_product_in_trolley_decrease_one()  {
    int sizeAfter = trolleyPage.getAllProductsInTrolley().size();
    assertThat(sizeAfter , equalTo(sizeBefore - 1));    
}

暫無
暫無

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

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