"Query" and Other Helpers of Testing with Calabash

There is a number of mobile automation testing tools out there and Calabash is the one which is definitely worth mentioning. In the previous article we explored how to set up calabash testing tool and determined that it is a very convenient mobile testing framework.

In combination with Cucumber, Calabash helps to write clear and useful tests. The only difficulty I faced with an iOS mobile application testing was to find the elements required to execute steps. There is a handy solution for this issue in Calabash testing and we will review it in this tutorial.

A great helper in this situation is the "query" method of Calabash iOS Ruby API. This method allows by means of console to display all UI elements, located on the screen of your application.

Let's сonsider some examples and look at their implementation in Calabash automation tool:

  • You want to see all the visible elements on the screen of the application. To do this, we enter a request in calabash ios console:
irb(main):001:0> query("view")

As a result, we get:

Mobile testing with Calabash

  • Now, we turn to the search for a particular element. For example, a button. Pass the following request via console:
irb(main):002:0> query ("UIButton")

In response to this request, the console gives a list of all the elements visible in the current view of the screen:

Mobile testing with Calabash

We can see, that there is one button "Sign out" on the screen.

Now we can use this button in the step of quitting the application:

Then  I logout from application

Calabash step will look like this:

Then(/^I logout from application$/) do
  touch("UIButton marked:'Sign out'")

Thus, the query request helped us to find the "Sign out" button.

  • Let's look at the case when we need to find an element with the specific text. We submit the command to the console:

Here we get:

Mobile testing with Calabash

There are several elements matching this command. Then we narrow the request to search for an item with the text "ddd".

query("label marked:'ddd'")

Console finds the matching element:

Mobile testing with Calabash

When we detected the required element, we can use the flash command to "highlight" it on the device and check the accuracy of its recognition.

flash("label marked:'ddd'")

It happens, that even the Query request can not help to find the right element. For example, there are several identical elements. In this case, you have to use the step in which we indicate the exact coordinates of the element. This step will look like this:

And   I touch on screen 20 from the left and 160 from the top

And its implementation:

Then /^I (?:press|touch) on screen (\d+) from the left and (\d+) from the top$/ do |x, y|
  touch(nil, {:offset => {:x => x.to_i, :y => y.to_i}})

Fortunately, it happens rarely. The request Query helps to find required element and use it for writing the test.

To sum up, I would like to add that Calabash makes our life easier. When you release your app, you have a lot of worries and selecting proper software testing tools is one of them. If you have a rapid development cycle, tight schedule and do not have time to test your app on all devices, functional testing tools are extremely helpful. Calabash is able to relieve your stress. With this mobile testing tool, you always know the state of your apps even as platforms evolve.