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.
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.
The
(-r) require
option alone won't fix this issue. Let's say, you define the steps in two different folders,The solution is to enable
(-e) exclude
option instead of setting the steps to be required.Exec cmd for IOS
Exec cmd for Android
Why don't you add a line to the cucumber.yml that denotes whether you are using android or ios?
And in an environment file, you could do this:
And then in your step definition files, you can start doing this:
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
But 95% of your code should work for both the O.S.
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.
You can switch between the alternative step definition folders using the cucumber -r (require) option like so:
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:
As you can see below, when each of these profiles are run, the relevant platform-specific step definitions are invoked.
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
this needs to become
implemented by
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.