如何在Selenium 2中编写带有参数的方法?

如何在Selenium 2中编写带有参数的方法?,selenium,selenium-webdriver,junit,pageobjects,Selenium,Selenium Webdriver,Junit,Pageobjects,测试此站点: 我的测试验证be可以从购物车中添加和删除产品 我编写了一个没有参数的方法,如下所示: public AllProductPage chooseProduct() { //Click on product iPhone5 driver.findElement(By.className("wpsc_buy_button")).click(); //Expected: Product "iPhone5" has been opened

测试此站点:

我的测试验证be可以从购物车中添加和删除产品

我编写了一个没有参数的方法,如下所示:

public AllProductPage chooseProduct() {
        //Click on product iPhone5
        driver.findElement(By.className("wpsc_buy_button")).click();
        //Expected: Product "iPhone5" has been opened
    return new AllProductPage(driver);
    }
    @Test
    public void verifyThatBeCanAddAndRemoveTheProductFromCart() throws InterruptedException {

        ImplicitWait(driver);

        HomePage onHomePage = new HomePage(driver);
        System.out.println("Step 1");
        AllProductPage onAllProductPage = onHomePage.clickOnAllProduct();
        System.out.println("Step 2");
        onAllProductPage.chooseProduct("iPhone 5");
        onAllProductPage.buttonGoToCheckout();
        onAllProductPage.submitForm();
        System.out.println("Step 3");
        Assert.assertTrue(onAllProductPage.getMessage().contains("Oops, there is nothing in your cart."));
    }
我需要编写一个带有参数的方法,并在测试中选择产品,而不是在我编写的代码中选择产品

@Test
    public void verifyThatBeCanAddAndRemoveTheProductFromCart() throws InterruptedException {

        ImplicitWait(driver);

        HomePage onHomePage = new HomePage(driver);
        System.out.println("Step 1");
        AllProductPage onAllProductPage = onHomePage.clickOnAllProduct();
        System.out.println("Step 2");
        onAllProductPage.chooseProduct();
        onAllProductPage.buttonGoToCheckout();
        onAllProductPage.submitForm();
        System.out.println("Step 3");
        Assert.assertTrue(onAllProductPage.getMessage().contains("Oops, there is nothing in your cart."));
    }

我不知道要传递给函数的数据类型,但可以尝试传递包含产品名称的字符串,如下所示:

public AllProductPage chooseProduct(String productName) {
    //Click on product received in parameter
    driver.findElement(By.xpath("//div[contains(@class,'productcol')][descendant::*[contains(text(),'"+productName+"')]]//input[@class='wpsc_buy_button']")).click();
    //Expected: Product has been opened
    return new AllProductPage(driver);
}
您的测试可能如下所示:

public AllProductPage chooseProduct() {
        //Click on product iPhone5
        driver.findElement(By.className("wpsc_buy_button")).click();
        //Expected: Product "iPhone5" has been opened
    return new AllProductPage(driver);
    }
    @Test
    public void verifyThatBeCanAddAndRemoveTheProductFromCart() throws InterruptedException {

        ImplicitWait(driver);

        HomePage onHomePage = new HomePage(driver);
        System.out.println("Step 1");
        AllProductPage onAllProductPage = onHomePage.clickOnAllProduct();
        System.out.println("Step 2");
        onAllProductPage.chooseProduct("iPhone 5");
        onAllProductPage.buttonGoToCheckout();
        onAllProductPage.submitForm();
        System.out.println("Step 3");
        Assert.assertTrue(onAllProductPage.getMessage().contains("Oops, there is nothing in your cart."));
    }

请注意,在本例中,产品在测试代码中是固定的,但它也可以是一个变量。

这是我现在的错误:org.openqa.selenium.NoSuchElementException:没有这样的元素:无法定位元素:{“方法”:“xpath”,“选择器”:”//div[contains(@class,'productcol')][后代::*[contains(text(),'iPhone5')]//input[@name='wpsc\u ajax\u action']“}而不是'iP‌​在xpath中按5“使用”iPhone 5。传递给函数的文本需要与页面中显示的完全匹配。这是错误:org.openqa.selenium.ElementNotVisibleException:element NotVisible抱歉,xpath确实定位了错误的输入。我编辑了答案,现在xpath是正确的。