如何在水豚场景中添加等待条件?

问题描述 投票:0回答:4

我正在使用 capybara 来测试我的 Rails 应用程序以进行集成测试。 在我的应用程序中有很多 Lightbox、Ajax 和 js 调用。

 @javascript   
  Scenario: I agree functionatilty
   Given I go to the create account page
   When I click on button which is given as image "lnkTerms2"
   And I follow "i_agree"
   Then I go to the create account page

在上面的代码中,lnkTerms2 是 id,这将有助于调用 js 函数来打开灯箱。 我收到错误为

   Element is not currently visible and so may not be interacted with (Selenium::WebDriver::Error::ElementNotDisplayedError)
      [remote server] resource://fxdriver/modules/atoms.js:9519:in `unknown'
      [remote server] file:///tmp/webdriver-profile20111117-6876-18cfcfp/extensions/[email protected]/components/nsCommandProcessor.js:256:in `unknown'
      [remote server] file:///tmp/webdriver-profile20111117-6876-18cfcfp/extensions/[email protected]/components/nsCommandProcessor.js:305:in `unknown'
      [remote server] file:///tmp/webdriver-profile20111117-6876-18cfcfp/extensions/[email protected]/components/nsCommandProcessor.js:320:in `unknown'
      [remote server] file:///tmp/webdriver-profile20111117-6876-18cfcfp/extensions/[email protected]/components/nsCommandProcessor.js:197:in `unknown'
      (eval):2:in `send'
      (eval):2:in `click_link'
      ./features/step_definitions/web_steps.rb:300:in `/^I click on button which is given as image "([^"]*)"$/'
      features/Sign_up_process.feature:61:in `When I click on button which is given as image "lnkTerms2"'

问题是当这个函数在 webdriver 中调用时,它没有时间加载 javascript 和 ajax 调用。 而且灯箱打不开。 所以请建议我任何解决方案。

如果假设我写了这行

When I click on button which is given as image "lnkTerms2"

在 4 到 5 条语句之后,它工作正常,因为有时间加载 js。

ruby-on-rails cucumber capybara
4个回答
14
投票

通常固定的睡眠/等待是一件坏事。它们是一种蛮力方法,要么导致脚本脆弱,要么脚本缓慢,或者通常两者兼而有之。如果你没有将它们设置得足够长,那么测试偶尔会中断,如果你将它们设置得太长,那么测试永远不会中断,但由于所有固定的拇指转动时间,它们很慢。

大多数自动化工具要么自动处理等待,要么提供更优雅的方式来将脚本与应用程序同步

JNicklas 最近发表的一篇博客文章解释了 Capybara 在这方面的一些最新变化,提供了一些针对一些特殊情况执行特定等待条件类型代码的方法的示例,并且一般建议了解更多该工具以及它如何处理等待、ajax 操作和同步。


3
投票

要在步骤后暂停以等待ajax,请尝试:

And I wait 5 seconds

您必须添加到 web_steps.rb 下一个代码:

When /^I wait (\d+) seconds?$/ do |seconds|
  sleep seconds.to_i
end

1
投票

等待固定的秒数以希望您的场景足够快并不是最好的策略,因为它可能会导致随机测试失败。我建议您等到满足某些条件为止:

And I wait until '#meow' is visible

#...
When /^I wait until '([^']+)' is visible$/ do |selector|
  wait_until do # you can also specify timeout here
    find(selector).visible?
  end
end

0
投票

您可以添加一个名为 wait 的参数

page.find(:css, "#button", wait: 5).click

© www.soinside.com 2019 - 2024. All rights reserved.