当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Calabash step to check if application is being run for the first time

问题描述:

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)

网友答案:

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

网友答案:

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.

网友答案:

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();

    }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: