水豚 selenium 和 JavaScript Destroy

发布于 2024-09-02 03:25:51 字数 917 浏览 0 评论 0原文

我正在使用 Rails 2.3.5,这就是我所做的。我安装了最新的黄瓜、黄瓜导轨和水豚。

rails demo
cd demo
ruby script/generate cucumber --rspec --capybara
ruby script/generate feature post title:string body:text published:boolean
ruby script/generate scaffold post title:string body:text published:boolean
rake db:migrate
rake cucumber

所有测试都通过了。现在我想使用 Javascript 进行测试。

情况

  Scenario: Delete post
    Given the following posts:
      |title|body|published|
      |title 1|body 1|false|
      |title 2|body 2|true|
      |title 3|body 3|false|
      |title 4|body 4|true|
    When I delete the 3rd post
    Then I should see the following posts:
      |Title|Body|Published|
      |title 1|body 1|false|
      |title 2|body 2|true|
      |title 4|body 4|true|

此时,这就是我在顶部添加@javascript 的

。现在,当我运行 rake cucumber 时,我会看到一个确认页面。但在我点击之前什么也没有发生。

我需要做什么才能自动单击“确定”并继续进行测试。

I am using rails 2.3.5 and this is what I did. I have latest cucumber, cucumber-rails and capybara installed.

rails demo
cd demo
ruby script/generate cucumber --rspec --capybara
ruby script/generate feature post title:string body:text published:boolean
ruby script/generate scaffold post title:string body:text published:boolean
rake db:migrate
rake cucumber

All the tests are passing. Now I want to test using Javascript.

At this time this is how scenario looks like

  Scenario: Delete post
    Given the following posts:
      |title|body|published|
      |title 1|body 1|false|
      |title 2|body 2|true|
      |title 3|body 3|false|
      |title 4|body 4|true|
    When I delete the 3rd post
    Then I should see the following posts:
      |Title|Body|Published|
      |title 1|body 1|false|
      |title 2|body 2|true|
      |title 4|body 4|true|

I added @javascript at the top.

Now when I run rake cucumber then I see a confirmation page. But nothing happens until I click.

What do I need to do so that OK is clicked automatically and test proceeds ahead.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉态萌生 2024-09-09 03:25:51

嗯,这是一种黑客攻击,但我认为现在这是唯一的方法:

When /^I confirm a js popup on the next step$/ do
  page.evaluate_script("window.alert = function(msg) { return true; }")
  page.evaluate_script("window.confirm = function(msg) { return true; }")
end

您必须将此步骤放在触发确认弹出窗口的步骤之前(点击链接)。它将修改标准警报并确认行为以始终返回 true。因此您不必自己单击“确定”按钮。

Well its kind of a hack, but I think right now its the only way:

When /^I confirm a js popup on the next step$/ do
  page.evaluate_script("window.alert = function(msg) { return true; }")
  page.evaluate_script("window.confirm = function(msg) { return true; }")
end

You have to put this step right in front of the one that triggers the confirm popup (follows the link). It will modify the standard alert and confirm behaviour to always return true. So you do not have to click the "OK" button yourself.

み零 2024-09-09 03:25:51

我对托比亚斯的解决方案实施了一个变体。

我想要像当我点击客户“Alice Angry”的“删除”链接时这样的步骤,所以我有以下内容:

When /^(.*) and (?:|I )click "OK"$/ do |step|
  click_ok_after { When step }
end

module JavascriptHelpers
  def click_ok_after
    begin
      page.evaluate_script("window.alert = function(msg) { return true; }")
      page.evaluate_script("window.confirm = function(msg) { return true; }")
    rescue Capybara::NotSupportedByDriverError
      # do nothing: we're not testing javascript
    ensure
      yield
    end
  end
end
World(JavascriptHelpers)

完整的解释可以在我在这里写的博客文章中找到< a href="http://davidsulc.com/blog/2011/07/10/cucumber-tweaks/" rel="nofollow">http://davidsulc.com/blog/2011/07/10/cucumber-tweaks / (包括用于测试 HTML 容器中内容的有用步骤定义)。

I've implemented a variation on Tobias's solution.

I wanted to have steps like When I follow the "Delete" link for customer "Alice Angry", so I have the following:

When /^(.*) and (?:|I )click "OK"$/ do |step|
  click_ok_after { When step }
end

module JavascriptHelpers
  def click_ok_after
    begin
      page.evaluate_script("window.alert = function(msg) { return true; }")
      page.evaluate_script("window.confirm = function(msg) { return true; }")
    rescue Capybara::NotSupportedByDriverError
      # do nothing: we're not testing javascript
    ensure
      yield
    end
  end
end
World(JavascriptHelpers)

The full explanation can be found in the blog post I wrote about it here http://davidsulc.com/blog/2011/07/10/cucumber-tweaks/ (including a helpful step definition for testing content within HTML containers).

揽月 2024-09-09 03:25:51

感谢史蒂文提供的解决方案,以下是我对其进行修改的方法,使其读起来更好一些:

When /^I follow "([^"]*)" and click OK$/ do |text|
  page.evaluate_script("window.alert = function(msg) { return true; }")
  page.evaluate_script("window.confirm = function(msg) { return true; }")
  When %{I follow "#{text}"}
end

Thanks to Steven for his solution, here's how I modified it so that it reads a little better:

When /^I follow "([^"]*)" and click OK$/ do |text|
  page.evaluate_script("window.alert = function(msg) { return true; }")
  page.evaluate_script("window.confirm = function(msg) { return true; }")
  When %{I follow "#{text}"}
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文