How to detect whether android app is running UI test with Espresso

后端 未结 8 1931
-上瘾入骨i
-上瘾入骨i 2020-12-03 06:56

I am writing some Espresso tests for Android. I am running in the the following problem:

In order for a certain test case to run properly, I need to disable some fea

8条回答
  •  南方客
    南方客 (楼主)
    2020-12-03 07:36

    You can use SharedPreferences for this.

    Set debug mode:

    boolean isDebug = true;
    
    SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putInt("DEBUG_MODE", isDebug);
    editor.commit();
    

    Check if debug mode:

    SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
    boolean isDebug = sharedPref.getBoolean("DEBUG_MODE", false);
    
    if(isDebug){
        //Activate debug features
    }else{
        //Disable debug features
    }
    

提交回复
热议问题