0

我正在使用 cucumber-ruby 框架,我们正在使用 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 页面中使用步骤?

谢谢!

4

2 回答 2

0

归功于google 群组中的“Jonas Maturana Larsen” 。另一个例子的类似问题,但是将“世界”传递给班级也为我解决了这个问题。

步骤在 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
于 2017-04-13T15:04:39.373 回答
0

老问题但提供答案

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

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

此外,正如你所发现的。在 Cucumber World 中运行,将所有 cucumber 方法扩展为顶级 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 页面上发布了某些内容。

于 2019-02-22T14:09:08.410 回答