我正在使用Selenium测试JSF2应用程序,但是获取了大量的StaleElementReferenceExceptions并且不知道如何解决这些问题。 Selenium文档告诉我,对象不再在DOM中,你必须重新连接它们。我怎么能这样做?
在javascript动态更新内容的页面上抛出异常。奇怪的是,有时测试是成功的,有时它们会失败,因此没有确定性的行为。
我正在使用HTMLUnitDriver(WebDriver)进行测试。我还尝试过ChromeDriver,Chrome在测试中正常运行。但Chrome在Jenkins CI版本中无法使用。
以下是示例跟踪:
org.openqa.selenium.StaleElementReferenceException: The element seems to be disconnected from the DOM. This means that a user cannot interact with it.
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.16.1', revision: '15405', time: '2012-01-05 12:30:12'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_29'
Driver info: driver.version: SenderPageDriver
at org.openqa.selenium.htmlunit.HtmlUnitWebElement.assertElementNotStale(HtmlUnitWebElement.java:854)
at org.openqa.selenium.htmlunit.HtmlUnitWebElement.getTagName(HtmlUnitWebElement.java:360)
at org.openqa.selenium.htmlunit.HtmlUnitWebElement.switchFocusToThisIfNeeded(HtmlUnitWebElement.java:316)
at org.openqa.selenium.htmlunit.HtmlUnitWebElement.sendKeys(HtmlUnitWebElement.java:345)
at ch.postlogistics.avg.common.AbstractPageDriver.fillInTextField(AbstractPageDriver.java:140)
at ch.postlogistics.avg.common.SenderPageDriver.senderCompany(SenderPageDriver.java:73)
at ch.postlogistics.avg.common.SenderPageDriver.addSender(SenderPageDriver.java:122)
at ch.postlogistics.avg.webtest.parcel.ParcelWorkflowTest.goThroughWorkflowWithLabelGenerationWithVinolog(ParcelWorkflowTest.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:24)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
答案 0 :(得分:3)
尝试这样的事情。请注意,此方法可能返回null。您可以编辑此方法以满足您的需求。例如,可以使用FluentWait来改进此方法。
public static WebElement getElementByLocator( By locator ) {
driver.manage().timeouts().implicitlyWait( 9, TimeUnit.SECONDS );
WebElement we = null;
boolean unfound = true;
int tries = 0;
while ( unfound && tries < 10 ) {
tries += 1;
try {
we = driver.findElement( locator );
unfound = false; // FOUND IT
} catch ( StaleElementReferenceException ser ) {
unfound = true;
} catch ( NoSuchElementException nse ) {
unfound = true;
} catch ( Exception e ) {
System.out.println("Unknown error.");
}
}
driver.manage().timeouts().implicitlyWait( DEFAULT_IMPLICIT_WAIT, TimeUnit.SECONDS );
return we;
}
答案 1 :(得分:1)
尝试在脚本中实现一些等待。可以抛出异常,因为您没有等待页面完全加载。