【问题标题】:Is it possible to use "steps" inside SitePrism pages?是否可以在 SitePrism 页面中使用“步骤”?
【发布时间】:2017-04-13 09:18:44
【问题描述】:

我正在使用 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 页面中使用步骤?

谢谢!

【问题讨论】:

  • 我认为这是 SitePrism 页面的问题,但我刚刚尝试了一个标准类甚至一个模块,并且我不断收到关于“未定义方法步骤”的​​相同问题。如果有人有建议我可以试试吗?

标签: ruby cucumber capybara site-prism


【解决方案1】:

感谢google group 中的“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

【讨论】:

    【解决方案2】:

    老问题,但提供答案

    使用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 官方页面上发布了某些问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      • 1970-01-01
      • 2014-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多