Android UIAutomator/espresso : open browser on a specific URL

标签 android android-intent android-espresso android-uiautomator start-activity

我正在玩弄 UIAutomator/espresso 和我的应用程序。其实我想做以下事情:

  1. 对我的应用程序执行一些操作/检查

  2. 打开默认浏览器访问 URL

  3. 返回我的应用程序并重新检查界面

实际上我做不到第 2 步。 以下是我尝试过的不同方式:

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://stackoverflow.com/"));

mActivityTestRule.launchActivity(browserIntent);
InstrumentationRegistry.getTargetContext().sendBroadcast(browserIntent);
InstrumentationRegistry.getInstrumentation().startActivitySync(browserIntent);

我在 Stack Overflow 上搜索过,但还没有找到解决方案。

@LargeTest
@RunWith(AndroidJUnit4.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class BackgroundForegroundTest {

    @Rule
    public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);

    private UiDevice mDevice;

    @Before
    public void before() {
        // Initialize UiDevice instance
        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

        assertThat(mDevice, notNullValue());
    }

    @Test
    public void testOpenBrowserAndSwitchBack() throws InterruptedException {
        onView(withId(R.id.text1)).check(matches(isDisplayed()));

        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://stackoverflow.com/"));
        mActivityTestRule.launchActivity(browserIntent);
        Thread.sleep(8000);

        getBackToApp("My App");

        onView(withId(R.id.text1)).check(matches(isDisplayed()));
    }

    private void getBackToApp(String appDescription){
        try {
            mDevice.pressRecentApps();
            UiSelector selector = new UiSelector();
            UiObject recentApp = mDevice.findObject(selector.descriptionContains(appDescription));
            recentApp.click();
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}

最佳答案

好的,伙计们,我想通了。这是解决方案:

Context context = InstrumentationRegistry.getInstrumentation().getContext();
Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.android.chrome");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.setData(Uri.parse("https://stackoverflow.com/"));
context.startActivity(intent);
mDevice.wait(Until.hasObject(By.pkg("com.android.chrome").depth(0)), TIMEOUT);

关于Android UIAutomator/espresso : open browser on a specific URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48406999/

相关文章:

android - 滑动直到元素在 Android Espresso 中可见

android 应用程序自动更新如果 apk 存在然后删除

android - 如何从工具栏禁用 DrawerArrowToggle 或向右移动

android - 使用 Google Cloud Messenger token 订阅主题,收到无效参数

android - BiometricPrompt 面部解锁不起作用

在内存清理中幸存下来的 Android 应用程序

android - 如何切换到其他正在运行的应用程序

Android - espresso - 单击基于自定义对象的 ListView 条目

android-arrayadapter - 使用 espresso 工具测试自动完成 TextView

android - 安全异常 : Permission Denial: reading (only on emulator)