2

Looks like the calabash pre-defined steps are not enough for my usage.

My scenario is this:

- If user 1st time login, my app pops up a view which contains EditText field ask user to input an nickname, then user input the name e.g. "John" and press OK button, then my app popup a view contain text "Ready to go".

- If user is not the 1st time login, my app pops up a view which contains text "Ready to go"

(In my app code, I check if there is unique id persisted, if not, it is 1st time login)

I might be wrong but I guess I need to write my own step definition in Ruby for this scenario, I looked at the sample code below:

Then /^I see the text "([^\"]*)"$/ do |text|
  macro %Q|I should see "#{text}"|

I get lost. My questions are:

Q1. Is it possible to invoke Android SDK API from calabash so that I can also check if this is 1st time login or not from ruby code?

Q2. If the answer for above question is no, how can I use calabash to test my case if the view could be different when login 1st time vs Not 1st time login. (Imaging I need to run the test on some devices multiple times, the 1st time login view is different than the rest times)

Rogério Peixoto
  • 2,176
  • 2
  • 23
  • 31
Leem.fin
  • 40,781
  • 83
  • 202
  • 354
  • Since the only one interacting with the phone is the test-scripts, you should know if the app has been started yet, not by inspecting the phone, but by saving the state in the test scripts – Tobias Jul 25 '16 at 02:46

3 Answers3

0

If you're testing an Android application, it's is very likely that this application creates a shared preference file.

So you can use adb to check if this file is present and/or parse it's content.

Do it manually first, by trying to list and inspecting the files created by the application, to map in what situation something changes that could give you a sign that it's not the first login:

# listing some app's prefs
$ adb run-as com.myapp.package ls shared_prefs/

# some file contents
$ adb run-as com.myapp.package cat shared_prefs/someprefs.xml

The following example function runs an adb command to list the files inside shared_prefs directory that is located inside the application data directory:

def list_shared_pref(your_app_package_id)
    `adb -s #{ENV['ADB_DEVICE_ARG']} run-as #{your_app_package_id} ls shared_prefs/`
end

note that the -s modifier is optional, it's there for selecting an specific serial number if you have multiple devices connected at the same time, the serial number should be exported as an environment variable (ADB_DEVICE_ARG).

And you need to provide the current application package id (something like com.android.camera)

Lets say that you've found that when the user performs the first login the app creates a Shared Preference called firstLogin.xml, then you could do the following:

def is_first_login
    list_shared_pref('com.yourapp.packageid').include? "firstLogin.xml"
end

You can call that function in your custom step declared inside the step_definitions directory:

Given(/^I'm not in first login$/) do
  unless is_first_login
      # DO STUFF
  else
      raise "Error: Did not expected to be the first login"
  end

  # optional pause
  sleep(STEP_PAUSE)
end

UPDATE:

References for my answer. Determine if android app is the first time used

Community
  • 1
  • 1
Rogério Peixoto
  • 2,176
  • 2
  • 23
  • 31
  • Thank you! This is very helpful, I will give a try. Actually, I have another question, is it possible to check the trust credential storage of the device in step definition in Ruby? I could also post another dedicated question about this. – Leem.fin Jul 22 '16 at 10:08
  • @Leem.fin were you able to find something that could indicate a first run by inspecting the data/shared prefs directory? – Rogério Peixoto Jul 23 '16 at 22:15
  • @Leem.fin I don't know about the trust credential storage of the device. – Rogério Peixoto Jul 23 '16 at 22:15
  • I don't know why the down-vote since my answer was based in other solid discussion. – Rogério Peixoto Jul 25 '16 at 12:20
  • No worries, I upvoted it back :) I am also wondering why it is downvoted. I am going to try your answer next week. Thanks. – Leem.fin Jul 28 '16 at 15:42
0

If you are looking for options These are some:

  1. You can either get device IMEI(SIM supported) else Build number or MAC from Bluetooth or Wi-fi. and check it for its presence, if not present in response send flag to show edit text screen else other.

But remember this unique key must be different.

NSNoob
  • 5,548
  • 6
  • 41
  • 54
0

you can use shared preference to do this

 public class Login extends Activity {

    SharedPreferences prefs = null;
    Button login;
    HashMap<String,String>params = new  HashMap<String,String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Perhaps set content view here

        prefs = getSharedPreferences("com.yourapp.packagename", MODE_PRIVATE);
        login = (Button)findViewById(R.id.button);
        login.setOnClickListener(new View.OnClickListener()
        {
             @Override
             public void onClick(View v)
               {
                  boolean firstloginfromapp = prefs.getBoolean("firstrun", true);

                  params .put ("firsttime",firstloginfromapp );
                  params .put ("userid",userid);
                  params .put ("password",password);

                  JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
            url, params,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {

                 try{
                    JSONObject responseOject=response;
                     if(responseOject.getString("status").equals("success"))
                         prefs.edit().putBoolean("firstrun", false).commit();
                }catch(Exception e){}
               }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    // hide the progress dialog
                    pDialog.hide();
                }
            });

               }
             });
    }

    @Override
    protected void onResume() {
        super.onResume();

    }
}
SaravInfern
  • 3,338
  • 1
  • 20
  • 44