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.
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: 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.
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.
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
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 ...
Execution failed for task ':app:transformClassesWithDexForDebug' Error while gradle build
Under USB-hub the USB_DEVICE_DETACHED broadcast event fired not on all detaching devices
How can I get place details from Google Places API for Android?
Android - Supports tablets' dimens.xml
How can I get the value of charging current (in mA) in an android application?
How to get data from Movement Method on TextView in Android
Find host activity of FragmentActivity
How to get contacts with email or phone number
Introducing Butterknife library causes runtime exception in fragment class when binding views
Parsing JSON with no title with volley tutorial slidenerd
How can I see where exactly exception occurred in Android Studio?
Difference in appearance of Button and ImageButton
How to retrieve the textColor attribute from a custom style
Android DroneKit over Bluetooth
Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Widget.Button.Inverse'
android icon flow menu