我已经使用硒网络驱动程序JAVA将产品添加到手推车中,并将其从手推车中删除.如何断言产品是否已移除



这是我从手推车或推车中取出产品。

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

最新更新