Search code examples
androidiosrubycucumberappium

How do I use same Cucumber step definitions for android and iOS


I'm currently beginning an automation project for an app that is identical in flow and design for both Android and iOS. I'm using Ruby with the Cucumber framework.

I have begun automating Android and essentially what I need to do is each step definition to have separate code for android and ios kind of like this pseudo-code:

Then (/^I click the Login Button$/) do
 if mobile_platform = android
     #android locators and code here
 else
     #iOS locators and code here
 end 
end

How would I go about setting up my project to allow for such specific step definition usage?

Is it better to have separate feature and step definitions for each OS instead of trying to meld them together?

Thanks for any and all help you can give me.


Solution

  • Given the commonality between the apps, it makes sense to share the feature files. A clean way to manage the platform-specific step_definitions is to keep them in separate directories.

    Take the following simple example project.

    simple cucumber project

    You can switch between the alternative step definition folders using the cucumber -r (require) option like so:

    cucumber -r features/step_definitions_android -r features/support
    

    Note that automatic loading is disabled as soon as you use the -r option, hence why you need to explicitly include the second require to pull any code you have in the features/support folder.

    To make it easy to run against the alternative targets, you can create corresponding profiles:

    # cucumber.yaml
    android: -r features/step_definitions_android -r features/support
    ios: -r features/step_definitions_ios -r features/support
    

    As you can see below, when each of these profiles are run, the relevant platform-specific step definitions are invoked.

    run cucumber with alternative profiles