下面列出了android.support.test.uiautomator.UiDevice#findObject ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void allowPermissionsIfNeeded() {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
sleep(PERMISSIONS_DIALOG_DELAY);
UiDevice device = UiDevice.getInstance(getInstrumentation());
UiObject allowPermissions = device.findObject(new UiSelector()
.clickable(true)
.checkable(false)
.index(GRANT_BUTTON_INDEX));
if (allowPermissions.exists()) {
allowPermissions.click();
}
}
} catch (UiObjectNotFoundException e) {
System.out.println("There is no permissions dialog to interact with");
}
}
@Ignore
@Test
public void testUiAutomatorAPI() throws UiObjectNotFoundException, InterruptedException {
UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
UiSelector editTextSelector = new UiSelector().className("android.widget.EditText").text("this is a test").focusable(true);
UiObject editTextWidget = device.findObject(editTextSelector);
editTextWidget.setText("this is new text");
Thread.sleep(2000);
UiSelector buttonSelector = new UiSelector().className("android.widget.Button").text("CLICK ME").clickable(true);
UiObject buttonWidget = device.findObject(buttonSelector);
buttonWidget.click();
Thread.sleep(2000);
}
public static boolean viewExistsContains(UiDevice device, String... textToMatch) throws UiObjectNotFoundException
{
if (textToMatch == null || textToMatch.length < 1)
{
return false;
}
UiObject view;
boolean contains = false;
for (int i = 0; i < textToMatch.length; i++)
{
view = device.findObject(new UiSelector().textContains(textToMatch[i]));
contains = view.exists();
if (contains) return true;
}
return false;
}
public static void inputRandomTextToEditText() {
UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
UiObject edit = null;
int i = 0;
do {
edit = null;
edit = device.findObject(new UiSelector().className("android.widget.EditText").instance(i++));
if (edit != null && edit.exists()) {
try {
Random rand = new Random();
String text = Config.RANDOM_TEXT[rand.nextInt(Config.RANDOM_TEXT.length - 1)];
edit.setText(text);
} catch (UiObjectNotFoundException e) {
// Don't worry
}
}
} while (edit != null && edit.exists());
}
public static boolean viewExistsContains(UiDevice device, String... textToMatch) throws UiObjectNotFoundException
{
if (textToMatch == null || textToMatch.length < 1)
{
return false;
}
UiObject view;
boolean contains = false;
for (int i = 0; i < textToMatch.length; i++)
{
view = device.findObject(new UiSelector().textContains(textToMatch[i]));
contains = view.exists();
if (contains) return true;
}
return false;
}
public void skipPermission(JSONArray permissionPatterns, int scanningCount) {
UiDevice mDevice = Elements.getGlobal().getmDevice();
// if permission list is empty, avoid execution
if (permissionPatterns.size() == 0) {
return;
}
// regular check for permission scanning
try {
for (int i = 0; i < scanningCount; i++) {
inner:
for (int j = 0; j < permissionPatterns.size(); j++) {
String text = permissionPatterns.getString(j);
UiObject object = mDevice.findObject(new UiSelector().text(text));
if (object.exists()) {
object.click();
break inner;
}
}
Thread.sleep(3000);
}
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println(e.getCause().toString());
}
}
public static void allowPermissionsIfNeeded(UiDevice device) {
if (Build.VERSION.SDK_INT >= 23) {
UiObject allowPermissions = device.findObject(new UiSelector().text(TEXT_ALLOW));
if (allowPermissions.exists()) {
try {
allowPermissions.click();
} catch (UiObjectNotFoundException ignored) {
}
}
}
}
public static void allowPermissionsIfNeeded(Activity activity, String permissionNeeded) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !hasNeededPermission(activity, permissionNeeded)) {
sleep(PERMISSIONS_DIALOG_DELAY);
UiDevice device = UiDevice.getInstance(getInstrumentation());
UiObject allowPermissions = device.findObject(new UiSelector().clickable(true).index(GRANT_BUTTON_INDEX));
if (allowPermissions.exists()) {
allowPermissions.click();
}
}
} catch (UiObjectNotFoundException e) {
System.out.println("There is no permissions dialog to interact with");
}
}
protected void click(String target) {
UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
UiObject button = device.findObject(new UiSelector().text(target));
try {
button.click();
} catch (UiObjectNotFoundException e) {
throw new IllegalStateException(e);
}
}
/**
* Clicks a button with the given text. This will try to find the widget exactly as the text is given, and
* will try upper casing the text if it is not found.
*/
public static void clickButton(UiDevice device, String buttonText) throws UiObjectNotFoundException
{
UiObject accept = device.findObject(new UiSelector().text(buttonText));
if (accept.exists())
{
accept.click();
return;
}
accept = device.findObject(new UiSelector().text(buttonText.toUpperCase()));
accept.click();
}
/**
* Clicks a button with the given text. This will try to find the widget exactly as the text is given, and
* will try upper casing the text if it is not found.
*/
public static void clickButton(UiDevice device, String buttonText) throws UiObjectNotFoundException
{
UiObject accept = device.findObject(new UiSelector().text(buttonText));
if (accept.exists())
{
accept.click();
return;
}
accept = device.findObject(new UiSelector().text(buttonText.toUpperCase()));
accept.click();
}
public static boolean viewExistsExact(UiDevice device, String messageText) throws UiObjectNotFoundException
{
UiObject view = device.findObject(new UiSelector().text(messageText));
return view.exists();
}
protected boolean dialogIsShownWith(String expectedMessage) {
UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
UiObject dialog = device.findObject(new UiSelector().textMatches(expectedMessage));
return dialog.exists();
}
public static boolean turnOnPermissionDialogShowing(UiDevice device)
{
UiObject allowButton = device.findObject(new UiSelector().text(TEXT_ALLOW));
return allowButton.exists();
}
public void test004Rearrange() throws InterruptedException, UiObjectNotFoundException {
UiDevice mDevice = UiDevice.getInstance(getInstrumentation());
UiObject start = mDevice.findObject(new UiSelector().textContains(codes[0][0]));
UiObject end = mDevice.findObject(new UiSelector().textContains(codes[1][0]));
start.dragTo(start, 10);
start.dragTo(end, 10);
mDevice.pressBack();
Thread.sleep(2000);
String t = codes[0][0];
codes[0][0] = codes[1][0];
codes[1][0] = t;
for(int i = 0; i < codes.length; i++){
onData(anything()).inAdapterView(withId(R.id.listView))
.atPosition(i)
.onChildView(withId(R.id.textViewLabel))
.check(matches(withText(codes[i][0])));
}
start = mDevice.findObject(new UiSelector().textContains(codes[0][0]));
end = mDevice.findObject(new UiSelector().textContains(codes[1][0]));
start.dragTo(start, 10);
start.dragTo(end, 10);
mDevice.pressBack();
Thread.sleep(2000);
t = codes[0][0];
codes[0][0] = codes[1][0];
codes[1][0] = t;
for(int i = 0; i < codes.length; i++){
onData(anything()).inAdapterView(withId(R.id.listView))
.atPosition(i)
.onChildView(withId(R.id.textViewLabel))
.check(matches(withText(codes[i][0])));
}
}
public static void denyPermission(UiDevice device) throws UiObjectNotFoundException
{
UiObject deny = device.findObject(new UiSelector().text(TEXT_DENY));
deny.click();
}
public static boolean turnOnPermissionDialogShowing(UiDevice device)
{
UiObject allowButton = device.findObject(new UiSelector().text(TEXT_ALLOW));
return allowButton.exists();
}
public static void allowPermission(UiDevice device) throws UiObjectNotFoundException
{
UiObject allow = device.findObject(new UiSelector().text(TEXT_ALLOW));
allow.click();
}
public static boolean viewExistsExact(UiDevice device, String messageText) throws UiObjectNotFoundException
{
UiObject view = device.findObject(new UiSelector().text(messageText));
return view.exists();
}
public static boolean viewExistsContains(UiDevice device, String textToMatch) throws UiObjectNotFoundException
{
UiObject view = device.findObject(new UiSelector().textContains(textToMatch));
return view.exists();
}