Web applications Selenium在自定义错误页上的isTextPresent()上引发异常
在selenium测试中,我打开了一个我没有权限访问的页面,我想验证是否显示了错误页面而不是打开的页面 检查此错误页是否简单:Web applications Selenium在自定义错误页上的isTextPresent()上引发异常,web-applications,jboss,selenium,integration-testing,Web Applications,Jboss,Selenium,Integration Testing,在selenium测试中,我打开了一个我没有权限访问的页面,我想验证是否显示了错误页面而不是打开的页面 检查此错误页是否简单: Assert.assertTrue(selenium.isTextPresent("HTTP Status 403")); 现在我通过web.xml中的标记将默认的JBoss错误页面更改为自定义页面。之后,硒就不起作用了。它从isTextPresent()引发异常。当然,页面上的文字也发生了变化,但这在这里并不重要 Stacktrace是: com.thoughtwo
Assert.assertTrue(selenium.isTextPresent("HTTP Status 403"));
现在我通过web.xml
中的
标记将默认的JBoss错误页面更改为自定义页面。之后,硒就不起作用了。它从isTextPresent()
引发异常。当然,页面上的文字也发生了变化,但这在这里并不重要
Stacktrace是:
com.thoughtworks.selenium.SeleniumException: ERROR: Command execution failure. Please search the forum at http://clearspace.openqa.org for error details from the log window. The error message is: doc.style is undefined
at com.thoughtworks.selenium.HttpCommandProcessor.throwAssertionFailureExceptionOrError(HttpCommandProcessor.java:97)
at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:91)
at pl.softwaremill.common.test.web.selenium.screenshots.ScreenshotHttpCommandProcessor.doCommand(ScreenshotHttpCommandProcessor.java:31)
at com.thoughtworks.selenium.DefaultSelenium.captureEntirePageScreenshot(DefaultSelenium.java:679)
at pl.softwaremill.common.test.web.selenium.AbstractSeleniumTest.captureScreenshot(AbstractSeleniumTest.java:166)
at pl.softwaremill.common.test.web.selenium.AbstractSeleniumTest$1.doScreenshot(AbstractSeleniumTest.java:95)
... 27 more
com.thoughtworks.selenium.SeleniumException: ERROR: Couldn't access document.body. Is this HTML page fully loaded?
at com.thoughtworks.selenium.HttpCommandProcessor.throwAssertionFailureExceptionOrError(HttpCommandProcessor.java:97)
at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:91)
at com.thoughtworks.selenium.HttpCommandProcessor.getString(HttpCommandProcessor.java:262)
at com.thoughtworks.selenium.HttpCommandProcessor.getBoolean(HttpCommandProcessor.java:335)
at pl.softwaremill.common.test.web.selenium.screenshots.ScreenshotHttpCommandProcessor.getBoolean(ScreenshotHttpCommandProcessor.java:92)
at com.thoughtworks.selenium.DefaultSelenium.isTextPresent(DefaultSelenium.java:499)
at za.co.fnb.commercial.dms.uitests.view.debtor.DebtorInsuranceAnnexurePage.<init>(DebtorInsuranceAnnexurePage.java:18)
at za.co.fnb.commercial.dms.uitests.view.debtor.UploadDebtorFilesPage.submit(UploadDebtorFilesPage.java:45)
at za.co.fnb.commercial.dms.uitests.view.debtor.UploadDebtorFilesSeleniumTest.shouldStoreFileEvenIfParseFails(UploadDebtorFilesSeleniumTest.java:145)
com.thoughtworks.selenium.SeleniumException:错误:命令执行失败。请浏览论坛http://clearspace.openqa.org 有关日志窗口中的错误详细信息。错误消息是:doc.style未定义
位于com.thoughtworks.selenium.HttpCommandProcessor.ThrowassertionFailureExceptionError(HttpCommandProcessor.java:97)
位于com.thoughtworks.selenium.HttpCommandProcessor.docomand(HttpCommandProcessor.java:91)
位于pl.softwaremill.common.test.web.selenium.screenshots.ScreenshotHttpCommandProcessor.docomand(ScreenshotHttpCommandProcessor.java:31)
位于com.thoughtworks.selenium.DefaultSelenium.CaptureEntityRepageScreenshot(DefaultSelenium.java:679)
位于pl.softwaremill.common.test.web.selenium.AbstractSeleniumTest.captureScreenshot(AbstractSeleniumTest.java:166)
位于pl.softwaremill.common.test.web.selenium.AbstractSeleniumTest$1.doScreenshot(AbstractSeleniumTest.java:95)
... 还有27个
com.thoughtworks.selenium.SeleniumException:错误:无法访问document.body。此HTML页面是否已完全加载?
位于com.thoughtworks.selenium.HttpCommandProcessor.ThrowassertionFailureExceptionError(HttpCommandProcessor.java:97)
位于com.thoughtworks.selenium.HttpCommandProcessor.docomand(HttpCommandProcessor.java:91)
位于com.thoughtworks.selenium.HttpCommandProcessor.getString(HttpCommandProcessor.java:262)
位于com.thoughtworks.selenium.HttpCommandProcessor.getBoolean(HttpCommandProcessor.java:335)
位于pl.softwaremill.common.test.web.selenium.screenshots.ScreenshotHttpCommandProcessor.getBoolean(ScreenshotHttpCommandProcessor.java:92)
位于com.thoughtworks.selenium.DefaultSelenium.isTextPresent(DefaultSelenium.java:499)
在za.co.fnb.commercial.dms.uitests.view.debutor.debutorinsuranceanexurepage.(debutorinsuranceanexurepage.java:18)
在za.co.fnb.commercial.dms.uitests.view.debtor.uploaddebortorfilespage.submit(uploaddebortorfilespage.java:45)
在za.co.fnb.commercial.dms.uitests.view.debtor.UploadDebtor文件SeleniumTest.shouldStoreFileEvenIfParseFails(UploadDebtor文件SeleniumTest.java:145)
pl.softwaremill.common.test.web.selenium.screenshots.Screenshot HttpCommandProcessor
只需将工作委托给com.thoughtworks.selenium.HttpCommandProcessor
因为错误消息显示“此HTML页面是否已完全加载?”您可以认为该页面未完全加载,但它确实已完全加载。我在调试中尝试了它,给了页面一些时间来加载,它被加载了,但是仍然发生了这个错误。实际上我也有同样的异常,首先搜索原因,我跟踪stacktrace并调试selenium代码,这是我的学习 我使用了selenium远程服务器 Selenium版本: 我的理解 在此之前,只有一个调用层次结构,实际的实现者方法是
getCommandResponseAsString(String命令)
,因此很难从该方法的调试中获得准确的(实际上我指的是稳定的)结果,因为在检查实现时,它似乎是一个检查响应代码HttpURLConnection.HTTP_OK
(其值为200)如果响应代码正常,它将返回已执行的字符串,但在某些耗时的情况下它不会返回,并抛出throwAssertionFailureExceptionOrError(uc.getResponseMessage())代码>
实施者方法
脏溶液
我试图在上面解释原因,所以在找到理想的方法之前,我会先使用Thread.sleep()方法或selenium.setSpeed(1000)方法,然后再使用selenium.isTextPresent()方法进行测试。Dunno。。。感兴趣的部分是“doc.style未定义”。需要检查的事项:自定义错误页是否为有效的html,并因此被浏览器解析?您使用哪个浏览器进行测试?在firefox中,firebug会告诉你关于doc.style的什么?MoritzPage是有效的HTML,在Chrome和Firefox中是可解析的(可见的)。doc未定义,但在每个页面上都是相同的。我尝试了document.style-它也是未定义的,并且也在“普通”页面上。
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.39.0</version>
<scope>test</scope>
</dependency>
public boolean isTextPresent(String pattern) {
return commandProcessor.getBoolean("isTextPresent", new String[] {pattern,});
}
public boolean getBoolean(String commandName, String[] args) {
String result = getString(commandName, args);
..........
..............
}
public String getString(String commandName, String[] args) {
String result = doCommand(commandName, args);
..........
..............
}
public String doCommand(String commandName, String[] args) {
DefaultRemoteCommand command = new DefaultRemoteCommand(commandName, args);
String result = executeCommandOnServlet(command.getCommandURLString());
..........
..............
}
/** Sends the specified command string to the bridge servlet */
public String executeCommandOnServlet(String command) {
try {
return getCommandResponseAsString(command);
} catch (IOException e) {
..........
..............
}
}
protected String getCommandResponseAsString(String command) throws IOException {
String responseString = null;
int responsecode = HttpURLConnection.HTTP_MOVED_PERM;
HttpURLConnection uc = null;
Writer wr = null;
Reader rdr = null;
while (responsecode == HttpURLConnection.HTTP_MOVED_PERM) {
URL result = new URL(pathToServlet);
String body = buildCommandBody(command);
try {
uc = getHttpUrlConnection(result);
uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
uc.setInstanceFollowRedirects(false);
uc.setDoOutput(true);
wr = getOutputStreamWriter(uc);
wr.write(body);
wr.flush();
responsecode = getResponseCode(uc);
if (responsecode == HttpURLConnection.HTTP_MOVED_PERM) {
pathToServlet = uc.getHeaderField("Location");
} else if (responsecode != HttpURLConnection.HTTP_OK) {
throwAssertionFailureExceptionOrError(uc.getResponseMessage());
} else {
rdr = getInputStreamReader(uc);
responseString = stringContentsOfInputStream(rdr);
}
} finally {
closeResources(uc, wr, rdr);
}
}
return responseString;
}