2014-03-04 56 views
18

您好我是新來的Android Junit的測試:安卓:java.lang.SecurityException異常:注射到另一個應用程序需要INJECT_EVENTS許可

我已經寫在MainActivityFunctionalTest.java一些測試代碼文件

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

    } 


    } 

但是,我得到一個錯誤: java.lang.SecurityException異常:注射到另一個應用程序需要INJECT_EVENTS許可

在com.example.myfirstapp2.test.MainActivityFunctionalTest.testStartSecondActivity(MainActivityFunctionalTest.java:70)

TouchUtils.clickView(this, view); 

請幫

+0

INJECT_EVENTS權限將被添加到清單文件中。 –

+0

嗨,我已經這樣做了,但它仍然不起作用 – user3238961

+1

http://stackoverflow.com/questions/5383401/android-inject-events-permission?rq=1看看這個鏈接 –

回答

7

我面臨這個同樣的問題我自己,這裏是我發現了什麼關於這個問題。

1)將INJECT_EVENTS權限添加到您的應用程序中,使Android Studio指出此權限「僅授予系統應用程序」。而且,對於manifest.permissions,Google's reference guide聲明此權限「不適用於第三方應用程序」。

現在,很可能您的應用與我的一樣,不是系統應用。因此添加此權限絕對不是一件好事,而且幸運的是不會適用於您的第三方項目。至少在Android Studio上開發時。

2)我可以看到,在你的setUp方法中,你調用了setActivityInitialTouchMode(false);正如Google's best practices for UI testing所指出的那樣,在測試UI時,必須將觸摸模式設置爲true。否則,您的測試夾具將無法與UI元素交互。

3)還有一件事。這是一個模擬用戶對應用程序的操作的自動化測試。如果我們與設備進行交互(真實或虛擬,無關緊要),我們很可能會讓其他事情獲得關注(即使在測試中的應用程序內部),然後會與觸摸模式設置發生衝突,即setUp方法已經表演過。

最終,這就是發生在我身上的事情。我簡單地通過不點擊/觸摸/與正在運行測試的設備交互來解決了我的問題。

6

這是因爲您的設備被鎖定/任何其他打開的對話框打開 /任何阻止測試點擊按鈕的能力。例如。如果電話被鎖定 - 當測試嘗試點擊按鈕時,它不能因爲設備被鎖定。

我在模擬器上遇到麻煩,因爲它總是顯示「啓動器崩潰」。因此,無論何時嘗試點擊該按鈕,都不會因爲警報對話框打開。

總之。確保您的屏幕已解鎖,並且沒有消息框干擾測試,並且可以點擊按鈕。

-1

一些更多的方式來解決「注入到另一個應用程序需要INJECT_EVENTS許可」與TouchUtils發生......

EG。官方Android開發者網站顯示:

// Stop the activity - The onDestroy() method should save the state of the Spinner 
mActivity.finish(); 

// Re-start the Activity - the onResume() method should restore the state of the Spinner 
mActivity = getActivity(); 

然而,如果在測試方法,這是由TouchUtils.clickView直接跟着這可能會導致錯誤:

// Stop the activity - The onDestroy() method should save the state of the Spinner 
mActivity.finish(); 

// Re-start the Activity - the onResume() method should restore the state of the Spinner 
mActivity = getActivity(); 

// Possible inject error! 
TouchUtils.clickView(this, someView); 

然而,拆分爲兩個測試方法並允許setUp()在中間運行似乎可以解決問題(注意,這是由方法名稱在測試按字母順序運行時由方法名稱控制):

*在調用intent後仍然可能失敗,但不會在完成之後更長的時間被稱爲

public void testYTestFinishing() { 

    TouchUtils.clickView(this, someView); 

    // Finish & restart the activity 
    activity.finish(); 
} 

// ------------------------------------------- 
// Called before every test case method 
@Override 
protected void setUp() throws Exception { 
    super.setUp(); 

    setActivityInitialTouchMode(true); 

    activity = getActivity(); 

    getViews(); 
} 
// ------------------------------------------- 

public void testZOnReturn() { 

    TouchUtils.clickView(this, someView); 

} 

有趣的是,把什麼是在設置()之前TouchUtils既可以失敗,並且工作:

public void testYTestFinishing() { 

    TouchUtils.clickView(this, someView); 

    // Finish & restart the activity 
    activity.finish(); 

    setActivityInitialTouchMode(true); 

    activity = getActivity(); 

    getViews(); 

    // SORRY, this fails here on some builds and succeeds on others 
    TouchUtils.clickView(this, someView); 
} 

您也可以在TouchUtils之前直接嘗試waitForActivity超時可*修復它在其他時間如意圖被調用後:

*注入錯誤仍然可能發生,如果在相同的測試方法中使用...將需要拆分成另一種方法,如上所示。

Instrumentation.ActivityMonitor monitor = getInstrumentation() 
     .addMonitor(Instrumentation.ActivityMonitor.class.getName(), 
     null, false); 

    // Wait for activity to fix inject error; Increase or decrease as needed 
    monitor.waitForActivityWithTimeout(2000); 

    // Should no longer fail 
    TouchUtils.clickView(this, someView); 
22

我有同樣的問題,我的代碼是這樣的(用於正常登錄活動):

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

最後一行與拋出:SecurityException崩潰。在最後一次文字輸入後發現,鍵盤保持打開狀態,因此下一次單擊被認爲是在不同的應用程序上。

要解決這個問題,我只需在打字後關閉鍵盤。我還必須添加一些睡眠以確保鍵盤已關閉,否則測試會偶爾中斷。所以最終代碼看起來像這樣:

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

這工作得很好。

+0

這對我有用,似乎有道理。我覺得奇怪的是,這個問題只發生在運行API 25的模擬器上。也想要注意的是,從API 23(Marshmallow)'INJECT_EVENTS'權限[**已被刪除**](https://developer.android.com/sdk/api_diff/23/changes.html)。 –

+0

對我來說,問題是浮在我想點擊的視圖上方的Facebook聊天頭。這也是一種FAB,因此乍一看看起來很相似。 :-) – ferini

3

對於有根的設備,this file幫了我很多。 它具有:

Injector.pressBackButton(); 
Injector.pressHomeButton(); 
Injector.pressPowerButton(); 
Injector.showNotificationCenter(); 
Injector.swipeLeftRight(); 
Injector.swipeRightLeft(); 
Injector.touch(x, y); 
14

我有同樣的問題,並添加closeSoftKeyboard()方法解決了對我來說。

onView(withId(R.id.view)).perform(typeText(text_to_be_typed), closeSoftKeyboard()); 
0

運行espresso測試時,我遇到了完全相同的問題和錯誤消息。當運行整個軟件包時,其中一個總是失敗,但是當我單獨運行它時總會失敗。有趣的是,這個問題發生,因爲我已經在AndroidManifest.xml中添加以下行到我的活動之一:

android:windowSoftInputMode="stateHidden" 

提到的測試駛過:

android:windowSoftInputMode="stateUnchanged|adjustResize" 

刪除或上述行更改爲後運行整個包時。

相關問題