【问题标题】:Typing text with UiAutomator Android使用 UiAutomator Android 输入文本
【发布时间】:2016-01-07 11:59:42
【问题描述】:

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

使用的设置对应于默认的谷歌“添加帐户”设置。

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

这是我的代码

        // 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" 并注入了字符串,但随后“下一步”按钮未启用。

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

我在这里做错了什么?

编辑:

我已对其余代码进行了注释并更改了按钮声明,但正如预期的那样,“下一步”按钮未启用,因为没有输入任何文本。

// 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 字段。我发现我写的文本没有填充 text 属性,而是填充到了 content-description 属性。

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

【问题讨论】:

  • 您可以通过adb shell input text your_text 输入您的文字吗?

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


【解决方案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 的作用就像魅力一样。两者均由 Google 开发:

查看此网站以了解更多信息:https://google.github.io/android-testing-support-library/

使用 Espresso 检查 EditTextUpdating an EditText with Espresso

希望对你有帮助

【讨论】:

  • 在使用 uiautomator 实施此测试之前,我已经尝试过了,但我无法让 espresso 检测到 webview 中的元素。我相信这个问题与设置的文本有关,因为当我手动输入文本时填充的字段是内容描述。
  • 要在 Espresso 中测试 WebView,您需要使用 espresso-web - 是的,espresso 有一个用于此视图的特定库。还要检查上面的新链接
【解决方案2】:

我可以使用 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-23
    • 2014-08-31
    • 1970-01-01
    • 2011-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多