我正在測試做一些用戶錯誤驗證的網頁。當網頁第一次出現時,不應該出現錯誤消息,所以我需要檢查。然後,根據錯誤(有時在用戶輸入數據之後單擊「提交」其他時間後),我需要驗證是否顯示了正確的錯誤消息。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錯誤消息的邏輯,並確定它們是否是可見或不可見的網頁上看到?
謝謝。
是出現在同一個容器或不同的所有錯誤消息? –