Skip to content Skip to sidebar Skip to footer

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 aut

Solution 1:

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

Solution 2:

Why don't you add a line to the cucumber.yml that denotes whether you are using android or ios?

mobile_platform:'android'

And in an environment file, you could do this:

require'yaml'
cucumber_options = YAML.load_file('cucumber.yml')
$mobile_platform = cucumber_options['mobile_platform']

And then in your step definition files, you can start doing this:

Then (/^I click the Login Button$/) doif$mobile_platform == 'android'#android locators and code hereelse#iOS locators and code hereendend

Solution 3:

I would not go for separate feature files for different operating system. You want your application to behave the same way independent of the operating system. Chances are that your features will diverge if you have two of them.

My approach would be to do two execution and separate the target environments down in the stack. I would use an environment variable to indicate which operating system I'm currently targeting.

The code that will execute something on the particular environment will be very different so I would use a factory to select the current implementation to use. I would not consider separating the execution using multiple conditions in the code as your small example suggest. The only place where I would have a condition of that type would be in the factory method that creates the actual class that will use your application.

Solution 4:

You should go with single feature file having no dependency on any O.S.

If you found any such case where you have to separate out operation depending on O.S you have add checks for it.Like you shown above

if mobile_platform = android
     #android locators and code hereelse#iOS locators and code here

But 95% of your code should work for both the O.S.

Solution 5:

As Thomas says the key to make this simple is to push things down the stack. To do this you need to apply a simple pattern with quite alot of discipline.

The pattern is to make each step definition implementation a single call to a helper method. Once you are in your helper methods then you can use techniques like using an environment variable, configuration or some conditional to choose implementation.

An example might clarify this. Lets say both apps have the capability to add a friend. When you first add this functionality you will have a step like

When'I add a friend' do
  fill_in first_name: 'Frieda'
  fill_in last_name: 'Fish'
  ...
end

this needs to become

When'I add a friend' do
   add_friend name: 'Frieda'
end

implemented by

module FriendStepHelper
  def add_friend( ...)
    //Onlystart thinking about IOS or Android from here down.
    ...
  endend

Now this might seem a bit of a pain, but what you have done is remove this problem from the realm of Cucumber (which is not designed to deal with this sort of problem) and moved it into the realm of Ruby which certainly is designed to deal with this sort of problem.

Now you are in you programming language you can use all sorts of techniques to make applying your conditional elegant and simple e.g.

#use hungarian prefix'sdefios_add_frienddefdroid_add_friend#return early from os specific functions if wrong OSdefios_add_friendreturnif droid?
  ...
end# run both implementations when they are differentdefadd_friend
  ios_add_friend
  droid_add_friend
end# loads of other alternatives

...

Post a Comment for "How Do I Use Same Cucumber Step Definitions For Android And Ios"