“返回”未返回调用函数

时间:2019-01-09 04:21:47

标签: java loops return

我想在带有主题行的收件箱中搜索电子邮件,电子邮件需要花费一些时间才能进入邮箱。如果在迭代过程中未找到电子邮件,请等待一段时间,然后再次搜索电子邮件,当找到的电子邮件返回true时;

public boolean searchForEmail(String MySubject, String linkAddress, String attachment) {
    boolean flag = false;
    List<WebElement> unreademeil = driver.findElements(By.xpath(""));
    for (int i = 0; i < unreademeil.size(); i++) {
        if ((unreademeil.get(i).isDisplayed()) == true) {
            System.out.println(unreademeil.get(i).getText());
            if (unreademeil.get(i).getText().contains(MySubject)) {
                unreademeil.get(i).click();
                flag=true;
                break;
            }
        }
    }
    if (!flag) {
        System.out.println("No mail with Subject ::--:: " + MySubject);
        while (counter < 40 && !flag) {
            sleep(10000);
            counter++;
            cloop();
        }
        SoftAssertor.asserTrue(isElementPresent("//*[contains(@text()='" + MySubject + "')]"),
                "Failed to search a email with Subject line  ::: " + MySubject);
    }
return flag; 
}



public void cloop(){
searchForEmail(SubjectLine, seeDetailsLink, "xpath");
}

以上程序将搜索带有主题行的电子邮件。有时需要一些时间才能收到电子邮件。因此程序在睡眠后继续搜索(10000)。

在return语句中出现问题,即使在不满足while循环的条件下,“ return flag”语句在while循环下也重定向到“ cloop()”,即“ flag == true”; 由于此程序进入无限循环。请帮助

1 个答案:

答案 0 :(得分:0)

重构是一种解决方案:

public boolean searchForEmail(String MySubject, String linkAddress, String attachment) {
        boolean flag = false;
        sleep(200);
        List<WebElement> unreademeil = driver.findElements(By.xpath("//*[@class='bqe']"));
        for (int i = 0; i < unreademeil.size(); i++) {
            if ((unreademeil.get(i).isDisplayed()) == true) {
                System.out.println(unreademeil.get(i).getText());
                if (unreademeil.get(i).getText().contains(MySubject)) {
                    unreademeil.get(i).click();
                    flag=true;
                    break;
                }
            }
        }
        return flag=true
    }

    public boolean isSearchPassed(String MySubject, String linkAddress, String attachment){
        boolean flag= false;
        int count =0;
        while (!searchForEmail( MySubject, linkAddress,  attachment) && (count< 45)) {
            sleep(10000);count++;   
        }
        return flag=true;
    }