我一直试图找到一种方法可以将相同的产品添加到购物车3次,但是它不起作用,您能否告诉我我所缺少的东西。如您所见,我正在通过 Page Object Pattern 获取元素并将其导入测试类。
'''
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");
}
}
'''