【问题标题】:How to test a confirm dialog with Cucumber?如何用 Cucumber 测试确认对话框?
【发布时间】:2011-01-28 08:30:31
【问题描述】:

我在 Cucumber 和 Capybara 中使用 Ruby on Rails。

我将如何测试一个简单的确认命令(“你确定吗?”)?

另外,我在哪里可以找到有关此问题的更多文档?

【问题讨论】:

标签: automated-tests capybara integration-testing


【解决方案1】:

硒驱动now supports this

从 Capybara 你可以这样访问它:

page.driver.browser.switch_to.alert.accept

page.driver.browser.switch_to.alert.dismiss

 page.driver.browser.switch_to.alert.text

【讨论】:

  • 对于其他任何关注此内容的人 - 请注意,Derek 的回答确实有效,我发现官方 Selenium 文档中的代码没有(黄瓜/硒)。请注意 Derek 的回答中 page.driver.browser 的存在
  • Peter - 这里的代码是专门为使用 capybara 而量身定制的,而文档中的代码是针对你直接使用 selenium-webdriver 时的 - 我也写了那个例子,所以我希望它有效!跨度>
  • 啊。是的,很好,我完全错过了。在这种情况下,感谢您提供这两个示例。
【解决方案2】:

不幸的是,在水豚中似乎没有办法做到这一点。但是,如果您使用 Selenium 驱动程序(可能还有其他支持 JavaScript 的驱动程序)运行测试,您可以破解它。就在执行将打开确认对话框的操作之前,覆盖confirm 方法以始终返回true。这样,对话框将永远不会显示,并且您的测试可以继续进行,就像用户按下了 OK 按钮一样。如果要模拟反向,只需将其更改为返回 false。

page.evaluate_script('window.confirm = function() { return true; }')
page.click('Remove')

【讨论】:

  • 这似乎在 Firefox 4 中不再适用...@derek-ekins 下面的解决方案,根据谷歌告诉我的,似乎更向前兼容,虽然我不能确认然而(我被困在 Capybara 0.3.9 上)。
  • 查看下面关于使用“page.driver.browser.switch_to ...”的答案
【解决方案3】:

我在/features/step_definitions/web_steps.rb中实现了这两个网络步骤:

When /^I confirm popup$/ do
  page.driver.browser.switch_to.alert.accept    
end

When /^I dismiss popup$/ do
  page.driver.browser.switch_to.alert.dismiss
end

【讨论】:

    【解决方案4】:

    如果您想专门测试正在显示的消息,这里有一个特别老套的方法。我不认可它是漂亮的代码,但它可以完成工作。你需要加载 http://plugins.jquery.com/node/1386/release,或者如果你不想要 jQuery,将其更改为原生 cookie。

    使用这种故事:

    Given I am on the menu page for the current booking
    And a confirmation box saying "The menu is £3.50 over budget. Click Ok to confirm anyway, or Cancel if you want to make changes." should pop up
    And I want to click "Ok"
    When I press "Confirm menu"
    Then the confirmation box should have been displayed
    

    还有这些步骤

    Given /^a confirmation box saying "([^"]*)" should pop up$/ do |message|
      @expected_message = message
    end
    
    Given /^I want to click "([^"]*)"$/ do |option|
      retval = (option == "Ok") ? "true" : "false"
    
      page.evaluate_script("window.confirm = function (msg) {
        $.cookie('confirm_message', msg)
        return #{retval}
      }")
    end
    
    Then /^the confirmation box should have been displayed$/ do
      page.evaluate_script("$.cookie('confirm_message')").should_not be_nil
      page.evaluate_script("$.cookie('confirm_message')").should eq(@expected_message)
      page.evaluate_script("$.cookie('confirm_message', null)")
    end
    

    【讨论】:

    【解决方案5】:

    更新当前版本的 Capybara。目前大多数 Capybara 驱动程序都支持模态 API。要接受确认模式,您会这样做

    accept_confirm do  # dismiss_confirm if not accepting
      click_link 'delete'  # whatever action triggers the modal to appear
    end
    

    这可以在 Cucumber 中使用,例如

    When /^(?:|I )press "([^"]*)" and confirm "([^"]*)"$/ do |button, msg|
      accept_confirm msg do
        click_button(button)
      end
    end
    

    它将单击命名按钮,然后接受带有文本匹配 msg 的确认框

    【讨论】:

      【解决方案6】:

      capybara-webkit 驱动程序也支持这一点。

      【讨论】:

        【解决方案7】:
        Scenario: Illustrate an example has dialog confirm with text
            #     
            When I confirm the browser dialog with tile "Are you sure?"
            #
        =====================================================================
        my step definition here:
        
        And(/^I confirm the browser dialog with title "([^"]*)"$/) do |title|
          if page.driver.class == Capybara::Selenium::Driver
            page.driver.browser.switch_to.alert.text.should eq(title)
            page.driver.browser.switch_to.alert.accept
          elsif page.driver.class == Capybara::Webkit::Driver
            sleep 1 # prevent test from failing by waiting for popup
            page.driver.browser.confirm_messages.should eq(title)
            page.driver.browser.accept_js_confirms
          else
           raise "Unsupported driver"
         end
        end
        

        【讨论】:

          【解决方案8】:

          Prickle 为在 selenium 和 webkit 中处理弹出窗口添加了一些方便的方法

          【讨论】:

            【解决方案9】:

            This gist 包含使用任何 Capybara 驱动程序在 Rails 2 和 3 中测试 JS 确认对话框的步骤。

            这是对先前答案的改编,但不需要 jQuery Cookie 插件。

            【讨论】:

              【解决方案10】:

              没有运气尝试了上述答案。最后这对我有用:

              @browser.alert.ok
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-05-24
                • 2016-02-02
                • 2023-04-03
                • 1970-01-01
                相关资源
                最近更新 更多