Hi there I am new to Android Junit testing:
I have written some test code in MainActivityFunctionalTest.java file
MainActivityFunctionalTest.java:
package com.example.myfirstapp2.test; public class MainActivityFunctionalTest extends ActivityInstrumentationTestCase2<Login>{ private static final String TAG = "MainActivityFunctionalTest"; private Login activity; public MainActivityFunctionalTest() { super(Login.class); } @Override protected void setUp() throws Exception { Log.d(TAG,"Set-Up"); super.setUp(); setActivityInitialTouchMode(false); activity = getActivity(); } public void testStartSecondActivity() throws Exception { // add monitor to check for the second activity ActivityMonitor monitor = getInstrumentation(). addMonitor(DisplayMessageActivity.class.getName(), null, false); //addMonitor(MainActivity.class.getName(), null, false); // find button and click it Button view = (Button) activity.findViewById(R.id.btnLogin); // TouchUtils handles the sync with the main thread internally TouchUtils.clickView(this, view); // to click on a click, e.g., in a listview // listView.getChildAt(0); // wait 2 seconds for the start of the activity DisplayMessageActivity startedActivity = (DisplayMessageActivity) monitor .waitForActivityWithTimeout(5000); assertNotNull(startedActivity); // search for the textView TextView textView = (TextView) startedActivity.findViewById(R.id.Email); // check that the TextView is on the screen ViewAsserts.assertOnScreen(startedActivity.getWindow().getDecorView(), textView); // validate the text on the TextView assertEquals("Text incorrect", "1http://www.vogella.com", textView.getText().toString()); // press back and click again this.sendKeys(KeyEvent.KEYCODE_BACK); TouchUtils.clickView(this, view); } }
However,I get an error: java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission
at com.example.myfirstapp2.test.MainActivityFunctionalTest.testStartSecondActivity(MainActivityFunctionalTest.java:70)
TouchUtils.clickView(this, view);
Please help
I had the same problem, and my code was something like this (for a normal login activity):
onView(withId(R.id.username)) .perform(new TypeTextAction("test_user")); onView(withId(R.id.password)) .perform(new TypeTextAction("test123")); onView(withId(R.id.login)).perform(click());
The last line was crashing with SecurityException. Turned out after the last text typing, the keyboard was left open, hence the next click was considered on a different application.
To fix this, I simply had to close the keyboard after typing. I also had to add some sleep to make sure the keyboard is closed, otherwise the test would break every now and then. So the final code looked like this:
onView(withId(R.id.username)) .perform(new TypeTextAction("test_user")); onView(withId(R.id.password)) .perform(new TypeTextAction("test123")).perform(closeSoftKeyboard()); Thread.sleep(250); onView(withId(R.id.login)).perform(click());
This worked just fine.
I had the same issue and adding the closeSoftKeyboard() method resolved it for me.
onView(withId(R.id.view)).perform(typeText(text_to_be_typed), closeSoftKeyboard());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With