2013-05-07 69 views
2

Boolean变量的定义中的webdriver作为后盾:布尔变量中的webdriver

boolean r1 = selenium.isTextPresent("something"); 

while循环使用这一点。我想将我的代码,代码的webdriver和我想:

boolean jr1 = driver.findElement(By.linkText("something")) != null; 

但它只能在文字存在和价值是true。当值应为false我的变量返回我收到的控制台错误:

Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Unable to locate element: {"method":"link text","selector":"something"} 

你能给我什么建议,我怎么能做到这一点?

回答

2

我通过编写返回true或false的isElementPresent方法来解决这个问题。 编辑 - 刚刚在另一个答案中看到了这个确切的方法,我不再相信我写了它。显然,我发现它很久以前,并已使用它很长时间,感觉就像我的!所有信用卡原作者

​​

然后,您可以使用:

boolean jr1 = isElementPresent(By.linkText("Something")); 

添加这种方法的基类,并从其扩展您的测试类,或将其添加到您的每个测试类。

0

如果元素没有linktext'something'Webdriver总会返回异常,我很害怕。

您是否尝试了预期条件?

try { 
     wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("something") 
     Boolean jr1 = True 
    } 
catch (Exception e) { 
     Boolen jr1 = False 
    } 

这是一个相当粗糙实现,但在这种情况下,我将尽力找到元素,如果它发现它,真。如果请求超时,False。

0

对我来说最直接的实现isTextPresent作为webdriver的代码如下:

public boolean isTextPresent(String text) { 
    String allText = webDriver.findElement(By.tagName("body")).getText(); 
    boolean isTextPresent = allText.contains(text); 
    return isTextPresent; 
} 
0

我身边这让使用以下方法:

  1. 起初我设定布尔味精的值不显示为假
  2. 等待50秒,直到元素可点击
  3. 如果表格中的文本等于显示的文本即no记录然后返回true
  4. 否则返回false

    公共布尔isRecordDisplayed(字符串sDisplayText){ 布尔isNoRecordDisplayedMsg = FALSE; (WebDriverWait(webDriver,50).until(ExpectedConditions.elementToBeClickable(table)));

     if (search.getText().equals(sDisplayText)) { 
          isNoRecordDisplayedMsg = true; 
         } 
         return isRecordHostDisplayedMsg; 
        } 
    

    By table = By.xpath(“// div [contains(@ class,'record')] // th”);