2016-09-16 43 views
3

我正在測試做一些用戶錯誤驗證的網頁。當網頁第一次出現時,不應該出現錯誤消息,所以我需要檢查。然後,根據錯誤(有時在用戶輸入數據之後單擊「提交」其他時間後),我需要驗證是否顯示了正確的錯誤消息。Selenium WebDriver - 使用Java - 如何檢查錯誤消息是否可見或不在網頁中?

在下面的代碼中,第一次加載網頁時不會顯示錯誤消息,但如果我沒有輸入日期並單擊提交按鈕,應該會顯示錯誤消息。

<div id="showNotIE" style="display: none;"> 
<input id="txtImplantDate" class="ng-pristine ng-untouched ng-empty ng-invalid ng-invalid-required" type="date" required="" placeholder="YYYY-MM-DD" name="txtImplantDate" ng-model="ImplantInsert.ImplantImplantDate"> 
</div> 
<div class="ng-scope" ng-if="showMsgs && (Currentdate < newDate)" style=""> 
<span style="color:red;">Implant Date cannot be greater than today and is required</span> 

使用下面的Java代碼,這似乎正常工作(在IE瀏覽器11的第一檢查過程很長一段時間,但它似乎工作)。

//Confirming text is not visible 
boolean isPresent = driver.findElements(By.xpath(textLocator)).size() > 0; 
if (isPresent) { 
//Write to log that text is present (FAIL) 
} else { 
//Write to log that text is not present (PASS) 
} //end if 

此代碼也似乎工作:

//Confirming text is not visible 
boolean isEmpty = driver.findElements(By.xpath(textLocator)).isEmpty(); 
if (isEmpty) { 
    //Write to log that text is not present (PASS) 
} else { 
     //Write to log that text is present (FAIL) 
} //end if 

然而,當我測試的此HTML代碼,並使用相同的硒的webdriver Java的邏輯測試,我得到錯誤的結果。

<select id="selPatientTypes" class="fontInput ng-pristine ng-untouched ng-empty ng-invalid ng-invalid-required" required="" name="selPatientTypes" ng-options="n.PatienTypeID as n.PatientTypeDescription for n in scPatientTypes | filter:FilterPatientTypes" ng-model="ImplantInsert.ImplantPatientTypeID"> 
<option class="" value="" selected="selected">-- Please select Patient Type --</option> 
<option label="Adult" value="number:1">Adult</option> 
<option label="Pediatric" value="number:2">Pediatric</option> 
</select> 
<span class="ng-hide" style="color:red;" ng-show="showMsgs && ImplantForm.selPatientTypes.$error.required">Patient Type is required</span> 

如果我嘗試使用這個「isDisplayed」代碼,Java錯誤了。

try { 
    boolean xpathIsDisplayed = driver.findElement(By.xpath(fieldLocator[value])).isDisplayed(); 
    // Write to log that text is present (FAIL) 
} catch (Error e) { 
    //Write to log that text is not present (PASS) 

} //end try 

的錯誤信息是:

org.openqa.selenium.NoSuchElementException: Unable to find element with xpath == //div[2]/table[2]/tbody/tr[1]/td[2]/div[3]/span (WARNING: The server did not provide any stacktrace information) 
Command duration or timeout: 30.06 seconds 
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html (BTW: This URL doesn’t provide any useful information) 

這是錯誤的邏輯另一類型的網頁上使用。

<input id="txtPatientBSA" class="fontInput ng-pristine ng-untouched ng-valid ng-empty ng-valid-maxlength" oninput="this.value = this.value.replace(/[^0-9.]/g, ''); this.value = this.value.replace(/(\..*)\./g, '$1'); " title="Patient BSA should be in range of 0 to 5.00 (X.XX)" ng-keyup="ValidateBSA()" style="direction: rtl" max="5.00" min="0.00" maxlength="4" size="4" ng-model="ImplantInsert.ImplantPatientBSA" name="txtPatientBSA" placeholder="X.XX"> 
<span id="ErrorMsgBSA" class="error error-keyup-1 ng-hide" style="color:red;" ng-show="ShowErrorMsgBSA"> Patient BSA should be in range of 0 to 5.00 (X.XX)</span> 

有誰知道是否有一種方法來檢查所有類型的HTML錯誤消息的邏輯,並確定它們是否是可見或不可見的網頁上看到?

謝謝。

+0

是出現在同一個容器或不同的所有錯誤消息? –

回答

-2

您應該在try catch塊中添加findElement,並使用findElement代替findElements(Xpath是唯一的)。 用於施工。重點是你的元素可以在那裏,但硒可能無法立即找到它。所以你需要多檢查一次,並在每次迭代後等待一會兒。

WebElement element = null; 
    boolean flag = false; 
     for (int i = 0; i< 10; i++) {// iterating 10 times 
      try { 
       element = driver.findElement(By.xpath("yourXpath")); 
       if (element != null) { 
        flag = true; 
        break; 
       } 
      } catch (Exception e) { 
       //you can log or whatever you want 
      } 
      pause(1000); //after iteration wait for a second , 
         //so it will give a time to load the DOM 
     } 
if(flag){ 
    //your code goes here 
}else{ 
    //your code goes here 
} 
+1

XPath並不總是唯一的......它取決於XPath和頁面的HTML。無論有多少元素匹配,'.findElement'只會返回第一個元素。這個函數不是必須的.​​.....你應該使用'WebDriverWait'和'ExpectedConditions'並等待元素可見。 – JeffC

+0

我開始加載這個邏輯之前需要等待5秒才能加載整個頁面,所以我認爲這不會起作用。 –

+0

@JeffC如果使用Webdriver,你應該知道元素存在的情況很多,但是它不能被selenium發現,所以你甚至不得不使用JS,因爲其他方法不起作用。告訴我一個情況,你有兩個不同的元素具有相同的xpath,它很有趣... – Arno

3

我猜你的第一個錯誤代碼需要很長時間的原因是因爲你有一個隱含的等待設置了很長時間。隱式等待將等待一個元素出現的指定時間量。在不存在的錯誤消息的情況下,它將等待超時時間,然後繼續前進,這會使代碼執行緩慢。我會刪除隱含的等待,並在需要時添加顯式等待。第二個錯誤只是說它找不到具有給定XPath的元素。您可能需要更新您的XPath或使用另一個定位器。

這裏是一個Java函數,您可以將所需的定位器傳入,如果元素存在並且可見,它將返回true。

/** 
* Returns whether an element is visible 
* 
* @param locator the locator to find the desired element 
* @return true if the element exists and is visible, false otherwise 
*/ 
public boolean IsVisible(By locator) 
{ 
    List<WebElement> elements = driver.findElements(locator); 
    if (elements.isEmpty()) 
    { 
     // element doesn't exist 
     return false; 
    } 
    else 
    { 
     // element exists, check for visibility 
     return elements.get(0).isDisplayed(); 
    } 
} 

對於下面的元素,

<span id="ErrorMsgBSA" class="error error-keyup-1 ng-hide" style="color:red;" ng-show="ShowErrorMsgBSA"> Patient BSA should be in range of 0 to 5.00 (X.XX)</span> 

您可以使用定位器,By.id("ErrorMsgBSA")

對於下面的元素,

<span class="ng-hide" style="color:red;" ng-show="showMsgs && ImplantForm.selPatientTypes.$error.required">Patient Type is required</span> 

你可以使用一個CSS選擇器一樣,"span.ng-hide"這只是意味着找到一個SPAN包含類(。)ng-hide

A CSS Reference

CSS Selector Tips

+0

這個邏輯不報錯了,看來當錯誤消息文本不存在時工作,但當錯誤文本出現時,「isDisplayed()」正在讀取「False」。 –

+0

我認爲您關於使用另一個定位器的聲明可能會起作用 –

+0

我想要搜索cssSelector,但我不知道如何創建地址,也許通過查看「ng-show」的值來看看它是什麼意思,可以解決問題,或者class =「ng-hide」... –

相關問題