首页 > 解决方案 > 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");
            }

        }     
    '''

标签: javaseleniumpom.xml

解决方案


推荐阅读