Search code examples
androidcucumbercalabashcalabash-android

How to create a custom calabash step to run a command?


I am using calabash-android to test my app. I want to create a custom step which executes an adb command.

This is what I tried:

I created the following custom step which takes no arguments (I created it under step_definitions/ folder):

Run adb command for our app do |cukes|
   system("adb devices")
end

In my_first.feature, I call above step like this:

Feature: My feature

  Scenario: My scenario
    Run adb command for our app

When I run the test with the command calabash-android run myApp.apk, I get an error message:

syntax error, unexpected tIDENTIFIER, expecting keyword_do or '{' or '('
Run adb command for our app do |cukes|

Where am I wrong? How to create a simple step which takes no argument & just runs one adb command?


Solution

  • Several issues:

    • Run isn't a Gherkin identifier. Start your step with Given, When, Then, And or But.
    • Step definitions don't have the bare wording of the step, but a regular expression that matches it.
    • The number of block parameters should match the number of capture groups in the regular expression. In this case there aren't any, so there should be no block parameters.

    This should work:

    features/my_first.feature

    Feature: My feature
    
      Scenario: My scenario
        When I run the adb command for our app
    

    step_definitions/my_first_steps.rb

    When /^I run the adb command for our app$/ do
      system("adb devices")
    end