2010-07-19 68 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); 
} 

還在尋找一種方式來避免超時完全雖然...