类android.support.test.uiautomator.By源码实例Demo

下面列出了怎么用android.support.test.uiautomator.By的API类实例代码及写法,或者点击链接到github查看源代码。

@Before
public void startMainActivityFromHomeScreen() {
    mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    mDevice.pressHome();

    final String launcherPackage = mDevice.getLauncherPackageName();
    assertThat(launcherPackage, notNullValue());
    mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
            LAUNCH_TIMEOUT);

    Context context = InstrumentationRegistry.getContext();
    final Intent intent = context.getPackageManager().getLaunchIntentForPackage(TARGET_PACKAGE);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    context.startActivity(intent);

    mDevice.wait(Until.hasObject(By.pkg(TARGET_PACKAGE).depth(0)), LAUNCH_TIMEOUT);
}
 
@Before
public void startMainActivityFromHomeScreen() {
    mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    mDevice.pressHome();

    final String launcherPackage = mDevice.getLauncherPackageName();
    assertThat(launcherPackage, notNullValue());
    mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
            LAUNCH_TIMEOUT);

    Context context = InstrumentationRegistry.getContext();
    final Intent intent = context.getPackageManager().getLaunchIntentForPackage(TARGET_PACKAGE);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    context.startActivity(intent);

    mDevice.wait(Until.hasObject(By.pkg(TARGET_PACKAGE).depth(0)), LAUNCH_TIMEOUT);
}
 
@Before
public void startMainActivityFromHomeScreen() {
    mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    mDevice.pressHome();

    final String launcherPackage = mDevice.getLauncherPackageName();
    assertThat(launcherPackage, notNullValue());
    mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
            LAUNCH_TIMEOUT);

    Context context = InstrumentationRegistry.getContext();
    final Intent intent = context.getPackageManager().getLaunchIntentForPackage(TARGET_PACKAGE);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    context.startActivity(intent);

    mDevice.wait(Until.hasObject(By.pkg(TARGET_PACKAGE).depth(0)), LAUNCH_TIMEOUT);
}
 
@Before
public void startMainActivityFromHomeScreen() {
    mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    mDevice.pressHome();

    final String launcherPackage = mDevice.getLauncherPackageName();
    assertThat(launcherPackage, notNullValue());
    mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
            LAUNCH_TIMEOUT);

    Context context = InstrumentationRegistry.getContext();
    final Intent intent = context.getPackageManager().getLaunchIntentForPackage(TARGET_PACKAGE);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    context.startActivity(intent);

    mDevice.wait(Until.hasObject(By.pkg(TARGET_PACKAGE).depth(0)), LAUNCH_TIMEOUT);
}
 
@Before
public void startMainActivityFromHomeScreen() {
    mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    mDevice.pressHome();

    final String launcherPackage = mDevice.getLauncherPackageName();
    assertThat(launcherPackage, notNullValue());
    mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
            LAUNCH_TIMEOUT);

    Context context = InstrumentationRegistry.getContext();
    final Intent intent = context.getPackageManager().getLaunchIntentForPackage(TARGET_PACKAGE);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    context.startActivity(intent);

    mDevice.wait(Until.hasObject(By.pkg(TARGET_PACKAGE).depth(0)), LAUNCH_TIMEOUT);
}
 
@Before
public void startMainActivityFromHomeScreen() {
    mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    mDevice.pressHome();

    final String launcherPackage = mDevice.getLauncherPackageName();
    assertThat(launcherPackage, notNullValue());
    mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
            LAUNCH_TIMEOUT);

    Context context = InstrumentationRegistry.getContext();
    final Intent intent = context.getPackageManager().getLaunchIntentForPackage(TARGET_PACKAGE);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    context.startActivity(intent);

    mDevice.wait(Until.hasObject(By.pkg(TARGET_PACKAGE).depth(0)), LAUNCH_TIMEOUT);
}
 
@Before
public void startMainActivityFromHomeScreen() {
    mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    mDevice.pressHome();

    final String launcherPackage = mDevice.getLauncherPackageName();
    assertThat(launcherPackage, notNullValue());
    mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
            LAUNCH_TIMEOUT);

    Context context = InstrumentationRegistry.getContext();
    final Intent intent = context.getPackageManager().getLaunchIntentForPackage(TARGET_PACKAGE);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    context.startActivity(intent);

    mDevice.wait(Until.hasObject(By.pkg(TARGET_PACKAGE).depth(0)), LAUNCH_TIMEOUT);
}
 
@Before
public void startMainActivityFromHomeScreen() {
    // Initialize UiDevice instance
    mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

    // Start from the home screen
    mDevice.pressHome();

    // Wait for launcher
    final String launcherPackage = mDevice.getLauncherPackageName();
    Assert.assertThat(launcherPackage, CoreMatchers.notNullValue());
    mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
            LAUNCH_TIMEOUT);

    // Launch the app
    Context context = InstrumentationRegistry.getContext();
    final Intent intent = context.getPackageManager()
            .getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE);
    // Clear out any previous instances
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    context.startActivity(intent);

    // Wait for the app to appear
    mDevice.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)),
            LAUNCH_TIMEOUT);
}
 
源代码9 项目: UIAutomatorWD   文件: MUiDevice.java
public List<UiObject2> findObjects(Object selector) throws Exception {
    uiDevice.waitForIdle();
    ArrayList<AccessibilityNodeInfo> accessibilityNodeInfos = ((NodeInfoList) selector).getNodeList();
    int size = accessibilityNodeInfos.size();
    List<UiObject2> list = new ArrayList<UiObject2>();
    if (size > 0) {
        for (int i = 0; i < size; i++) {
            AccessibilityNodeInfo node = accessibilityNodeInfos.get(i);
            if (node == null) {
                continue;
            }
            selector = By.clazz(node.getClassName().toString());
            UiObject2 uiObject2 = doFindObject(selector, node);
            list.add(uiObject2);
        }
    } else {
        return null;
    }
    return list;
}
 
源代码10 项目: UIAutomatorWD   文件: ElementController.java
private static BySelector getSelector(String strategy, String text) throws Exception {
    BySelector selector = null;
    switch (strategy) {
        case "CLASS_NAME":
            selector = By.clazz(text);
            break;
        case "NAME":
            selector = By.desc(text);
            if(selector == null || elements.getmDevice().findObject(selector) == null){
                selector = By.text(text);
            }
            break;
        case "ID":
            selector = By.res(text);
            break;
        case "TEXT_CONTAINS":
            selector = By.textContains(text);
            break;
        case "DESC_CONTAINS":
            selector = By.descContains(text);
            break;
    }
    return selector;
}
 
源代码11 项目: UIAutomatorWD   文件: AlertController.java
private static UiObject2 getAlertButton(String alertType) throws Exception {
    UiDevice mDevice = Elements.getGlobal().getmDevice();
    int buttonIndex;
    if (alertType.equals("accept")) {
        buttonIndex = 1;
    } else if (alertType.equals("dismiss")) {
        buttonIndex = 0;
    } else {
        throw new Exception("alertType can only be 'accept' or 'dismiss'");
    }

    List<UiObject2> alertButtons = mDevice.findObjects(By.clazz("android.widget.Button").clickable(true).checkable(false));
    if (alertButtons.size() == 0) {
        return null;
    }
    UiObject2 alertButton = alertButtons.get(buttonIndex);

    return alertButton;
}
 
源代码12 项目: AppCrawler   文件: UiHelper.java
public static boolean launchApp(String targetPackage) {
    FileLog.i(TAG_MAIN, "{Launch} " + targetPackage);

    UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    String launcherPackage = device.getLauncherPackageName();
    if (launcherPackage.compareToIgnoreCase(targetPackage) == 0) {
        launchHome();
        return true;
    }
    Context context = InstrumentationRegistry.getContext();
    final Intent intent = context.getPackageManager().getLaunchIntentForPackage(targetPackage);
    if (intent != null) {
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); // Make sure each launch is a new task
        context.startActivity(intent);
        device.wait(Until.hasObject(By.pkg(Config.sTargetPackage).depth(0)), Config.sLaunchTimeout);
    } else {
        String err = String.format("(%s) No launchable Activity.\n", targetPackage);
        Log.e(TAG, err);
        Bundle bundle = new Bundle();
        bundle.putString("ERROR", err);
        InstrumentationRegistry.getInstrumentation().finish(1, bundle);
    }
    return true;
}
 
源代码13 项目: espresso-samples   文件: PermissionsTest.java
@Before
public void startMainActivityFromHomeScreen() {
    // Initialize UiDevice instance
    device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

    // Start from the home screen
    device.pressHome();

    // Wait for launcher
    final String launcherPackage = getLauncherPackageName();
    MatcherAssert.assertThat(launcherPackage, IsNull.notNullValue());
    device.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT);

    // Launch the app
    Context context = InstrumentationRegistry.getContext();
    final Intent intent = context.getPackageManager()
                                 .getLaunchIntentForPackage(APP_PACKAGE_NAME);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);    // Clear out any previous instances
    context.startActivity(intent);

    // Wait for the app to appear
    device.wait(Until.hasObject(By.pkg(APP_PACKAGE_NAME).depth(0)), LAUNCH_TIMEOUT);
}
 
/**
 * When using UiAutomator you can interact with ui elements outside of your own process.
 * You can start your application by pressing the home button of the device and launch the
 * target app through the android launcher.
 *
 * <p>
 * If you only want to launch and test a single {@link Activity} you can use {@link
 * Instrumentation} directly to only launch the target {@link Activity}
 * </p>
 */
@Before
public void startBlueprintActivityFromHomeScreen() {
    // Initialize UiDevice instance
    mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

    // Start from the home screen
    mDevice.pressHome();

    // Wait for launcher
    final String launcherPackage = getLauncherPackageName();
    assertThat(launcherPackage, notNullValue());
    mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT);

    // Launch the blueprint app
    Context context = InstrumentationRegistry.getContext();
    final Intent intent = context.getPackageManager().getLaunchIntentForPackage(TARGET_PACKAGE);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);    // Clear out any previous instances
    context.startActivity(intent);

    // Wait for the app to appear
    mDevice.wait(Until.hasObject(By.pkg(TARGET_PACKAGE).depth(0)), LAUNCH_TIMEOUT);
}
 
@Test
public void findViewPerformActionAndCheckAssertion() {
    // Click on Button with content description
    final String btnContentDescription = InstrumentationRegistry.getTargetContext()
            .getString(R.string.content_desc_hello_android_testing);
    mDevice.findObject(By.desc(btnContentDescription)).click();

    // Verify that correct text is displayed
    final String textViewResId = "text_view_rocks";
    UiObject2 androidRocksTextView = mDevice
            .wait(Until.findObject(By.res(TARGET_PACKAGE, textViewResId)),
                    500 /* wait 500ms */);
    assertThat(androidRocksTextView, notNullValue());

    final String androidTestingRocksText = InstrumentationRegistry.getTargetContext()
            .getString(R.string.android_testing_rocks);
    assertThat(androidRocksTextView.getText(), is(equalTo(androidTestingRocksText)));
}
 
源代码16 项目: twittererer   文件: LoginActivityTest.java
@Before
public void startMainActivityFromHomeScreen() {
    device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

    // Start from the home screen
    device.pressHome();

    // Wait for launcher
    final String launcherPackage = getLauncherPackageName();
    assertThat(launcherPackage, notNullValue());
    device.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), TIMEOUT);

    // Launch the app
    Context context = InstrumentationRegistry.getContext();
    final Intent intent = context.getPackageManager().getLaunchIntentForPackage(APP_PACKAGE);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); // Clear out any previous instances
    context.startActivity(intent);

    // Wait for the app to appear
    device.wait(Until.hasObject(By.pkg(APP_PACKAGE).depth(0)), TIMEOUT);
}
 
@Test
public void it_should_be_possible_to_open_add_new_item_window() throws UiObjectNotFoundException {
    mDevice.findObject(
            new UiSelector()
                    .descriptionContains("Add New Stock Button")
    ).click();

    UiObject2 result = mDevice.findObject(
            By.res(TARGET_PACKAGE, "portfolio_addnew_symbol")
    );

    assertThat(result.getText(), is("GOOG"));
}
 
@Test
public void it_should_be_possible_to_open_add_new_item_window() throws UiObjectNotFoundException {
    mDevice.findObject(
            new UiSelector()
                    .descriptionContains("Add New Stock Button")
    ).click();

    UiObject2 result = mDevice.findObject(
            By.res(TARGET_PACKAGE, "portfolio_addnew_symbol")
    );

    assertThat(result.getText(), is("GOOG"));
}
 
@Test
public void it_should_be_possible_to_open_add_new_item_window() throws UiObjectNotFoundException {
    mDevice.findObject(
            new UiSelector()
                    .descriptionContains("Add New Stock Button")
    ).click();

    UiObject2 result = mDevice.findObject(
            By.res(TARGET_PACKAGE, "portfolio_addnew_symbol")
    );

    assertThat(result.getText(), is("GOOG"));
}
 
@Test
public void it_should_be_possible_to_open_add_new_item_window() throws UiObjectNotFoundException {
    mDevice.findObject(
            new UiSelector()
                    .descriptionContains("Add New Stock Button")
    ).click();

    UiObject2 result = mDevice.findObject(
            By.res(TARGET_PACKAGE, "portfolio_addnew_symbol")
    );

    assertThat(result.getText(), is("GOOG"));
}
 
@Test
public void it_should_be_possible_to_open_add_new_item_window() throws UiObjectNotFoundException {
    mDevice.findObject(
            new UiSelector()
                    .descriptionContains("Add New Stock Button")
    ).click();

    UiObject2 result = mDevice.findObject(
            By.res(TARGET_PACKAGE, "portfolio_addnew_symbol")
    );

    assertThat(result.getText(), is("GOOG"));
}
 
@Test
public void it_should_be_possible_to_open_add_new_item_window() throws UiObjectNotFoundException {
    mDevice.findObject(
            new UiSelector()
                    .descriptionContains("Add New Stock Button")
    ).click();

    UiObject2 result = mDevice.findObject(
            By.res(TARGET_PACKAGE, "portfolio_addnew_symbol")
    );

    assertThat(result.getText(), is("GOOG"));
}
 
@Test
public void it_should_be_possible_to_open_add_new_item_window() throws UiObjectNotFoundException {
    mDevice.findObject(
            new UiSelector()
                    .descriptionContains("Add New Stock Button")
    ).click();

    UiObject2 result = mDevice.findObject(
            By.res(TARGET_PACKAGE, "portfolio_addnew_symbol")
    );

    assertThat(result.getText(), is("GOOG"));
}
 
@Test
public void testChangeText_newActivity() {
    // Type text and then press the button.
    mDevice.findObject(By.res(BASIC_SAMPLE_PACKAGE, "editTextUserInput"))
            .setText(STRING_TO_BE_TYPED);
    mDevice.findObject(By.res(BASIC_SAMPLE_PACKAGE, "activityChangeTextBtn"))
            .click();

    // Verify the test is displayed in the Ui
    UiObject2 changedText = mDevice
            .wait(Until.findObject(By.res(BASIC_SAMPLE_PACKAGE, "show_text_view")),
                    500 /* wait 500ms */);
    assertThat(changedText.getText(), is(equalTo(STRING_TO_BE_TYPED)));
}
 
源代码25 项目: UIAutomatorWD   文件: MUiDevice.java
/**
 * Returns the first object to match the {@code selector} criteria.
 */
public UiObject2 findObject(Object selector) throws Exception {
    AccessibilityNodeInfo node;
    uiDevice.waitForIdle();
    node = ((NodeInfoList) selector).getNodeList().size() > 0 ? ((NodeInfoList) selector).getNodeList().get(0) : null;
    selector = By.clazz(node.getClassName().toString());
    if (node == null) {
        return null;
    }
    return doFindObject(selector, node);
}
 
/**
 * Starts the main activity to test from the home screen
 */
@Before
public void startMainActivityFromHomeScreen() {

    l(this, "@Before startMainActivityFromHomeScreen");

    // Initialize UiDevice instance
    mDevice = UiDevice.getInstance(getInstrumentation());

    // Start from the home screen
    mDevice.pressHome();

    // Wait for launcher
    final String launcherPackage = mDevice.getLauncherPackageName();
    assertThat(launcherPackage, notNullValue());
    mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT_MS);

    // Launch the app
    Context context = InstrumentationRegistry.getContext();
    final Intent intent = context.getPackageManager().getLaunchIntentForPackage(PACKAGE_APP_PATH);

    // Clear out any previous instances
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    context.startActivity(intent);

    // Wait for the app to appear
    mDevice.wait(Until.hasObject(By.pkg(PACKAGE_APP_PATH).depth(0)), LAUNCH_TIMEOUT_MS);

}
 
源代码27 项目: SmoothClicker   文件: ItSettingsActivity.java
/**
 * Starts the main activity to test from the home screen
 */
@Before
public void startMainActivityFromHomeScreen() {

    l(this, "@Before startMainActivityFromHomeScreen");

    // Initialize UiDevice instance
    mDevice = UiDevice.getInstance(getInstrumentation());

    // Start from the home screen
    mDevice.pressHome();

    // Wait for launcher
    final String launcherPackage = mDevice.getLauncherPackageName();
    assertThat(launcherPackage, notNullValue());
    mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT_MS);

    // Launch the app
    Context context = InstrumentationRegistry.getContext();
    final Intent intent = context.getPackageManager().getLaunchIntentForPackage(PACKAGE_APP_PATH);

    // Clear out any previous instances
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    context.startActivity(intent);

    // Wait for the app to appear
    mDevice.wait(Until.hasObject(By.pkg(PACKAGE_APP_PATH).depth(0)), LAUNCH_TIMEOUT_MS);

    // Prepare for tests
    openSettingsScreenFromMenu();

}
 
源代码28 项目: SmoothClicker   文件: ItIntroScreensActivity.java
/**
 * Starts the main activity to test from the home screen
 */
@Before
public void startMainActivityFromHomeScreen() {

    l(this, "@Before startMainActivityFromHomeScreen");

    Context context = InstrumentationRegistry.getContext();

    // Initialize UiDevice instance
    mDevice = UiDevice.getInstance(getInstrumentation());

    // Start from the home screen
    mDevice.pressHome();

    // Wait for launcher
    final String launcherPackage = mDevice.getLauncherPackageName();
    assertThat(launcherPackage, notNullValue());
    mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT_MS);

    // Launch the app
    final Intent intent = context.getPackageManager().getLaunchIntentForPackage(PACKAGE_APP_PATH);

    // Clear out any previous instances
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    context.startActivity(intent);

    // Wait for the app to appear
    mDevice.wait(Until.hasObject(By.pkg(PACKAGE_APP_PATH).depth(0)), LAUNCH_TIMEOUT_MS);

}
 
源代码29 项目: SmoothClicker   文件: ItClickerActivity.java
/**
 * Starts the main activity to test from the home screen
 */
@Before
public void startMainActivityFromHomeScreen() {

    l(this, "@Before startMainActivityFromHomeScreen");

    // Initialize UiDevice instance
    mDevice = UiDevice.getInstance(getInstrumentation());

    // Start from the home screen
    mDevice.pressHome();

    // Wait for launcher
    final String launcherPackage = mDevice.getLauncherPackageName();
    assertThat(launcherPackage, notNullValue());
    mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT_MS);

    // Launch the app
    Context context = InstrumentationRegistry.getContext();
    final Intent intent = context.getPackageManager().getLaunchIntentForPackage(PACKAGE_APP_PATH);

    // Clear out any previous instances
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    context.startActivity(intent);

    // Wait for the app to appear
    mDevice.wait(Until.hasObject(By.pkg(PACKAGE_APP_PATH).depth(0)), LAUNCH_TIMEOUT_MS);

}
 
源代码30 项目: SmoothClicker   文件: ItStandaloneModeDialog.java
/**
 * Starts the main activity to test from the home screen
 */
@Before
public void startMainActivityFromHomeScreen() {

    l(this, "@Before startMainActivityFromHomeScreen");

    // Initialize UiDevice instance
    mDevice = UiDevice.getInstance(getInstrumentation());

    // Start from the home screen
    mDevice.pressHome();

    // Wait for launcher
    final String launcherPackage = mDevice.getLauncherPackageName();
    assertThat(launcherPackage, notNullValue());
    mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT_MS);

    // Launch the app
    Context context = InstrumentationRegistry.getTargetContext();
    final Intent intent = context.getPackageManager().getLaunchIntentForPackage(PACKAGE_APP_PATH);

    // Clear out any previous instances
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    context.startActivity(intent);

    // Wait for the app to appear
    mDevice.wait(Until.hasObject(By.pkg(PACKAGE_APP_PATH).depth(0)), LAUNCH_TIMEOUT_MS);

    // Prepare for tests
    openStandaloneModeDialog();

}
 
 类所在包
 类方法
 同包方法