Selenium在自定义错误页面上的isTextPresent()上抛出异常

时间:2011-03-21 12:49:32

标签: web-applications jboss selenium integration-testing

在selenium测试中,我打开了一个我没有权限的页面,我想验证是否显示了错误页面而不是打开的错误页面。

检查这是否是错误页面很简单:

Assert.assertTrue(selenium.isTextPresent("HTTP Status 403"));

现在我通过<error-page>中的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)

pl.softwaremill.common.test.web.selenium.screenshots.ScreenshotHttpCommandProcessor只需将工作委托给com.thoughtworks.selenium.HttpCommandProcessor

因为错误消息显示“此HTML页面是否已完全加载?”你可以认为页面没有完全加载,但确实如此。我在调试中尝试了它,给页面加载一些时间,并且它已加载,但仍然发生此错误。

1 个答案:

答案 0 :(得分:0)

实际上我有相同的例外并首先搜索原因我跟踪堆栈跟踪并调试selenium代码这些是我的学习;

我使用了selenium远程服务器

Selenium版本:

 <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.39.0</version>
            <scope>test</scope>
        </dependency>

这些是selenium.isTextPresent()

的调用层次结构

调用层次结构

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) {
     ..........
       ..............
    }
  }

我的理解

直到这里只有一个调用层次结构实际的实现方法是getCommandResponseAsString(String command)所以很难从这个方法的调试中得到准确的(实际上我的意思是稳定)结果,因为当检查实现它似乎方法检查响应代码HttpURLConnection.HTTP_OK(其值为200)如果响应代码正常,则返回已执行的字符串,但是一些耗时的情况则不是,并抛出throwAssertionFailureExceptionOrError(uc.getResponseMessage());

实施者方法

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;
  }

脏溶液

我尝试在上面解释原因,所以直到我找到一个理想的方法,我在selenium.isTextPresent()方法之前使用Thread.sleep()方法或selenium.setSpeed(1000)方法使用我的测试。