Mobile Software Test Automation: Step Definitions

Last week we went through creating a .feature file, so now it's time to hook these into step definitions.  But first, I think it's time we had a quick discussion on about the tool we will use to write code.  I wanted to include this in last week's post, but I didn't want to make it too long, and this section should be shorter, so here it goes.

IDE or Text Editor

There are a host of options out there we can use to develop software test automation, but they break down into one of two categories: Integrated Development Environment (IDE) or a simple Text Editor.  Every developer is going to have their own opinion of which is best, but it primarily depends on one's habits.  Everything we will be doing can be done using a text editor like Sublime Text or even using a vim or nano editor from the command line.  If you are looking for a free product that also gives you a little syntax help, I recommend Sublime Text.  I even use it as my notepad from time to time, and it is a great product.  

However, as far as coding goes, I feel that there really isn't a replacement for a good IDE.  In addition to having a good source code editor, and IDE also gives you intelligent code completion, a class hierarchy diagram, and a class and object browser.  Also, an IDE will compile code on the fly to instantly point out syntax errors, incorporate version control, branch compare with visual diff, and quick refactoring.  I'm sure I missed a bunch of great advantages to a good IDE, but these are the ones important to me.  The only downfall, oftentimes, is cost.  My IDE of choice is RubyMine.  They have recently changed their pricing structure, but it is relatively inexpensive for what you get, and you can try it free for 30 days.

All that to say, a good IDE can be very helpful, especially if you are just starting out.  Yes, you can do everything in the command line or with a text editor for free, but you will miss out on a lot of benefits by doing so.  Also, some muscle memory may be involved for me, as I learned Java building apps in Eclipse and then Android Studio, and I also develop web and mobile automation using IntelliJ IDEA, which is JetBrain's open source IDE that Android Studio was built on.  So for me, it was a no-brainer based off my familiarity with the JetBrains product line.  From here on out, I will mostly show examples from RubyMine, but if you chose a free editor, examples will look very similar if you are using Sublime Text.

Step Definitions

So you have a Gherkin .feature file(s) created? Great.  Now lets start hooking them in so Cucumber can pick them up to run a test.  In your step_definitions folder we generated a few posts ago, you will want to create a .rb file with a name describing the contained feature.  Here is an example of what I mean. 

step.definition.sample

On the left hand side, is the file structure from finder, and on the right, is the file structure as seen in RubyMine.  It's the exact same.  You will also notice from the sample, that step definition files are divided into categories.  For example, all the steps that are specific to the help screen are contained in the helpScreen_steps.rb file.  It is not essential, but this helps keep everything organized and easier to maintain.  Who wants to comb through 1000+ lines of code to find the step definition you are looking for?  

Anyways, create the file that you need.  Then you will copy over the steps from your .feature file.  The only difference here is that the syntax will be different.  Here is a screenshot for reference:

help-screen-steps-sample

You will see the keyword listed in red, followed by a parentheses and some symbols around the remainder of your step.  The (/^Any text here$/) format is how this is done in Ruby on Rails.  Also, there is a code block set up here between the 'do' and the 'end', which is where we will put our page object oriented code.  For now, all you will need to do is create one of these blocks for each step you have.  Keep in mind, the step is technically anything between the parenthesis, and you can only define each step once.  If you are using an IDE, you will get an error if defining this more than once, whereas if you are using a text editor, you will get an error when the code is run.

Sample Project

If you are following along with the sample project, here is what the step definitions should look like, following what we learned today. 

sampleProject_step_definitions

That's all for this week.  Next we will create page support to allow these step definitions to interact with mobile objects for each platform, so stay tuned.