gpt4 book ai didi

java - Selenium WebDriver - 使用 Java - 如何检查错误消息在网页中是否可见?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:52:34 26 4
gpt4 key购买 nike

我正在测试一个进行用户错误验证的网页。当网页首次出现时,不应出现任何错误消息,因此我需要检查一下。然后,根据错误(有时是在用户输入数据后点击“提交”之后),我需要验证是否出现了正确的错误消息。

在下面的代码中,首次加载网页时不应出现错误消息,但如果我不输入日期并单击提交按钮,就会出现错误消息。

<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 代码进行测试并使用相同的 Selenium 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 错误消息逻辑并确定它们在网页上是否可见?

谢谢。

最佳答案

我猜你的第一个错误代码需要很长时间的原因是因为你有一个很长一段时间的隐式等待集。隐式等待将等待元素出现指定的时间量。如果错误消息不存在,它将等待超时时间,然后继续执行,这会使您的代码执行缓慢。我会删除隐式等待并在需要时添加显式等待。第二个错误只是说它找不到具有给定 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>

你可以使用像 "span.ng-hide" 这样的 CSS 选择器,这意味着找到一个包含类 (.) ng- 的 SPAN隐藏

A CSS Reference

CSS Selector Tips

关于java - Selenium WebDriver - 使用 Java - 如何检查错误消息在网页中是否可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39535384/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com