如何使用java / selenium在for循环中添加条件

时间:2018-06-18 06:00:49

标签: java selenium

我想点击添加按钮添加项目,点击添加按钮后如果显示错误则应删除添加的项目再次点击添加按钮添加项目

这是我的代码:

    driver.findElement(By.id("addItem")).click(); // it will add item 

    WebElement errrmsg = driver.findElement(By.id("sameAMC-invalid"));


     /* if error is displayed then it should remove added item and should
      *again add another item again clicking on add item button and should again check
      *whether it is showing error message or not if not then else condition should get
      *execute*/

    if(errrmsg.isDisplayed()) 
    {
        driver.findElement(By.id("remove-Itemadd")).click();
        Thread.sleep(2000);
    }
    else
    {
        driver.findElement(By.id("button-payment")).click(); 
        Thread.sleep(6000);

    }

我想在发生错误时放置循环条件,也就是它应该再次点击添加项目按钮,如果没有它应该检查是否显示任何错误然后它应该在其他情况下执行.....我不要我不知道怎么做......需要帮助

3 个答案:

答案 0 :(得分:0)

您可以使用while,如下所示。但它会进入无限循环,如果你知道最大计数。我们也可以包括检查条件。

driver.findElement(By.id("addItem")).click(); // it will add item 

WebElement errrmsg = driver.findElement(By.id("sameAMC-invalid"));

while(errrmsg.isDisplayed()) 
{
    driver.findElement(By.id("remove-Itemadd")).click();
    Thread.sleep(2000);
    driver.findElement(By.id("addItem")).click(); // it will add item
    errrmsg = driver.findElement(By.id("sameAMC-invalid")); 
}

driver.findElement(By.id("button-payment")).click(); 
Thread.sleep(6000);

答案 1 :(得分:0)

尝试以下代码:使用 findElements 代替 findElement

     driver.findElement(By.id("addItem")).click();
     while(true) {
        List<WebElement> errMsg =   UI.getdriver().findElements(By.id("sameAMC-invalid"));
        if(errMsg.size()==1) {
                driver.findElement(By.id("remove-Itemadd")).click();
                Thread.sleep(2000);
                driver.findElement(By.id("addItem")).click(); 
        }
        else if(errMsg.size() == 0 ) {
            driver.findElement(By.id("button-payment")).click(); 
            Thread.sleep(6000);
            break;
        }
    }  

注意:如果没有错误信息,则@Murthi提供的代码将失败。

答案 2 :(得分:0)

您可以尝试下一个:

private By removeButtons = By.xpath("remove-Itemadd");
private By addButtons = By.xpath("addItem");
private By errorMsg = By.xpath("sameAMC-invalid");
private By payButton = By.id("button-payment");

private boolean isErrorOccur() {
    try {
        return driver.findElement(errorMsg).isDisplayed();
    } catch (org.openqa.selenium.NoSuchElementException ex) {
        return false;
    }
}

public void someMethod() {
    List<WebElement> addItemsButtons = driver.findElements(addButtons);
    List<WebElement> deleteItemsButtons = driver.findElements(removeButtons);

    //it will be works if this add/remove buttons are enabled for all items - list size is the same
    for (int i = 0; i < addItemsButtons.size(); i++) {
        addItemsButtons.get(i).click();
        if (isErrorOccur()){
            deleteItemsButtons.get(i).click();
            //here need some wait, possibly
        }
    }
    driver.findElement(payButton).click();
    //here need some wait, possibly 

}