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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



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.




6 Answers
6



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





This is exactly the sort of thing I was looking for. Thank you.
– Jesse Fish
Apr 13 '17 at 15:28



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$/) do
if $mobile_platform = 'android'
#android locators and code here
else
#iOS locators and code here
end
end



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.



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 here
else
#iOS locators and code here



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



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( ...)
// Only start thinking about IOS or Android from here down.
...
end
end



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's
def ios_add_friend
def droid_add_friend

#return early from os specific functions if wrong OS
def ios_add_friend
return if droid?
...
end

# run both implementations when they are different
def add_friend
ios_add_friend
droid_add_friend
end

# loads of other alternatives

...



The (-r) require option alone won't fix this issue. Let's say, you define the steps in two different folders,


(-r) require


--features
----step_definitions_android
----step_definitions_ios



The solution is to enable (-e) exclude option instead of setting the steps to be required.


(-e) exclude


cucumber -e features/step_definitions_android -r features/scenarios/


cucumber -e features/step_definitions_ios -r features/scenarios/






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS