使用UiAutomator Android

时间:2016-01-07 11:59:42

标签: android coded-ui-tests uiautomator ui-testing android-uiautomator

我试图测试我的应用google signup / login featureparticularly用户尝试添加未在对话框中列出的新帐户。

使用的设置对应于默认的Google"添加帐户"设置。

我遇到的问题是,当应用程序要求时,我似乎无法输入电子邮件,然后按下“下一步”按钮。按钮

这是我的代码

        // Find add account button using uiautomator
        UiObject addButton = mDevice.findObject(new UiSelector().resourceId("android:id/button2"));
        assertEquals(true, addButton.exists());
        // Click the button

        // Fill email text box
        UiObject emailBox = mDevice.findObject(new UiSelector().resourceId("identifierId"));
        emailBox.waitForExists(TIMEOUT);
        assertEquals(true, emailBox.exists());
        emailBox.legacySetText(googleEmail);

        // Find 'NEXT' Button
        UiObject nextButton = mDevice.findObject(new UiSelector().resourceId("identifierNext"));
        assertEquals(true, nextButton.exists());
        nextButton.click();

        // Fill password
        UiObject passwordBox = mDevice.findObject(new UiSelector().resourceId("password"));
        passwordBox.waitForExists(TIMEOUT);
        assertEquals(true, passwordBox.exists());
        emailBox.clearTextField();
        emailBox.setText(googlePassword);

        // Find 'NEXT' Button
        nextButton = mDevice.findObject(new UiSelector().resourceId("next"));
        assertEquals(true, nextButton.exists());
        nextButton.click();

        // Find 'ACCEPT' Button
        nextButton = mDevice.findObject(new UiSelector().resourceId("next"));
        nextButton.waitForExists(TIMEOUT);
        assertEquals(true, nextButton.exists());
        nextButton.click();

应用程序进入屏幕应该输入的电子邮件,并且"assertEquals(true, emailBox.exists());"上的电子邮件不会失败,所以我相信该应用正在检测到它。

我还尝试使用"setText"代替"legacySetText",然后注入字符串,然后注意" next"按钮未启用。

此外,有时会弹出键盘并输入文字,但这似乎是随机的,我无法随意复制。

我在这里做错了什么?

编辑:

我已经对剩下的代码进行了评论并更改了按钮断言,正如预期的那样," next"按钮没有启用,因为没有输入任何文字。

// Find add account button using uiautomator
        UiObject addButton = mDevice.findObject(new UiSelector().resourceId("android:id/button2"));
        assertEquals(true, addButton.exists());
        // Click the button
        addButton.click();

        // Fill email text box
        UiObject emailBox = mDevice.findObject(new UiSelector().resourceId("identifierId").descriptionContains("Enter your email "));
        emailBox.waitForExists(TIMEOUT);
        assertEquals(true, emailBox.exists());
        emailBox.legacySetText(googleEmail);

        // Find 'NEXT' Button
        UiObject nextButton = mDevice.findObject(new UiSelector().resourceId("identifierNext"));
        assertEquals(true, nextButton.isEnabled());
        nextButton.click();

错误说它预期为真,但结果在" assertEquals(true,nextButton.isEnabled());"线

编辑2: 我发现问题与谷歌登录页面解释文字的方式有关。

使用uiautomatorviewer,我抓住了视图的屏幕截图,并检查了我手动编写了一些文本的EditText字段。我发现我写的文字没有填充文字属性,而是转到了内容描述属性。

我现在的问题是我只找到了一种用框架中的setText方法编辑文本的方法。我尝试点击视图,然后设置文字,但没有成功。有没有办法用uiautomator改变内容描述?

2 个答案:

答案 0 :(得分:1)

而不是:

emailBox.legacySetText(googleEmail);

尝试编写如下代码:

// click the admin button
new UiObject(new UiSelector().text("admin")).click();
// set pwd text
new UiObject(new UiSelector().description("pwdEditText")).setText("admin");
// click login button
new UiObject(new UiSelector().description("loginButton")).click();

我认为您可能会发现在Espresso中编写测试非常有用。我认为它更适合这个目的。 uiatomator最好是获得许可或通知或屏幕锁定,它与Espresso的魅力相似。两者都是由谷歌开发的:

检查此网站以了解有关他们的更多信息:https://google.github.io/android-testing-support-library/

使用Espresso检查EditTextUpdating an EditText with Espresso

希望有所帮助

答案 1 :(得分:0)

我可以使用Gmail登录,请删除设备中已配置的任何gmail帐户。 100%工作,每天成功运行。

 final UiDevice mDevice=             
 UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
 final int timeOut = 1000 * 60; 
 mDevice.wait(Until.findObject(By.clazz(WebView.class)), timeOut);

    // Set Login
    UiObject emailInput = mDevice.findObject(new UiSelector()
            .instance(0)
            .className(EditText.class));

    emailInput.waitForExists(timeOut);
    emailInput.setText("your - Email");// please set your email-id here
   // Confirm Button Click
    UiObject Next = mDevice.findObject(new UiSelector()
            .resourceId("identifierNext"));
    Next.waitForExists(timeOut);
    Next.click();

    // Set Password
    UiObject passwordInput = mDevice.findObject(new UiSelector()
            .resourceId("password"));

    passwordInput.waitForExists(timeOut);
    passwordInput.setText("password");// type your password here

    // Confirm Button Click
    Next = mDevice.findObject(new UiSelector()
            .resourceId("passwordNext"));
    Next.waitForExists(timeOut);
    Next.click();

    UiObject nextButton = mDevice.findObject(new UiSelector()
            .resourceId("next"));
    nextButton.waitForExists(timeOut);
    nextButton.click();
    UiObject layout = mDevice.findObject(new UiSelector()
    .resourceId("suw_layout_content"));
    layout.waitForExists(timeOut);
    ElapsedTimeIdlingResource.WaitForScreenContentLoad(5);
    UiObject buttonNext = mDevice.findObject(new UiSelector()
            .className(Button.class));
    buttonNext.waitForExists(timeOut);
    assertEquals(false, nextButton.exists());
    buttonNext.click();
    ElapsedTimeIdlingResource.WaitForScreenContentLoad(50);