【问题标题】:Calling cucumber methods from a module从模块调用黄瓜方法
【发布时间】:2016-04-05 21:46:02
【问题描述】:

我正在使用 Ruby 2.3.0 编写葫芦测试,但我无法从模块中调用黄瓜方法。

module A
    module_function
    def visible
        wait_for_elements_exist("Some element query")
    end
end

还有:

Class B
    include A
end

当我调用B.visible 或在B 中定义时:

def visible
    A::visible
end

然后调用B.visible,我得到NoMethodError,表示模块A中的wait_for_elements_exist

我尝试在模块中使用require 'calabash-cucumber/cucumber',在模块中使用include Calabash::Cucumber。没用。

我可以从项目中的其他类访问所有黄瓜方法,并且我在 env.rb 中需要黄瓜,因此已加载库。我想知道如何从模块中访问库函数,或者如何正确地将内容包含到我的模块中。

编辑

我试过include Calabash::Cucumber::WaitHelpers

还有include Calabash::Cucumber::Operations

没有用。我用extend替换了include,现在我可以访问这些方法了,但这并不能解决我的问题。

我需要的是

AndroidModule < Calabash::Android::Operations

IosModule < Calabash::Cucumber::Operations

在这些模块中,我定义了平台之间不同的方法。

然后有ScreenModule 用于各种屏幕,我在其中定义特定于屏幕的方法,并且根据我启动的测试,我需要ScreenModule 来包含平台模块之一。

我需要从ScreenModule 访问::Operations,但它找不到任何这些方法。

我不知道为什么我不能使用wait_for_elements_exist,即使我已经包含了::Operations moudle

【问题讨论】:

    标签: ruby cucumber calabash


    【解决方案1】:

    简短的回答是您的模块中没有包含正确的模块。

    include Calabash::Cucumber::WaitHelpers
    

    更长的答案取决于您要做什么。由于您似乎是 ruby​​ 新手,因此我推荐这种方法:

    # features/steps/my_steps.rb
    module MyApp
      module A
        def visible(mark)
           wait_for_element_exist("* marked:'mark'")
        end
      end
    end
    
    World(MyApp::A) # Add your module to the Cucumber World.
    
    Then(/^I touch next$/) do
      visible("Next")
      touch("* marked:'Next'")
    end
    

    您可以在CalSmoke project 中查看此示例。

    这是您将模块加载到 Cucumber 中的方式。

    根据您的评论,我了解到您希望使用页面对象模型 (POM)。

    我们在x-platform-example repoXamarin docs 中提供了如何执行此操作的示例。

    您需要了解 ruby​​ 如何加载代码和如何向 Cucumber 公开方法是有区别的。

    如果你是 ruby​​ 新手,我推荐这本书Metaprogramming in Ruby。如果你是 Cucumber 的新手,我推荐这个资源。

    【讨论】:

    • 我在这里尝试创建的是一个用于页面对象方法的模块,可用于 Android 和 iOS 页面对象,因此我可以在两个平台之间共享步骤定义。例如,我的一个步骤是:Then(/^I see login screen$/)do ... create login page object... login_screen.visible login_screen.do_stuff 我想为visibledo_stuff 等方法创建模块,这样我就可以在iOS 和Android 页面类中包含这些模块。我通过在模块 A 中扩展 Calabash::Cucumber::Operations 来获得对操作的访问权限。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-19
    • 2020-06-16
    相关资源
    最近更新 更多