使用Selenium 2测试GWT应用程序

时间:2013-02-01 12:00:14

标签: gwt selenium

我正在尝试使用Selenium测试按钮点击。 我的第一页有一个ID = HOME_START_BUTTON的按钮。 当我点击这个我的应用程序然后转到带有ID = CONTACTS_ADD_BUTTON的按钮的页面。 这是我必须测试的代码。

private static boolean checkPageContainsStartButton()
{
    // type search query
    // driver.findElement(By.name("q")).sendKeys("qa automation\n");

    //Use static finals for these button names
    WebElement startButton = driver.findElement(By.id("HOME_START_BTN"));
    if (startButton == null)
    {
        return false;
    }
    else
    {
        startButton.click();
    }

    WebElement myDynamicElement = (new WebDriverWait(driver, 30))
      .until(new ExpectedCondition<WebElement>(){
            @Override
            public WebElement apply(WebDriver d) {
            return d.findElement(By.id("CONTACTS_ADD_BTN"));
    }});

    WebElement addButton = driver.findElement(By.id("CONTACTS_ADD_BTN"));
}

错误堆栈跟踪

Command duration or timeout: 28 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.29.0', revision: '58258c3', time: '2013-01-17 22:47:00'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_04'
Session ID: b25e7efb428c98da880826fbf9e68de6
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{platform=XP, chrome.chromedriverVersion=26.0.1383.0, acceptSslCerts=false, javascriptEnabled=true, browserName=chrome, rotatable=false, locationContextEnabled=false, version=24.0.1312.57, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, nativeEvents=true, webStorageEnabled=true, applicationCacheEnabled=false, takesScreenshot=true}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:187)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:533)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:302)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:331)
    at org.openqa.selenium.By$ById.findElement(By.java:216)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:294)
    at WebDriverTestClass.checkPageContainsStartButton(WebDriverTestClass.java:67)
    at WebDriverTestClass.main(WebDriverTestClass.java:35)
Test failed.

如果我发表评论,我的测试通过

WebElement addButton = driver.findElement(By.id("CONTACTS_ADD_BTN"));

因此,当我点击按钮时,Selenium似乎无法在浏览器窗口中看到新的小部件

2 个答案:

答案 0 :(得分:0)

您需要添加等待,直到第一个按钮操作完成。在应用中的所有等待中使用默认常量。 DEFAULT_RPC_WAIT,DEFAULT_SCREEN_WAIT等。通过使用常数而不仅仅是幻数来了解你在哪里设置等待会有所帮助。

另请参阅GWT指南w.r.t到Selenium - https://developers.google.com/web-toolkit/doc/latest/DevGuideTestingRemoteTesting

答案 1 :(得分:0)

  1. 您发布了ID = HOME_START_BUTTON
    但在我的代码中,我看到:By.id("HOME_START_BTN")

  2. 来自WebDriver接口的
  3. 对findElement方法的评论:
    不应使用findElement来查找不存在的元素,而是使用findElements(By)并声明零长度响应。

    /**
     * Find the first {@link WebElement} using the given method.
     * This method is affected by the 'implicit wait' times in force at the time of execution.
     * The findElement(..) invocation will return a matching row, or try again repeatedly until 
     * the configured timeout is reached.
     *
     * findElement should not be used to look for non-present elements, use {@link #findElements(By)}
     * and assert zero length response instead.
     *
     * @param by The locating mechanism
     * @return The first matching element on the current page
     * @throws NoSuchElementException If no matching elements are found
     * @see org.openqa.selenium.By
     * @see org.openqa.selenium.WebDriver.Timeouts
     */
    WebElement findElement(By by);