【问题标题】:Get Activity instance of other app using UiAutomator?使用 UiAutomator 获取其他应用程序的 Activity 实例?
【发布时间】:2017-01-12 08:29:33
【问题描述】:

我正在使用 UiAutomator 在我的应用程序中跨应用程序编写一些自动化测试用例。我的目标是找到我点击的所有应用的当前活动。

我有一个名为 MyApp 的项目,其中包含一个名为 com.example 的包和一个 Activity,MainActivity

我尝试了以下(androidTest 下我的应用程序中的所有内容)

public class ActivityTester extends InstrumentationTestCase {

private UiDevice device;

@Test
public void testAdd() throws Exception {

}

@Override
protected void setUp() throws Exception {
    super.setUp();

    Instrumentation instrumentation = getInstrumentation();

    Instrumentation.ActivityMonitor monitor = instrumentation.addMonitor("com.example.MainActivity", null, false);


    device = UiDevice.getInstance(instrumentation);

    device.pressHome();

    device.wait(Until.hasObject(By.desc("Apps")), 3000);

    UiObject2 appsButton = device.findObject(By.desc("Apps"));
    appsButton.click();

    device.wait(Until.hasObject(By.text("MyApp")), 3000);

    UiObject2 calculatorApp = device.findObject(By.text("MyApp"));
    calculatorApp.click();

    Activity currentActivity = instrumentation.waitForMonitorWithTimeout(monitor, 3000);

}

我在这里点击 HomeMenu 并启动 Myapp 并使用 com.example.MyActivity 连接到显示器,我可以得到这行代码中的activity实例

Activity currentActivity = instrumentation.waitForMonitorWithTimeout(monitor, 3000);

现在如果我改变流程。 HomeMenu --> SomeOtherApp 并使用 SomeOtherApp 的完全限定的 launcherActivity 连接到监视器,例如 com.someotherapp.MainActivity。 我无法获取活动实例。当前活动为空

有没有办法可以获取我通过 UiAutomator 启动的任何应用的当前 Activity 实例?

【问题讨论】:

    标签: android android-uiautomator android-espresso android-junit


    【解决方案1】:

    似乎只能监控同一包中的活动, 对于另一个应用的活动,waitForActivityWithTimeout 只返回 null。

    如果你只获取 context-needed-api 的活动实例,可以像我一样跟随

    1. 在同一个 ui-automator-test-class 包中启动活动
    2. 使用监视器获取该活动
    
        public class ExampleInstrumentedTest {
    
          private UiDevice mDevice;
          private Activity mActivity;
    
          @Before
          public void before() {
            // Initialize UiDevice instance
            mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    
            mActivity = getActivity();
    
            // Start from the home screen
            mDevice.pressHome();
          }
    
          private Activity getActivity() {
            Activity activity = null;
            Instrumentation inst = InstrumentationRegistry.getInstrumentation();
            Context context = inst.getContext();
            Instrumentation.ActivityMonitor monitor =
                    inst.addMonitor("com.wispeedio.uiautomator2hello.MainActivity", null, false);
    
            Intent intent = context.getPackageManager()
                    .getLaunchIntentForPackage("com.wispeedio.uiautomator2hello");
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            context.startActivity(intent);
            try {
              Thread.sleep(2000);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
            activity = monitor.waitForActivityWithTimeout(2000);
    
            return activity;
          }
    
          private void takeScreenshot(String name) {
            File file = Utils.CreateFileToExternal(mActivity, name, "test-screenshots");
            mDevice.takeScreenshot(file);
          }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-03
      相关资源
      最近更新 更多