安卓UIAutomator /咖啡:在一个特定的网址打开的浏览器

问题描述 投票:0回答:1

我与UIAutomator /咖啡和我的应用程序玩耍。其实我想做到以下几点:

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

我搜索了堆栈溢出,但无法找到解决办法呢。

@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();
        }
    }
}
android android-intent android-espresso uiautomator start-activity
1个回答
1
投票

OK,大家好我想通了。这里是解决方案:

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);
© www.soinside.com 2019 - 2024. All rights reserved.