我已经进行了一些非常彻底的挖掘,并且无法准确确定导致引发此异常的元素。我正在使用 Java 和 Selenium 2,并且最近接管了一个相当大的测试套件来维护。与其盲目地在失败的代码行中和周围放置流畅的等待,我想实际确定哪些元素导致了问题。
我已经考虑将一堆单行逻辑分解成更多的代码行以帮助确定它,但由于这些随机故障是间歇性的并且到处都是,我正在尝试找到一种方法来获取实际名称堆栈跟踪中的元素或定位器。
这是可能的还是我必须在代码端做一堆重构?
这是一个示例堆栈跟踪:
Starting ChromeDriver (v2.3) on port 24902
org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
(Session info: chrome=30.0.1599.101)
(Driver info: chromedriver=2.3,platform=Mac OS X 10.8.5 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 5 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.33.0', revision: '4ecaf82108b2a6cc6f006aae81961236eba93358', time: '2013-05-22 12:00:17'
System info: os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.8.5', java.version: '1.7.0_21'
Session ID: f3e5d2c3eb54afc2fcaacc1c663435e9
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{platform=MAC, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={chromedriverVersion=2.3}, rotatable=false, locationContextEnabled=true, version=30.0.1599.101, cssSelectorsEnabled=true, databaseEnabled=true, handlesAlerts=true, browserConnectionEnabled=false, webStorageEnabled=true, nativeEvents=true, applicationCacheEnabled=false, takesScreenshot=true}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:191)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
at org.openqa.selenium.remote.RemoteWebElement.findElements(RemoteWebElement.java:187)
at org.openqa.selenium.remote.RemoteWebElement.findElementsByCssSelector(RemoteWebElement.java:240)
at org.openqa.selenium.By$ByCssSelector.findElements(By.java:417)
at org.openqa.selenium.remote.RemoteWebElement.findElements(RemoteWebElement.java:163)
at com.xmatters.webui.pages.home.developer.managerelevanceengines.formdetails.layoutsections.CustomSectionScreen.rowPropertyIsInCustomSection(CustomSectionScreen.java:54)
at com.xmatters.webui.pages.home.developer.managerelevanceengines.formdetails.layoutsections.CustomSectionScreen.editNameForProperty(CustomSectionScreen.java:90)
at com.xmatters.webdriver.tests.REBProperties_BooleanTest.confirmBooleanPropertyNameCanBeEditedOnLayoutTab(REBProperties_BooleanTest.java:526)
可疑行上的代码在此辅助函数中。第 54 行是包含List<WebElement>...
public Integer rowPropertyIsInCustomSection(String propertyName) {
wait.until(ExpectedConditions.presenceOfElementLocated(customSectionLocator));
WebElement customSection = driver.findElement(customSectionLocator);
//wait until properties are present in custom section
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(propertiesInCustomSectionLocator));
List<WebElement> propertiesInCustomSection = customSection.findElements(propertyNameLocatorInCustomSection);
By propertyNameLoc = By.id("propertyName");
for (int i = 0; i < propertiesInCustomSection.size(); i++) {
wait.until(ExpectedConditions.visibilityOfElementLocated(propertyNameLoc));
String propName = propertiesInCustomSection.get(i).findElement(propertyNameLoc).getText();
if (propName.equals(propertyName)) {
return i;
}
}
return null;
抱歉,如果这已在其他地方得到解答,但我很难在堆栈跟踪中使用这么少的东西来确定这个问题。这段代码都是别人写的,所以对我来说,一块一块地剖析是最后的手段。
在此先感谢您的任何指点。
达尔文。