是否可以在SitePrism页面中使用“步骤”?

时间:2017-04-13 09:18:44

标签: ruby cucumber capybara site-prism

我正在使用黄瓜红宝石框架,我们正在使用Capybara和SitePrism来驱动浏览器。

我有一种情况,如果发生错误,我想重试一系列步骤,所以我在SitePrism页面中添加一个逻辑方法来覆盖它,如下所示:

steps %Q{
When I click on the back button
And I enter my reference number
Then I am able to complete the action successfully
}

我发现的问题是,当达到这部分代码时,执行失败并执行:

    undefined method `steps' for #<MySitePrismPage:0x000000063be5b0 @loaded=false> (NoMethodError)

我是否有办法在SitePrism页面中使用步骤?

谢谢!

2 个答案:

答案 0 :(得分:0)

归功于Jonas Maturana Larsen&#39;在google group。类似的问题与另一个例子,但传递世界&#39;上课也为我解决了这个问题。

  

步骤在Cucumbers RbWorld模块中定义。

     

您需要从您创建的地方传递世界实例   TestRubyCallStep类。

     

在您的情况下,您可能实际上想要创建一个模块而不是一个模块   如果你只是需要一个地方来保持共享方法,那就上课。

class TestRubyCallStep   
    include Calabash::Android::Operations   

    def initialize(world)
        @world = world   
    end   

    def callMethod
        @world.step %Q{my customized steps in custom_step.rb}   
    end 
end      
     

执行步骤定义的上下文世界:)

     

试试这个:

Then /^I call a step from Ruby class "([^\"]*)"$/ do |world|   
    testObj = TestRubyCallStep.new(self)   
    testObj.callMethod 
end

答案 1 :(得分:0)

旧问题,但提供了答案

使用steps作为方法调用被认为是反模式,并且在黄瓜中被弃用/删除。

强烈建议将常见行为提取到帮助程序模块/类中,然后在其上调用方法。

此外,您发现了。在黄瓜世界中运行,将所有黄瓜方法扩展到顶级DSL,因此只需调用steps就可以了。而DSL永远不会混入任何SitePrism上下文中,因此您无法做到这一点。

TL; DR-不要做您想做的事情。做这样的事情。

module MyReusableHelper
  def click_back
    # verbose code here
  end

  def enter_reference_number
    # verbose code here
  end

  def complete_action # Note this method name probably needs a rethink
    # verbose code here
  end

然后只需将该模块包含在需要的任何类中即可。

如果您还有其他疑问,请在此处询问,或者您是否确信官方GH页面上的帖子已损坏。

相关问题