Search code examples
androidcalabashcalabash-android

Calabash - Android - Finding Ids and handling placeholders


I'm completely new to Calabash, have spent the day learning (including getting all setup).

One thing i need some help with is how to find IDs with ease. So i have an Android app, which contains the placeholder text Username.

I've tried the following however its simply not working (I keep getting a timeout error that I presume is down to it not being able to find):

When I enter "[email protected]" as "Username"

So my questions:

1- is the above actually correct if i want to enter that email into a field with Username 2- If i didnt have a placeholder like above how can i easily obtain References or IDs and use that in Then /^I enter "([^\"]*)" into input field number (\d+)$/

Many Thanks.


Solution

  • To find locators you should use the calabash console. If you run
    calabash-android console your_app.apk
    then once it starts
    reinstall_apps
    and then
    start_test_server_in_background.

    Once it's running you can use the calabash query syntax to find the elements you want to interact with - https://github.com/calabash/calabash-ios/wiki/05-Query-syntax.

    To get you started query("*") will return everything that is currently on screen.

    query("id:'UserNameField'") would only return the element with the id UserNameField.

    To check if it's the one you actually want you can use flash("id:'UserNameField'") to make it flash a few times on your emulator/device.

    Then to interact with it, I would advise not using the pre written steps. Make your own step definition and remember to require in the files you need to use calabash if you haven't already

    require 'calabash-android/operations'
    
    When /^I enter the username (.*)$/ do |username|
      enter_text("id:'UserNameField'", username)
    end
    

    The prewritten steps can be useful but they end up making your scenarios hard to read, and once you do some more complicated things with it you will probably have to write your own steps anyway.

    To help you decide the correct commands for your step definition it can be useful to run the commands, e.g. enter_text("id:'UserNameField'", 'username_you_want') in the calabash console so you don't have to run your tests everytime you add a line.