代码在通过TestNG执行时进入无限搜索

时间:2015-09-29 12:15:53

标签: java selenium testng

以下代码在通过TestNG运行时进入无限搜索,否则在通过Java Application直接在Main方法中执行时会得到正确的结果。

Boolean iselementpresent = driver.findElements(By.linkText("Foreign exchange1")).size()!= 0;

无限搜索 - >

public boolean checkLinkPresence(String linkName){
    Boolean iselementpresent = driver.findElements(By.linkText("Foreign exchange1")).size()!= 0;
    if (iselementpresent == true)
        return true;
    else{
        System.out.print("Element " + linkName + " not Present");
        APP_LOGS.debug("Element " + linkName + " not Present");
        return false;
    }
}

2 个答案:

答案 0 :(得分:0)

您不应该在TestNG中使用main方法,因为所有内容都适用于注释。但是请确保您的隐式等待时间很短,以便在此之后发出命令findElements超时。如果您没有给出隐式等待,请尝试给出一个,然后尝试运行您的方法。这是一个例子 -

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //set this before you get the url using driver.get()

设置隐式等待时间后,尝试运行您的方法。

public boolean checkLinkPresence(String linkName){
    List elements = driver.findElements(By.linkText("Foreign exchange1"));
    if (elements.size() != 0)
        return true;
    else{
        System.out.print("Element " + linkName + " not Present");
        APP_LOGS.debug("Element " + linkName + " not Present");
        return false;
    }
}

希望它有所帮助。

答案 1 :(得分:0)

这就是我写这个函数的方法......

public boolean checkLinkPresence(String linkName)
{
    if (driver.findElements(By.linkText(linkName)).isEmpty())
    {
        System.out.print("Element " + linkName + " not Present");
        APP_LOGS.debug("Element " + linkName + " not Present");
        return false;
    }

    return true;
}

你的定位器里面有一个硬编码的字符串By.linkText("Foreign exchange1"),所以不管你传递了什么字符串,它总是在寻找那个字符串。