开发者

Selenium doesn't find links, buttons, texts in GWT?

I just tried testing my GWT web application with Selenium. I recorded something with the Selenium IDE Firefox plugin (register -> log in -> logout). But my test always fails at the logout part, because Selenium doesn't find the link. I ran the test again and then it worked. I guess that has something to do with the speed of the response? I also made some other tests and they always fail because links, buttons or texts aren't found. How can I solve that? Is the problem that Selenium checks while the page isn't ready yet? Would Thread.sleep work before checking or pressing bu开发者_如何学Pythonttons/links or maybe that waitForPageToLoad?


You're correct about the problem. Selenium runs the test before Javascript populates your DOM tree with content. You can Thread.sleep(), however that would slow your tests down and occasionally fail if your app runs slower than you anticipated. It's better to wait for element until the timeout expires.

All my tests extend this class, which in turn extends SeleneseTestCase, and instead of calling selenium.type() I call type() in my tests:

public class WebTestCase extends SeleneseTestCase {

  private final long DEFAULT_TIMEOUT = 30;

  protected SeleniumServer server;

  public WebTestCase() {
  }

  public WebTestCase(String name) {
    super(name);
  }

  protected void startServer() throws Exception {
    server = new SeleniumServer();
    server.start();
  }

  protected void stopServer() {
    server.stop();
  }

  public void waitForElement(final String waitingElement) {
    waitForElement(waitingElement, DEFAULT_TIMEOUT);
  }

  public void waitForElement(final String waitingElement, long timeoutInSeconds) {
    new Wait() {
      @Override
      public boolean until() {
        return selenium.isElementPresent(waitingElement);
      }
    }.wait("Timeout while waiting for element " + waitingElement, timeoutInSeconds * 1000);
  }

  public void type(String element, String text) {
    waitForElement(element);
    selenium.type(element, text);
  }

  public void typeKeys(String element, String text) {
    waitForElement(element);
    selenium.typeKeys(element, text);
  }

  public void click(String element) {
    waitForElement(element);
    selenium.click(element);
  }

  public void select(String element, String value) {
    waitForElement(element);
    selenium.select(element, value);
  }

  public void check(String element) {
    waitForElement(element);
    selenium.check(element);
  }

  public void uncheck(String element) {
    waitForElement(element);
    selenium.uncheck(element);
  }

  public void focus(String element) {
    waitForElement(element);
    selenium.focus(element);
  }

  public void checkTextPresent(final String text) {
    checkTextPresent(text, DEFAULT_TIMEOUT);
  }

  public void checkTextPresent(final String text, long timeoutInSeconds) {
    new Wait() {
      @Override
      public boolean until() {
        return selenium.isTextPresent(text);
      }
    }.wait("Timeout while waiting for text \"" + text + "\"", timeoutInSeconds * 1000);
  }

  public void checkTextNotPresent(final String text) {
    checkTextPresent(text, DEFAULT_TIMEOUT);
  }

  public void checkTextNotPresent(final String text, long timeoutInSeconds) {
    new Wait() {
      @Override
      public boolean until() {
        return !selenium.isTextPresent(text);
      }
    }.wait("Timeout while waiting for text \"" + text + "\"", timeoutInSeconds * 1000);
  }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜