Krumons
Krumons

Reputation: 61

Calling cucumber methods from a module

I'm writing calabash tests using Ruby 2.3.0 and I'm having trouble calling cucumber methods from a module.

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

And :

Class B
    include A
end

When I call B.visible or define this in B :

def visible
    A::visible
end

And call B.visible, I get NoMethodError indicating wait_for_elements_exist in module A.

I've tried to require 'calabash-cucumber/cucumber' in the module and include Calabash::Cucumber in the module. It didn't work.

I can access all cucumber methods from other classes in my project, and I have required the cucumber in my env.rb so the library is loaded. I'd like to know how to access library functions from a module, or how to properly include things into my modules.

EDIT

I tried include Calabash::Cucumber::WaitHelpers

And include Calabash::Cucumber::Operations

Did not work. I replaced include with extend, and now I can access the methods, but that won't solve my problem.

What I need is

AndroidModule < Calabash::Android::Operations

IosModule < Calabash::Cucumber::Operations

In these modules I define methods that are different between platforms.

Then there's ScreenModule for various screens, where I define methods specific for the screen, and based on the test I launch, I need ScreenModule to include one of the platform modules.

I need access to ::Operations from ScreenModule, but it won't find any of those methods.

I can't figure out, why I can't use wait_for_elements_exist, even if I've included ::Operations moudle

Upvotes: 2

Views: 580

Answers (1)

jmoody
jmoody

Reputation: 2480

The short answer is that you are not including the right module in your module.

include Calabash::Cucumber::WaitHelpers

The longer answer depends on what you are trying to do. Since it seems you are new to ruby, I recommend this approach:

# 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

You can see an example of this in the CalSmoke project.

This is how you load modules into Cucumber.

Based on your comment, I see that you want to use the Page Object Model (POM).

We have examples of how to do this in the x-platform-example repo and in the Xamarin docs.

You need to understand that there is a difference between how ruby loads code and how to expose methods to Cucumber.

If you are new to ruby, I recommend this book Metaprogramming in Ruby. If you are new to Cucumber, I recommend this resources.

Upvotes: 1

Related Questions