我创建的测试存在问题,我会随机出现此问题,据我了解,这是因为正在运行的 java 脚本会定期刷新元素。我不确定如何阻止此错误发生,这是我的测试代码;
编辑:按照建议,我已从变量中删除了元素并直接调用它们,但错误仍然存在,这是我更新的代码和错误消息(包括行号)
47 When(/^I click the create room button$/) do
49 Watir::Wait.for_condition(10, 2, "Waiting for room data to load") {
50 @browser.iframe(:id, 'iconsole-plugin-session_iframe__').div(:id, 'lobby_rooms').div(:id, 'room_list').present?
51 }
53 rooms = []
55 @browser.iframe(:id, 'iconsole-plugin-session_iframe__').div(:id, 'lobby_rooms').div(:id, 'room_list').as.each do |room |
56 rooms << room.attribute_value('data-room-id')
57 end
59 puts roomvalue = rooms.size.to_i
61 @current_rooms = rooms
63 roomvalue
65 Watir::Wait.for_condition(10, 2, "Waiting for button to be present") {
66 @browser.iframe(:id, 'iconsole-plugin-session_iframe__').button(:id, 'create_room_form_button').present?
67 }
68 @browser.iframe(:id, 'iconsole-plugin-session_iframe__').button(:id, 'create_room_form_button').click
69 end
我收到的错误是;
Element is no longer attached to the DOM - {:element=>#<Selenium::WebDriver::Element:0x68d78665e583d27c id="{80a6296c-fc63-4d63-917c-9a2bf35bb429}">} (Watir::Exception::UnknownObjectException)
./features/step_definitions/multiplayer_fe_steps.rb:56:in `block (2 levels) in <top (required)>'
./features/step_definitions/multiplayer_fe_steps.rb:55:in `/^I click the create room button$/'
features/multiplayer_fe.feature:21:in `When I click the create room button'
感谢 Steel 提供的答案,但是即使将元素直接放入等待语句中,我也会遇到相同的错误,是否还有其他方法可以尝试解决该问题?
编辑:我尝试过在我的列表中使用 lamda,但是,当我这样做时,我会收到一个不同的错误,指出“在我的 .as.each do |room | 上未定义方法 'as'”
最佳答案
我不太了解 Watir,但我怀疑问题在于您将引用存储在变量中。也许可以试试这个:
Watir::Wait.for_condition(10, 2, "Waiting for room data to load") {
@browser.iframe(:id, 'iconsole-plugin-session_iframe__').div(:id, 'lobby_rooms').present?
}
还有
Watir::Wait.for_condition(10, 2, "Waiting for button to be present") {
@browser.iframe(:id, 'iconsole-plugin-session_iframe__').button(:id, 'create_room_form_button').present?
}
@browser.iframe(:id, 'iconsole-plugin-session_iframe__').button(:id, 'create_room_form_button').click
问题是每次你调用 @browser.find_element...你都会重新查看 DOM。当您存储变量时,您并不是存储查找或不查找的过程,而是存储实际的元素(无论是否找到)。当您尝试调用已不存在的找到的元素变量时,您会收到此 Selenium StaleObjectError。
这里换个 Angular ,但您也可以尝试将这些调用包装在 Proc 或 Lambda 中,而不是变量中。
createpress = -> { @browser.iframe(:id, 'iconsole-plugin-session_iframe__').button(:id, 'create_room_form_button') }
然后,当你想触发它时,调用它:
create_press.call.present?
这非常冗长且重复。如果您曾经使用过 Capybara,您可以尝试使用 Dill gem 将这些元素包装在小部件中,这些小部件就像过程一样。 https://github.com/mojotech/dill
关于javascript - 瓦提尔 "Element is no longer attached to the DOM"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34042980/