1

我正在尝试使用 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 似乎无法在浏览器窗口上看到新的小部件

4

2 回答 2

0

You need to add a wait till the first button action is done. Use a default constant across all waits in your app. DEFAULT_RPC_WAIT, DEFAULT_SCREEN_WAIT etc. It would help to know where you have set up your waits by using Constants instead of just magic numbers.

Also reference GWT guidelines w.r.t to Selenium - https://developers.google.com/web-toolkit/doc/latest/DevGuideTestingRemoteTesting

于 2013-02-04T10:43:36.913 回答
0
  1. 你发布了, 但在你的代码中我看到:ID = HOME_START_BUTTON
    By.id("HOME_START_BTN")

  2. 来自 WebDriver 接口对 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);
    
于 2015-01-18T10:29:59.707 回答