【问题标题】:It looks like Capybara clicked on button properly, but doesn't proceed to the next pageCapybara 似乎正确单击了按钮,但没有进入下一页
【发布时间】:2016-07-12 12:54:39
【问题描述】:

我在编写 Cucumber 测试时遇到问题。通过 Paylane 付款后(不知何故,Paylane 场景只有问题;Paypal、Stripe 和 Payu 工作正常)当它在“谢谢”页面上时,它看起来像 Capybara 点击按钮(返回商店)它在浏览器中突出显示,但它什么都不做。此外,它进入下一步并检查它是否在商店页面上,所以它认为点击按钮很顺利(我想)。有人可以帮我吗?


@lp @purchase @paylane @javascript
Feature: Purchase a product from a Landing Page via Paylane
  As a Sneakpick user
  I can access Landing Pages of product
  So I can successfully purchase it via Paylane

  Scenario: Purchase product via Paylane # features/purchases/landing_page/paylane.feature:7
    Given I visit the Landing Page       # features/step_definitions/basic_steps.rb:5
    Then I want to order 1 product       # features/step_definitions/purchase_steps.rb:92
    And product is available via Paylane # features/step_definitions/purchase_steps.rb:1
DEPRECATION WARNING: Passing a template handler in the template name is deprecated. You can simply remove the handler name or pass render :handlers => [:erb] instead. (called from _app_views_paylane_pay_html_erb___1015711955565910954_70249561011280 at /home/szczepan/1000i/Git/sneakpick/app/views/paylane/pay.html.erb:2)
    Then I go to payment                 # features/step_definitions/purchase_steps.rb:53
    And I pay via Paylane                # features/step_definitions/paylane_steps.rb:1
      expected to find text "Product" in "Payment - Sneakpick.co Thank You! Your order has been processed. We've sent you a confirmation emailwith your order details. « Back to product powered by sneakpick" (RSpec::Expectations::ExpectationNotMetError)
      ./features/step_definitions/purchase_steps.rb:77:in `/^I am on Landing page$/'
      ./features/step_definitions/paylane_steps.rb:2:in `/^I pay via Paylane$/'
      features/purchases/landing_page/paylane.feature:12:in `And I pay via Paylane'
    And my Paylane order should be made  # features/step_definitions/paylane_steps.rb:76

  Scenario: Purchase product via Paylane             # features/purchases/landing_page/paylane.feature:15
    Given I visit the Landing Page                   # features/step_definitions/basic_steps.rb:5
    Then I want to order 1 product                   # features/step_definitions/purchase_steps.rb:92
    And product is available via Paylane             # features/step_definitions/purchase_steps.rb:1
DEPRECATION WARNING: Passing a template handler in the template name is deprecated. You can simply remove the handler name or pass render :handlers => [:erb] instead. (called from _app_views_paylane_pay_html_erb___1015711955565910954_70249561011280 at /home/szczepan/1000i/Git/sneakpick/app/views/paylane/pay.html.erb:2)
    Then I go to payment                             # features/step_definitions/purchase_steps.rb:53
    And I pay via Paylane not with card successfully # features/step_definitions/paylane_steps.rb:13
    And my Paylane order should be made              # features/step_definitions/paylane_steps.rb:76

哦,第二个工作正常,但第一个没有,但他们点击了同一个按钮。 步骤如下:


And /^I pay via Paylane$/ do
  steps %(
    And I check if Paylane form is filled with correct data
    And I fill in the card data
    And I send form
    And I accept confirmation alert
    And I am on Thank You page
    And I click 'Back to product' button
    And I am on Landing page
  )
end

And /^I pay via Paylane not with card successfully$/ do
  steps %(
    And I choose last available payment
    And I send form
    And I accept confirmation alert
    And I click 'SUCCESS' button
    And I am on Thank You page
    And I click 'Back to product' button
    And I am on Landing page
  )
end

以及“我点击 (.+) 按钮”步骤:


And /^I click '(.+)' button$/ do |button|
    click_on(button)
end

按钮包含文本“返回产品”。

任何帮助将不胜感激!

【问题讨论】:

  • 我在点击之前添加了睡眠,这会导致麻烦,这会有所帮助,但我将其视为临时解决方案。 cucumber 是否有问题要等到页面加载完毕再在上面做事?

标签: ruby-on-rails-3.2 cucumber capybara


【解决方案1】:

一个典型的解决方案(至少在 Ruby-Cucumber 中)是一个简单的方法,如 wait_for_ajax - 基本上,它会阻塞,直到它注册没有更多活动的 HTTP 调用,然后再继续。这通常在 Capybara 内部作为步骤定义中的函数调用来完成。每当您预计必须等待时(尤其是在第三方页面上),请在您的 step-defs 中随意使用它,尤其是在您执行操作之后(例如单击某物)。这比在预定的时间内致电sleep() 要可靠得多。

def wait_for_ajax
    Timeout.timeout(Capybara.default_max_wait_time) do
        loop until finished_all_ajax_requests?
    end
end

def finished_all_ajax_requests?
    page.evaluate_script('jQuery.active').zero?
end

来源:Thoughtbot 原始源代码(已经有一段时间了)和修复不可靠 Capybara/Cucumber 测试套件的个人经验。

另外,我上次检查过,Capybara 中的 steps%(...) 语法已被弃用,您应该使用 step '...' 来调用各个步骤。它使您可以将 step-def 更像函数调用(对于这种事情)。

【讨论】:

    猜你喜欢
    • 2013-01-24
    • 2010-09-08
    • 1970-01-01
    • 1970-01-01
    • 2017-04-12
    • 2021-06-12
    • 1970-01-01
    • 2013-10-27
    • 1970-01-01
    相关资源
    最近更新 更多