6

私は私のuiautomatorテストケースを持っています:

public class clickTest extends UiAutomatorTestCase {   

   public void myTest() throws UiObjectNotFoundException {   
       ...
       //Is it possible to get Context or Activity here?  

   }
}

で取得ContextまたはActivityインスタンス化することは可能UiAutomatorTestCaseですか?

またはどのように入るPackageManagerUiAutomatorTestCaseですか?

4

5 に答える 5

0

uiautomator テスト ケースから直接コマンドを実行することで、PackageManager を使用できます。

Runtime.getRuntime().exec("pm uninstall com.example.MyApp");

ただし、コンテキストやアクティビティなどへのアクセスが必要な場合は、Android の InstrumentationTestRunner を使用するのが最適な場合があります。詳細については、優れた Android ドキュメントを参照してください。 http://developer.android.com/tools/testing/activity_test.html

于 2015-03-20T15:11:56.743 に答える
0

これは、UiAutomator 2.0 以降で可能です。

UiAutomator の元のバージョンは、シェル プログラム ( adb shell uiautomator runtest ...) として実行されました。これは Android アプリケーションとして実行されていなかったため、アプリケーションの Context オブジェクトにアクセスできませんでした。

UiAutomator 2.0 はAndroid Instrumentationに基づいています。テストは APK にコンパイルされ、アプリケーション プロセスで ( 経由でadb shell am instrument ...) 実行されます。テストがInstrumentationTestCaseを拡張する場合、使用getInstrumentation().getContext()して Context を取得できます。

詳細については、UiAutomator のドキュメントを参照してください。

于 2015-08-01T00:19:53.683 に答える
0

これは機能します-

InstrumentationRegistry.getInstrumentation().targetContext
于 2019-01-06T11:36:56.853 に答える