如何在 android 中使用 UI-Automator 获取屏幕中出现的编辑文本数
How to get the number of edit-texts that are present in the screen using UI-Automator in android
我想做什么:
- 如图所示,线性布局中有2个编辑文本,如何获取
屏幕中的编辑文本数。
- 关于 UI-automater
的任何示例文档
ExampleInstrumentedTest.java
package com.testing.espressodemo;
import android.support.test.filters.SdkSuppress;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.Until;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.InstrumentationRegistry.getInstrumentation;
@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class ExampleInstrumentedTest {
private UiDevice mDevice;
@Before
public void startMainActivityFromHomeScreen() {
// Initialize UiDevice instance
mDevice = UiDevice.getInstance(getInstrumentation());
// Start from the home screen
mDevice.pressHome();
// Wait for the Apps icon to show up on the screen
mDevice.wait(Until.hasObject(By.desc("Apps")), 3000);
// Once you have a reference to the Apps icon, invoke the click method to simulate a click.
UiObject2 appsButton = mDevice.findObject(By.desc("Apps"));
appsButton.click();
// Wait for the Calculator icon to show up on the screen
mDevice.wait(Until.hasObject(By.text("EspressoDemo")), 3000);
//Use the findObject and click methods to obtain a reference to the Calculator icon and simulator a click.
UiObject2 calculatorApp = mDevice.findObject(By.text("EspressoDemo"));
calculatorApp.click();
}
@Test
public void useAppContext() throws Exception {
// Finding the number of EditText Views
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.testing.espressodemo.MainActivity">
<Button
android:id="@+id/buttonId"
android:text="click"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/nameId"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/passwordId"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
试试这个:
向包含 EditText 字段的 LinearLayout 添加一个 id
LinearLayout android:id="@+id/edit_text_container"
在您的 UiAutomator 测试中,对线性布局执行 getChildCount() 并保存值
UiObject editTextContainer = mDevice.findObject(new UiSelector()
.resourceId("com.testing.espressodemo:id/edit_text_container);
int editTextCount = editTextContainer.getChildCount();
我想做什么:
- 如图所示,线性布局中有2个编辑文本,如何获取 屏幕中的编辑文本数。
- 关于 UI-automater 的任何示例文档
ExampleInstrumentedTest.java
package com.testing.espressodemo;
import android.support.test.filters.SdkSuppress;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.Until;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.InstrumentationRegistry.getInstrumentation;
@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class ExampleInstrumentedTest {
private UiDevice mDevice;
@Before
public void startMainActivityFromHomeScreen() {
// Initialize UiDevice instance
mDevice = UiDevice.getInstance(getInstrumentation());
// Start from the home screen
mDevice.pressHome();
// Wait for the Apps icon to show up on the screen
mDevice.wait(Until.hasObject(By.desc("Apps")), 3000);
// Once you have a reference to the Apps icon, invoke the click method to simulate a click.
UiObject2 appsButton = mDevice.findObject(By.desc("Apps"));
appsButton.click();
// Wait for the Calculator icon to show up on the screen
mDevice.wait(Until.hasObject(By.text("EspressoDemo")), 3000);
//Use the findObject and click methods to obtain a reference to the Calculator icon and simulator a click.
UiObject2 calculatorApp = mDevice.findObject(By.text("EspressoDemo"));
calculatorApp.click();
}
@Test
public void useAppContext() throws Exception {
// Finding the number of EditText Views
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.testing.espressodemo.MainActivity">
<Button
android:id="@+id/buttonId"
android:text="click"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/nameId"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/passwordId"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
试试这个:
向包含 EditText 字段的 LinearLayout 添加一个 id
LinearLayout android:id="@+id/edit_text_container"
在您的 UiAutomator 测试中,对线性布局执行 getChildCount() 并保存值
UiObject editTextContainer = mDevice.findObject(new UiSelector() .resourceId("com.testing.espressodemo:id/edit_text_container); int editTextCount = editTextContainer.getChildCount();