java - Selenium - 如何多次将相同的产品添加到购物车
问题描述
我一直试图找到一种方法将相同的产品添加到购物车 3 次,但它不起作用,你能告诉我我缺少什么吗?如您所见,我通过页面对象模式获取元素并导入到测试类。
'''
import java.util.List;
import org.openqa.selenium.WebElement;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import Practice1.test1.BaseClass;
import pageObjects.CartPageObjects;
public class ShopPage1Test extends BaseClass {
//to invoke WebDriver and load website
@BeforeTest()
public void startURL() {
globalDriver();
}
@Test
public void getBase() {
//get the single product which you want to add to cart
WebElement product_to_add_to_cart = CartPageObjects.get_single_product_name();
//Get all your products in a list
List<WebElement> all_products = CartPageObjects.get_All_Products();
//Get all Products count
int products_count = CartPageObjects.get_All_Products().size();
//iterate through products list
for(int i=0; i<products_count;i++) {
if(all_products.get(i).equals(product_to_add_to_cart)) {
//click add to cart element if product found in index
int a=0;
//iterate 3 times and click element
while(a<3){
a++;
//click add to cart element
CartPageObjects.add_to_cart_button().get(i).click();
a=3;
}
}
}
}
@AfterTest
public void aftertests(){
System.out.println("test finished");
}
}
'''
解决方案
推荐阅读
- python - Python/Django - 无法访问通用视图中的链接模型
- python - 填充列表 - Python 分配索引并给出错误
- .htaccess - 将不带扩展名的文件重定向到特定 URL 并忽略带扩展名的文件
- c# - .NET Core API 在 Azure 上部署后无法正常工作
- java - 重构下面的代码以利用非常小的空间,即使是大数据
- android-studio - 无法加载文件:图片/保存的图片/AA16.png - tsx 文件问题
- python - FIX“检查与您的 MySQL 服务器版本相对应的手册以获取在 '%s 附近使用的正确语法”
- c - `(i) = 1` 在标准 C 中是非法的吗?
- reactjs - ReactJS Call Function of another Component react-router
- python - I am trying to rename multiple files programmatically but when i run code it deletes all files from given folder