Selenium throws exception on isTextPresent() on custom error page
In a selenium test, I open a page to which I don't have rights, and I want to verify that error page is displayed instead of the opened one.
Check if this is error page is simple:
Assert.assertTrue(selenium.isTextPresent("HTTP Status 403"));
Now I changed default JBoss error page to custom one, by <error-page>
tag in web.xml
. After this, selenium doesn't work. It throws exception from isTextPresent()
. Of course text on the page changed too, but it doesn't matter here.
Stacktrace is:
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.com开发者_如何学运维mercial.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)
pl.softwaremill.common.test.web.selenium.screenshots.ScreenshotHttpCommandProcessor
just delegates work to com.thoughtworks.selenium.HttpCommandProcessor
Because error message says "Is this HTML page fully loaded?" you can think the page is not fully loaded, but it is. I tried it in debug, giving some time for page to load, and it was loaded, yet still this error occured.
Actually i have the same exception and search the reason first i track the stacktrace and debug the selenium code these are my learning;
I used selenium remote server
Selenium Version:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.39.0</version>
<scope>test</scope>
</dependency>
These are call hierarchy of selenium.isTextPresent()
Call Hierarchy
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) {
..........
..............
}
}
My Understanding
Until here there is only a call hierarchy actual implementer method is getCommandResponseAsString(String command)
so it is hard to get exact(actually i mean stabile) result from debug of this method, because when check the implementation it seems method check the response code HttpURLConnection.HTTP_OK
(its value is 200) if the response code is OK, it returns the executed string, but some time consuming cases it is not and throw throwAssertionFailureExceptionOrError(uc.getResponseMessage());
Implementer Method
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;
}
Dirty Solution
I try to explain the reason at above, so until i find an ideal way i go on my testing with Thread.sleep() method or selenium.setSpeed(1000) method usage before selenium.isTextPresent() method.
精彩评论