mardi 30 janvier 2018

How can I start any test class in the activity with the "startInstrumentation" code?

I am trying to write automation for my android device. when I search from the internet I see that I can Test Class(for exmple AirplaneMode) with startInstrumentation in the Activity Class(AirplaneActivity). However, what I have not been able to do. It prints "NOK" in the Activity Class. Can you help me enter the StartInstrumentation parameter incorrectly?

 @RunWith(AndroidJUnit4.class)
    public class AirplaneMode {
    private static UiDevice mDevice;
    private static Context mContext;

    @BeforeClass
    public static void init() throws IOException, UiObjectNotFoundException {

      mDevice=UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        mContext = InstrumentationRegistry.getContext();
    }

    @Test
    public void test1() throws UiObjectNotFoundException, IOException {

        mDevice.openQuickSettings();

    }

    @AfterClass
    public static void tearDownClass() throws Exception {
        mDevice.pressHome();

    }
}


// MyActivity
public class AirplaneActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast.makeText(getApplicationContext(),"airplane",Toast.LENGTH_LONG).show();

        if (startInstrumentation(new ComponentName("com.evrim.myapp.test/android.support.test.runner.AndroidJUnitRunner", "com.evrim.myapp.AirplaneMode"), null, null))
            Toast.makeText(getApplicationContext(),"OK",Toast.LENGTH_LONG).show();
        else Toast.makeText(getApplicationContext(),"NOK",Toast.LENGTH_LONG).show();
    }

}

Aucun commentaire:

Enregistrer un commentaire