Capybara/Cucumber/Ruby 问题:仅在出现时按下元素并继续而不会出现错误
Capybara/Cucumber/Ruby issue: Press element only if appears and continue without errors
我是水豚世界的新手,我在编写测试以在有时只出现的屏幕中执行某些操作时遇到很多问题。
我想点击这个屏幕的一个元素,如果出现,如果没有出现,我需要继续点击并在下一屏幕填写表格。
我现在的解决方案有效,但它真的很难看:
步骤:
When(/^check my process with several screens$/) do
page=MyPage.new
page.check_and_click_screen1_if_appears
page.do_my_operations_in_screen2
end
MyPage 实现:
def check_and_click_screen1_if_appears
Capybara::find_by_id("idOfOneElement", :wait => 2).click
rescue Capybara::ElementNotFound
end
def do_my_operations_in_screen2
Capybara::find_by_id("idOfOtherElement").click
Capybara::fill_in('txtSearch', :with => 'mytext')
end
如果2秒后出现"idOfOneElement",点击它继续做"check_screen2_elements",如果没有出现,测试显示none错误,通过执行"do_my_operations_in_screen2".
效果很好,但我相信水豚可以更有效地做到这一点!
如果我需要改进我的测试,并想在我的可选 "screen1" 中填写一些文本框,我的救援异常解决方案不起作用!!!
一些帮助??
谢谢!!!
与其使用 find 或 expects,不如使用 Capybaras has_xxx? has_no_xxx?方法 (has_css?, has_button?, has_select?, ...) return 布尔结果而不是提高预期。在您的情况下,它将是
if page.has_css?('#idOfOneElement', wait: 2)
page.find_by_id("idOfOneElement").click
end
谢谢!!
非常有用!!!!
最终代码:
if Capybara::has_css?('#idOfOneElement', wait: 1)
Capybara::fill_in('idOfOneElement', :with => 'mytext')
Capybara::find_by_id("idOfOneButton").click
end
我是水豚世界的新手,我在编写测试以在有时只出现的屏幕中执行某些操作时遇到很多问题。
我想点击这个屏幕的一个元素,如果出现,如果没有出现,我需要继续点击并在下一屏幕填写表格。
我现在的解决方案有效,但它真的很难看:
步骤:
When(/^check my process with several screens$/) do
page=MyPage.new
page.check_and_click_screen1_if_appears
page.do_my_operations_in_screen2
end
MyPage 实现:
def check_and_click_screen1_if_appears
Capybara::find_by_id("idOfOneElement", :wait => 2).click
rescue Capybara::ElementNotFound
end
def do_my_operations_in_screen2
Capybara::find_by_id("idOfOtherElement").click
Capybara::fill_in('txtSearch', :with => 'mytext')
end
如果2秒后出现"idOfOneElement",点击它继续做"check_screen2_elements",如果没有出现,测试显示none错误,通过执行"do_my_operations_in_screen2".
效果很好,但我相信水豚可以更有效地做到这一点!
如果我需要改进我的测试,并想在我的可选 "screen1" 中填写一些文本框,我的救援异常解决方案不起作用!!!
一些帮助??
谢谢!!!
与其使用 find 或 expects,不如使用 Capybaras has_xxx? has_no_xxx?方法 (has_css?, has_button?, has_select?, ...) return 布尔结果而不是提高预期。在您的情况下,它将是
if page.has_css?('#idOfOneElement', wait: 2)
page.find_by_id("idOfOneElement").click
end
谢谢!!
非常有用!!!! 最终代码:
if Capybara::has_css?('#idOfOneElement', wait: 1)
Capybara::fill_in('idOfOneElement', :with => 'mytext')
Capybara::find_by_id("idOfOneButton").click
end