Uiautomator ------通过控件ID来查找进行测试

时间:2025-01-31 08:37:20

上一篇Uiautomator测试----系统测试是通过Uiautomatorviewer工具来搜索查找,当控件含有搜索字符时 唯一时,容易定位及编写代码如:

new UiSelector().className("").textContains("地图").resourceId("")

但遇到太多的view\RelativeLayout等父控件,子控件没有明显的搜索关键字/ID时,则需一级一级进行编写测试代码。


当应用开源已知每一个组件的id(开发使用findViewById来操作),则可直接针对ID进行搜索查找,同样达到测试的目的


 (("<span style="color: rgb(24, 54, 145); font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; line-height: 16.8px; white-space: pre;"></span>", "<strong>changeTextBt</strong>"))
                .click();

<strong>changeTextBt 为该控件的ID名称</strong>


例:

import ;
import ;
import ;

import ;
import ;
import ;
import ;
import ;
import ;
import .AndroidJUnit4;
import ;
import ;
import .UiObject2;
import ;

import static ;
import static ;
import static ;
import static ;

/**
 * Basic sample for unbundled UiAutomator.
 */
@RunWith()
@SdkSuppress(minSdkVersion = 18)
public class ChangeTextBehaviorTest {

    private static final String BASIC_SAMPLE_PACKAGE
            = "";

    private static final int LAUNCH_TIMEOUT = 5000;

    private static final String STRING_TO_BE_TYPED = "UiAutomator";

    private UiDevice mDevice;

    @Before
    public void startMainActivityFromHomeScreen() {
        // Initialize UiDevice instance
        mDevice = (());

        // Start from the home screen
        ();

        // Wait for launcher
        final String launcherPackage = getLauncherPackageName();
        assertThat(launcherPackage, notNullValue());
        (((launcherPackage).depth(0)), LAUNCH_TIMEOUT);

        // Launch the blueprint app
        Context context = ();
        final Intent intent = ()
                .getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE);
        (Intent.FLAG_ACTIVITY_CLEAR_TASK);    // Clear out any previous instances
        (intent);

        // Wait for the app to appear
        (((BASIC_SAMPLE_PACKAGE).depth(0)), LAUNCH_TIMEOUT);
    }

    @Test
    public void checkPreconditions() {
        assertThat(mDevice, notNullValue());
    }

    @Test
    public void testChangeText_sameActivity() {
        // Type text and then press the button.
        ((BASIC_SAMPLE_PACKAGE, "editTextUserInput"))
                .setText(STRING_TO_BE_TYPED);
        ((BASIC_SAMPLE_PACKAGE, "changeTextBt"))
                .click();

        // Verify the test is displayed in the Ui
        UiObject2 changedText = mDevice
                .wait(((BASIC_SAMPLE_PACKAGE, "textToBeChanged")),
                        500 /* wait 500ms */);
        assertThat((), is(equalTo(STRING_TO_BE_TYPED)));
    }

    @Test
    public void testChangeText_newActivity() {
        // Type text and then press the button.
        ((BASIC_SAMPLE_PACKAGE, "editTextUserInput"))
                .setText(STRING_TO_BE_TYPED);
        ((BASIC_SAMPLE_PACKAGE, "activityChangeTextBtn"))
                .click();

        // Verify the test is displayed in the Ui
        UiObject2 changedText = mDevice
                .wait(((BASIC_SAMPLE_PACKAGE, "show_text_view")),
                        500 /* wait 500ms */);
        assertThat((), is(equalTo(STRING_TO_BE_TYPED)));
    }

    /**
     * Uses package manager to find the package name of the device launcher. Usually this package
     * is "" but can be different at times. This is a generic solution which
     * works on all platforms.`
     */
    private String getLauncherPackageName() {
        // Create launcher Intent
        final Intent intent = new Intent(Intent.ACTION_MAIN);
        (Intent.CATEGORY_HOME);

        // Use PackageManager to get the launcher package name
        PackageManager pm = ().getPackageManager();
        ResolveInfo resolveInfo = (intent, PackageManager.MATCH_DEFAULT_ONLY);
        return ;
    }
}