2010-07-19 85 views
34

在我写的,如果我想断言WebElement测试出现在页面上,我可以做一个简单:断言一个WebElement不存在使用硒的webdriver与Java

driver.findElement(By.linkText("Test Search")); 

这将通过如果存在,并且如果它不存在就会被炸出来。但是现在我想断言一个链接不是存在。我不清楚如何做到这一点,因为上面的代码不返回布尔值。

编辑这就是我想出我自己的修复方法,我想知道是否还有更好的方法。

public static void assertLinkNotPresent (WebDriver driver, String text) throws Exception { 
List<WebElement> bob = driver.findElements(By.linkText(text)); 
    if (bob.isEmpty() == false) { 
    throw new Exception (text + " (Link is present)"); 
    } 
} 

回答

12

我认为,你可以赶上org.openqa.selenium.NoSuchElementException将由driver.findElement被抛出,如果没有这样的元素:硒的

import org.openqa.selenium.NoSuchElementException; 

.... 

public static void assertLinkNotPresent(WebDriver driver, String text) { 
    try { 
     driver.findElement(By.linkText(text)); 
     fail("Link with text <" + text + "> is present"); 
    } catch (NoSuchElementException ex) { 
     /* do nothing, link is not present, assert is passed */ 
    } 
} 
+0

不错的主意。虽然很奇怪,但没有一种机制可以处理Web驱动程序 – DevDave 2012-07-10 10:31:09

+1

中已有的这种断言,以使您可以传入By.id/cssSelector等而不是String文本。 – Dave 2013-01-09 20:06:31

+1

空的catch块从来都不是一个好主意。 – aimbire 2013-02-06 13:03:42

1
boolean titleTextfield = driver.findElement(By.id("widget_polarisCommunityInput_113_title")).isDisplayed(); 
assertFalse(titleTextfield, "Title text field present which is not expected"); 
2

试试这个 -

private boolean verifyElementAbsent(String locator) throws Exception { 
    try { 
     driver.findElement(By.xpath(locator)); 
     System.out.println("Element Present"); 
     return false; 

    } catch (NoSuchElementException e) { 
     System.out.println("Element absent"); 
     return true; 
    } 
} 
+0

我怀疑这是行不通的。我使用Perl绑定并尝试使用这种方法,问题是当找不到元素时驱动程序实例死亡。不确定Java是否会发生同样的情况。 – user907860 2013-03-01 13:07:49

-1

findElement将检查HTML源代码,并且将返回true,即使没有显示的元素。要检查是否显示元素或不使用 -

private boolean verifyElementAbsent(String locator) throws Exception { 

     boolean visible = driver.findElement(By.xpath(locator)).isDisplayed(); 
     boolean result = !visible; 
     System.out.println(result); 
     return result; 
} 
24

它更容易做到这一点:

driver.findElements(By.linkText("myLinkText")).size() < 1 
+0

谢谢,即使对于非Java绑定,这似乎也是最好的方法。 – user907860 2013-03-01 13:20:57

+1

这是迄今为止避免发生findElement异常的更好的答案。 – DMart 2017-06-12 19:41:14

6

有一个叫ExpectedConditions的类别:

By loc = ... 
    Boolean notPresent = ExpectedConditions.not(ExpectedConditions.presenceOfElementLocated(loc)).apply(getDriver()); 
    Assert.assertTrue(notPresent); 
+0

由于某些原因,这对我不起作用(至少在Selenium'2.53.0'中)。相反,我不得不使用** all **这样的元素:'ExpectedConditions.not(ExpectedConditions.presenceOfAllElementsLocatedBy(locator)));'。 – stiemannkj1 2017-06-20 16:09:08

0

可以utlilize Arquillian Graphene框架为了这。因此,例如,对于你的情况可能是

Graphene.element(By.linkText(text)).isPresent().apply(driver)); 

是还为您提供了不错的API的一堆与阿贾克斯,流畅的等待,网页对象,片段等工作。它绝对简化了基于硒的测试开发。

4

硒的webdriver会是这样的:

assertTrue(!isElementPresent(By.linkText("Empresas en Misión"))); 
0

于Node.js我发现下面几点对等因素不再存在有效的方法:

// variable to hold loop limit 
    var limit = 5; 
// variable to hold the loop count 
    var tries = 0; 
     var retry = driver.findElements(By.xpath(selector)); 
      while(retry.size > 0 && tries < limit){ 
       driver.sleep(timeout/10) 
       tries++; 
       retry = driver.findElements(By.xpath(selector)) 
      } 
+0

如果元素没有消失,那么您的代码可能陷入无限循环 – algot 2017-06-19 14:05:17

+0

非常好的一点。修正了。我不会添加错误处理。 – QualiT 2017-06-19 21:44:43

-1

适用于1.6版。0以上

WebElement button = (new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//XCUIElementTypeButton[@name='your button']")))); 
    button.click(); 

    Assert.assertTrue(!button.isDisplayed()); 
1

看起来如果找到至少一个元素像findElements()只返回快。否则,它会在返回零个元素之前等待隐式等待超时 - 就像findElement()一样。

为了保持测试良好的速度,这个例子临时缩短了等待含蓄,在等待元素消失:

static final int TIMEOUT = 10; 

public void checkGone(String id) { 
    FluentWait<WebDriver> wait = new WebDriverWait(driver, TIMEOUT) 
      .ignoring(StaleElementReferenceException.class); 

    driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS); 
    try { 
     wait.until(ExpectedConditions.numberOfElementsToBe(By.id(id), 0)); 
    } finally { 
     resetTimeout(); 
    } 
} 

void resetTimeout() { 
    driver.manage().timeouts().implicitlyWait(TIMEOUT, TimeUnit.SECONDS); 
} 

还在寻找一种方式来避免超时完全虽然...