- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在测试一个进行用户错误验证的网页。当网页首次出现时,不应出现任何错误消息,因此我需要检查一下。然后,根据错误(有时是在用户输入数据后点击“提交”之后),我需要验证是否出现了正确的错误消息。
在下面的代码中,首次加载网页时不应出现错误消息,但如果我不输入日期并单击提交按钮,就会出现错误消息。
<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
隐藏
关于java - Selenium WebDriver - 使用 Java - 如何检查错误消息在网页中是否可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39535384/
我想在 Watir webdriver 中使用 selenium webdriver Actions。这可能吗? 也可以在 watir webdriver 中使用 java 代码。请帮忙。 我浏览了很
我正在使用 watir-webdriver 浏览我的网站并在不同的浏览器中抓取屏幕截图。 有时在 IE 中截取的屏幕截图大小合适,但颜色完全是黑色。同时运行的 Firefox 测试看起来很好。 bro
我已经编写了 driver.findElement(By.id("kfiDocumentLink")).click(); 用于单击“KFI 文档”按钮的代码。 请找到HTML代码。 Download
我有一个包含以下内容的 html 页面: This is Login page. Please click below link
我想获得页面加载异常,但仍然没有结果。 我使用implicitlyWait 设置计时器以抛出异常。 WebDriver driver = new FirefoxDriver(); driver.man
我正在使用具有 IE 特定应用程序的 Selenium Webdriver。我知道我们可以截取执行的截图。同样,是否有任何选项可以将 selenium 执行记录为视频? 最佳答案 WebDriver
Selenium WebDriver 如何克服同源策略? Selenium RC 中存在同源策略问题 最佳答案 First of all “Same Origin Policy” is introdu
我将如何从输入文件中提取文本?我尝试使用 XPath/CSSSelector 但我得到一个空文本,因为它是一个输入字段。 这是我的 html 代码: 结果:195 行中的 1 到 50
如何使用 WebDriver 自动验证码? 是否有其他方法可以使用 Webdriver 自动执行验证码? 最佳答案 您只能使用“alt”属性中的显示验证码值来自动化验证码。 在 WebElement
最近我开始学习 WebDriver,因为我工作的客户计划使用 WebDriver 来自动化 Web 应用程序。 我怀疑 WebDriver 如何在网页上定位其 ID 动态变化的元素(比如每次登录应用程
我发现 watir-webdriver 在一个非常大的页面上通过正则表达式定位元素非常慢,至少在 FF 8.0.1 中对我来说是这样。航类搜索结果页面示例(包含大约 50 个搜索结果,每个都是 htm
我有一个动态更改其文本的字段。我需要一种方法来等待文本被更改。我不知道会出现什么文本,但我知道当前那里有什么文本。所以我想等待它在元素中消失。有办法吗? 最佳答案 你可以试试ExpectedCondi
自从我使用 Firefox 升级到 3.0 beta 后,我就有了这个异常(exception)。 Exception in thread "main" java.lang.IllegalStateE
任何人都可以帮助我使用 Selenium webdriver 截取整页屏幕截图。我正在使用 c#/Nunit。我正在使用的当前方法不是完整的浏览器页面。 我正在使用下面的代码截取屏幕截图。 publi
我通过 WebDriver (Chrome) 从网页下载图像 // STEP 1 $driver->get($link); // STEP 2 $els=$driver->findElements(W
Selenium WebDriver 的默认隐式等待值是什么? selenium 文档说它是“0”,但是当我在一个全新的项目上调用 .findElement 时,DOM 上不存在元素,它似乎在一段时间
我正在使用 Webdriver 测试 Web 应用程序,大致如下所述。当测试通过时,一切都很好。但是,当其中一个测试失败时,我注意到以下 2 个问题。 a) 如果一个测试失败,则套件中的其余测试将超时
我正在使用 Selenium WebDriver 并遇到问题。 在 UI 中,WebDriver 可以看到元素,但无法执行任何操作,例如单击、键入、选择等。元素由 selenium 找到并作为 web
我在 Java 中使用 Web 驱动程序处理 UntrustedSSLcertificates 时陷入困境。 我创建了 Firefox 配置文件,如: FirefoxProfile profile =
选择的编程语言是 Java。我已经用 Java 编写了一个方法,我将 WebDriver 作为参数传递给它... public boolean myMethod(WebDriver webDriver
我是一名优秀的程序员,十分优秀!