????Android UIAutomator??
???????
????Uiautomator??????????????UI??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????д?????????
???????UI Automator???????????instrumentation??API????????Android JunitRunner ??????UI Automator Test???????Android 4.3(API level 18)????汾??
???????
????????UI Automator????????????App????????????Gradle Testing???????gradle????????????????ɡ?
????dependencies {
????androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
????}
????UI??????
??????Android?????У???????findViewById??????????????????UI Automator?????????????????????????????????????????????UI Automator???????????????????????UI Automator?????????text??hint??contentDescription????????в?????????UI Automator??????????????????????????UI????????????????UI Automator???????????Щ???????
???????????uiautomatorviewer????????????????ι?????????????????????UI Automator?????????Щ???????????????????????
??????????裺
????* ????豸???App
????* ???豸???????????????
????* ??Android sdk???? <android-sdk>/tools/ ??
????* ??? uiautomatorviewer
????????????? uiautomatorviewer
????* ???Device ScreenShot????????????????????????????????????????????????????????
???????UI?????????????????UI Automator?????????text??hint??contentDescription????????в????????????????????????е???????????????????????????????????Щ????????????? AccessibilityNodeProvider ?????????????????á?
????????UI???
????UI Automator ?? UiDevice ??????????Щ?????????????豸???Щ?????????????????????ж??????????豸???????????????????????????? UiDevice ???????????Home????
import org.junit.Before;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.Until;
...
@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class ChangeTextBehaviorTest {
private static final String BASIC_SAMPLE_PACKAGE
= "com.example.android.testing.uiautomator.BasicSample";
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 = UiDevice.getInstance(InstrumentationRegistry.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);
// 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);
}
}
????????????? findObject() ????????????????UI????????????????????豸???????????????UI?????????????Щ?ж?????
UiObject cancelButton = mDevice.findObject(new UiSelector()
.text("Cancel"))
.className("android.widget.Button"));
UiObject okButton = mDevice.findObject(new UiSelector()
.text("OK"))
.className("android.widget.Button"));
// Simulate a user-click on the OK button?? if found.
if(okButton.exists() && okButton.isEnabled()) {
okButton.click();
}
???????????????
????????????????????????UI???????????? UiSelector ??????????Щ?е??????????????????UI?????
???????ж??????????UI?????????????????????????á????????????UiSelector????????????????????????????????????????????? UiAutomatorObjectNotFoundException ????
???????????? childSelector() ???????UiSelector?????????????ListView??????????к???????????????????????????????????????
????UiObject appItem = new UiObject(new UiSelector()
????.className("android.widget.ListView")
????.instance(1)
????.childSelector(new UiSelector()
????.text("Apps")));
??????????????????????????????ResourceId ???????????????????????????????????????????????????????????????仯???????????????????????汾????????
??????ж???
??????????????????????????????????????????????????????в??????
????click() ???
????dragTo() ?????????
????setText() ???????
????swipeUp() ?????????????????????????????swipeDown??swipeLeft??swipeRight??
????UI Automator??????????Context??????????????????Intent????????????Activity??
public void setUp() {
// Launch a simple calculator app
Context context = getInstrumentation().getContext();
Intent intent = context.getPackageManager()
.getLaunchIntentForPackage(CALC_PACKAGE);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
// Clear out any previous instances
context.startActivity(intent);
mDevice.wait(Until.hasObject(By.pkg(CALC_PACKAGE).depth(0))?? TIMEOUT);
}
???????????
??????????? UiCollection ?????????????UI??????в????????????????????и?Listview??????????? UiCollection ??????????????UiSelector???????UI?????????????????????????????
????????????????????????????н??в?????
UiCollection videos = new UiCollection(new UiSelector()
.className("android.widget.FrameLayout"));
// Retrieve the number of videos in this collection:
int count = videos.getChildCount(new UiSelector()
.className("android.widget.LinearLayout"));
// Find a specific video and simulate a user-click on it
UiObject video = videos.getChildByText(new UiSelector()
.className("android.widget.LinearLayout")?? "Cute Baby Laughing");
video.click();
// Simulate selecting a checkbox that is associated with the video
UiObject checkBox = video.getChild(new UiSelector()
.className("android.widget.Checkbox"));
if(!checkBox.isSelected()) checkbox.click();
?????????????????UI???
??????????? UiScrollable ??????????????????????????????????????????????????????????????????????????????????????棬??????????????????
????UiScrollable settingsItem = new UiScrollable(new UiSelector()
????.className("android.widget.ListView"));
????UiObject about = settingsItem.getChildByText(new UiSelector()
????.className("android.widget.LinearLayout")?? "About tablet");
????about.click();