geb StaleElementReferenceException

时间:2012-08-31 20:28:33

标签: selenium automated-tests geb

我刚开始使用geb和webdriver进行自动化测试。 As I understand it,当我在页面上定义内容时,每次调用内容定义时都应该查找页面元素。

//In the content block of SomeModule, which is part of a moduleList on the page:
itemLoaded {
    waitFor{ !loading.displayed }
}
loading { $('.loading') }


//in the page definition
moduleItems {index -> moduleList SomeModule, $("#module-list > .item"), index}

//in a test on this page
def item = moduleItems(someIndex)
assert item.itemLoaded

所以在这段代码中,我认为应该重复调用$('.loading'),以便在模块的基本元素的上下文中通过其选择器在页面上查找元素。然而,我有时会得到一个StaleElementReference异常。据我所知,该元素不会从页面中删除,但即使它确实如此,除非$在幕后进行一些缓存,否则不会产生此异常,但如果是这样的话会引起各种其他问题。

有人可以帮我理解这里发生了什么吗?为什么在查找元素时可能会出现StaleElementReferenceException?指向相关文档或geb源代码的指针也很有用。

1 个答案:

答案 0 :(得分:1)

事实证明,问题是模块本身所代表的元素的引用已经通过修改而变得陈旧,而不是.loading元素。我怀疑异常来自该行,因为它试图在模块的基本元素中搜索以找到.loading元素。解决方案是在检查模块内部元素的同时加载模块。在这种情况下,它看起来像这样:

//In the content block of SomeModule, which is part of a moduleList on the page:
itemLoaded { !loading.displayed }
loading { $('.loading') }

//in the page definition
moduleItems {index -> moduleList SomeModule, $("#module-list > .item"), index}

//in a test on this page
waitFor { moduleItems(someIndex).itemLoaded }

感谢gecin用户邮件列表上的Marcin,指出我正确的方向。

相关问题