2013-02-01 32 views
1

我正在嘗試使用Selen來測試按鈕單擊。 我的第一頁有一個ID = HOME_START_BUTTON的按鈕。 當我點擊這個我的應用程序,然後進入一個ID = CONTACTS_ADD_BUTTON按鈕的頁面。 這是我必須測試的代碼。如果我註釋掉使用Selenium 2測試GWT應用程序

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")); 

所以基本上硒似乎並不能夠看到瀏覽器窗口時,在新的widget我做了一個按鈕點擊

+0

嘿SSR,是的,就是這樣,我會接受你的建議作爲一個答案,但不能因爲它僅僅是一個註釋。 – MayoMan

+0

這將是很好,如果你可以在選擇答案後關閉問題:) – SSR

回答

0
  1. 您發佈ID = HOME_START_BUTTON
    但在你的代碼中,我看到:By.id("HOME_START_BTN")

  2. 從findElement方法webdriver的接口評論:
    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); 
    
相關問題