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