类android.support.test.uiautomator.UiObject2源码实例Demo

下面列出了怎么用android.support.test.uiautomator.UiObject2的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: UIAutomatorWD   文件: MUiDevice.java
private UiObject2 doFindObject(Object selector, AccessibilityNodeInfo node) throws Exception {

        Class uiObject2 = Class.forName("android.support.test.uiautomator.UiObject2");
        Constructor cons = uiObject2.getDeclaredConstructors()[0];
        cons.setAccessible(true);
        Object[] constructorParams = {uiDevice, selector, node};

        final long timeoutMillis = 1000;
        long end = SystemClock.uptimeMillis() + timeoutMillis;
        while (true) {
            UiObject2 object2 = (UiObject2) cons.newInstance(constructorParams);
            if (object2 != null) {
                return object2;
            }
            long remainingMillis = end - SystemClock.uptimeMillis();
            if (remainingMillis < 0) {
                return null;
            }
            SystemClock.sleep(Math.min(200, remainingMillis));
        }
    }
 
源代码2 项目: UIAutomatorWD   文件: MUiDevice.java
public List<UiObject2> findObjects(Object selector) throws Exception {
    uiDevice.waitForIdle();
    ArrayList<AccessibilityNodeInfo> accessibilityNodeInfos = ((NodeInfoList) selector).getNodeList();
    int size = accessibilityNodeInfos.size();
    List<UiObject2> list = new ArrayList<UiObject2>();
    if (size > 0) {
        for (int i = 0; i < size; i++) {
            AccessibilityNodeInfo node = accessibilityNodeInfos.get(i);
            if (node == null) {
                continue;
            }
            selector = By.clazz(node.getClassName().toString());
            UiObject2 uiObject2 = doFindObject(selector, node);
            list.add(uiObject2);
        }
    } else {
        return null;
    }
    return list;
}
 
源代码3 项目: UIAutomatorWD   文件: AlertController.java
private static UiObject2 getAlertButton(String alertType) throws Exception {
    UiDevice mDevice = Elements.getGlobal().getmDevice();
    int buttonIndex;
    if (alertType.equals("accept")) {
        buttonIndex = 1;
    } else if (alertType.equals("dismiss")) {
        buttonIndex = 0;
    } else {
        throw new Exception("alertType can only be 'accept' or 'dismiss'");
    }

    List<UiObject2> alertButtons = mDevice.findObjects(By.clazz("android.widget.Button").clickable(true).checkable(false));
    if (alertButtons.size() == 0) {
        return null;
    }
    UiObject2 alertButton = alertButtons.get(buttonIndex);

    return alertButton;
}
 
源代码4 项目: android-uiautomator-server   文件: ObjInfo.java
private ObjInfo(UiObject2 obj) {
	this._bounds = Rect.from(obj.getVisibleBounds());
	this._checkable = obj.isCheckable();
	this._checked = obj.isChecked();
	this._childCount = obj.getChildCount();
	this._clickable = obj.isClickable();
	this._contentDescription = obj.getContentDescription();
	this._enabled = obj.isEnabled();
	this._focusable = obj.isFocusable();
	this._focused = obj.isFocused();
	this._longClickable = obj.isLongClickable();
	this._packageName = obj.getApplicationPackage();
	this._scrollable = obj.isScrollable();
	this._selected = obj.isSelected();
	this._text = obj.getText();
	this._visibleBounds = Rect.from(obj.getVisibleBounds());
	this._className = obj.getClassName();
	this._resourceName = obj.getResourceName();
}
 
@Test
public void findViewPerformActionAndCheckAssertion() {
    // Click on Button with content description
    final String btnContentDescription = InstrumentationRegistry.getTargetContext()
            .getString(R.string.content_desc_hello_android_testing);
    mDevice.findObject(By.desc(btnContentDescription)).click();

    // Verify that correct text is displayed
    final String textViewResId = "text_view_rocks";
    UiObject2 androidRocksTextView = mDevice
            .wait(Until.findObject(By.res(TARGET_PACKAGE, textViewResId)),
                    500 /* wait 500ms */);
    assertThat(androidRocksTextView, notNullValue());

    final String androidTestingRocksText = InstrumentationRegistry.getTargetContext()
            .getString(R.string.android_testing_rocks);
    assertThat(androidRocksTextView.getText(), is(equalTo(androidTestingRocksText)));
}
 
源代码6 项目: incubator-weex-playground   文件: BenchmarkTest.java
@Repeat(TIMES)
//@Test
@SdkSuppress(minSdkVersion = 23)
public void testFlingFPS() {
  UiObject2 uiObject2 = loadPageForFPS();
  if (uiObject2 != null) {
    uiObject2.fling(Direction.DOWN, FLING_SPEED);
    uiObject2.fling(Direction.DOWN, FLING_SPEED);
    uiObject2.fling(Direction.DOWN, FLING_SPEED);
    uiObject2.fling(Direction.DOWN, FLING_SPEED);
    processGfxInfo(flingFrameSeconds);
  }
}
 
源代码7 项目: incubator-weex-playground   文件: BenchmarkTest.java
@Repeat(TIMES)
//@Test
@SdkSuppress(minSdkVersion = 23)
public void testScrollFPS() {
  UiObject2 uiObject2 = loadPageForFPS();
  if (uiObject2 != null) {
    uiObject2.scroll(Direction.DOWN, 6, SCROLL_SPEED);
    processGfxInfo(scrollFrameSeconds);
  }
}
 
@Test
public void it_should_be_possible_to_open_add_new_item_window() throws UiObjectNotFoundException {
    mDevice.findObject(
            new UiSelector()
                    .descriptionContains("Add New Stock Button")
    ).click();

    UiObject2 result = mDevice.findObject(
            By.res(TARGET_PACKAGE, "portfolio_addnew_symbol")
    );

    assertThat(result.getText(), is("GOOG"));
}
 
@Test
public void it_should_be_possible_to_open_add_new_item_window() throws UiObjectNotFoundException {
    mDevice.findObject(
            new UiSelector()
                    .descriptionContains("Add New Stock Button")
    ).click();

    UiObject2 result = mDevice.findObject(
            By.res(TARGET_PACKAGE, "portfolio_addnew_symbol")
    );

    assertThat(result.getText(), is("GOOG"));
}
 
@Test
public void it_should_be_possible_to_open_add_new_item_window() throws UiObjectNotFoundException {
    mDevice.findObject(
            new UiSelector()
                    .descriptionContains("Add New Stock Button")
    ).click();

    UiObject2 result = mDevice.findObject(
            By.res(TARGET_PACKAGE, "portfolio_addnew_symbol")
    );

    assertThat(result.getText(), is("GOOG"));
}
 
@Test
public void it_should_be_possible_to_open_add_new_item_window() throws UiObjectNotFoundException {
    mDevice.findObject(
            new UiSelector()
                    .descriptionContains("Add New Stock Button")
    ).click();

    UiObject2 result = mDevice.findObject(
            By.res(TARGET_PACKAGE, "portfolio_addnew_symbol")
    );

    assertThat(result.getText(), is("GOOG"));
}
 
@Test
public void it_should_be_possible_to_open_add_new_item_window() throws UiObjectNotFoundException {
    mDevice.findObject(
            new UiSelector()
                    .descriptionContains("Add New Stock Button")
    ).click();

    UiObject2 result = mDevice.findObject(
            By.res(TARGET_PACKAGE, "portfolio_addnew_symbol")
    );

    assertThat(result.getText(), is("GOOG"));
}
 
@Test
public void it_should_be_possible_to_open_add_new_item_window() throws UiObjectNotFoundException {
    mDevice.findObject(
            new UiSelector()
                    .descriptionContains("Add New Stock Button")
    ).click();

    UiObject2 result = mDevice.findObject(
            By.res(TARGET_PACKAGE, "portfolio_addnew_symbol")
    );

    assertThat(result.getText(), is("GOOG"));
}
 
@Test
public void it_should_be_possible_to_open_add_new_item_window() throws UiObjectNotFoundException {
    mDevice.findObject(
            new UiSelector()
                    .descriptionContains("Add New Stock Button")
    ).click();

    UiObject2 result = mDevice.findObject(
            By.res(TARGET_PACKAGE, "portfolio_addnew_symbol")
    );

    assertThat(result.getText(), is("GOOG"));
}
 
@Test
public void testChangeText_newActivity() {
    // Type text and then press the button.
    mDevice.findObject(By.res(BASIC_SAMPLE_PACKAGE, "editTextUserInput"))
            .setText(STRING_TO_BE_TYPED);
    mDevice.findObject(By.res(BASIC_SAMPLE_PACKAGE, "activityChangeTextBtn"))
            .click();

    // Verify the test is displayed in the Ui
    UiObject2 changedText = mDevice
            .wait(Until.findObject(By.res(BASIC_SAMPLE_PACKAGE, "show_text_view")),
                    500 /* wait 500ms */);
    assertThat(changedText.getText(), is(equalTo(STRING_TO_BE_TYPED)));
}
 
源代码16 项目: UIAutomatorWD   文件: MUiDevice.java
/**
 * Returns the first object to match the {@code selector} criteria.
 */
public UiObject2 findObject(Object selector) throws Exception {
    AccessibilityNodeInfo node;
    uiDevice.waitForIdle();
    node = ((NodeInfoList) selector).getNodeList().size() > 0 ? ((NodeInfoList) selector).getNodeList().get(0) : null;
    selector = By.clazz(node.getClassName().toString());
    if (node == null) {
        return null;
    }
    return doFindObject(selector, node);
}
 
源代码17 项目: UIAutomatorWD   文件: ElementController.java
private static UiObject2 getXPathUiObject(String expression) throws Exception {
    final NodeInfoList nodeList = XPathSelector.getNodesList(expression);
    if (nodeList.size() == 0) {
        throw new Exception(Status.XPathLookupError.getStatusDes());
    }
    return MUiDevice.getInstance().findObject(nodeList);
}
 
源代码18 项目: UIAutomatorWD   文件: ElementController.java
private static List<UiObject2> getXPathUiObjects(String expression) throws Exception {
    final NodeInfoList nodeList = XPathSelector.getNodesList(expression);
    if (nodeList.size() == 0) {
        throw new Exception(Status.XPathLookupError.getStatusDes());
    }
    return MUiDevice.getInstance().findObjects(nodeList);
}
 
源代码19 项目: UIAutomatorWD   文件: Elements.java
public Element addElement(UiObject2 element) {
	counter++;
	final String key = counter.toString();
	Element elem = new Element(key, element);
	getElems().put(key, elem);
	return elem;
}
 
源代码20 项目: UIAutomatorWD   文件: Elements.java
public List<Element> addElements(List<UiObject2> elements) {
	List<Element> elems = new ArrayList<Element>();
	for(int i = 0; i < elements.size(); i++) {
		counter++;
		Element elem = new Element(counter + "", elements.get(i));
		getElems().put(counter + "", elem);
		elems.add(elem);
	}

	return elems;
}
 
源代码21 项目: UIAutomatorWD   文件: Elements.java
public Element getElement(BySelector sel) throws Exception {
	UiObject2 el = mDevice.findObject(sel);
	Element result = addElement(el);
	if (el != null) {
		return result;
	} else {
		throw new Exception("not found");
	}
}
 
源代码22 项目: UIAutomatorWD   文件: Elements.java
public List<Element> getMultiElement(BySelector sel) throws Exception {
	List<UiObject2> el = mDevice.findObjects(sel);
	List<Element> result = addElements(el);
	if (result != null) {
		return result;
	} else {
		throw new Exception("not found");
	}
}
 
源代码23 项目: android-uiautomator-server   文件: Selector.java
public UiObject2 toUiObject2() {
	if (checkBySelectorNull(this)) return null;

	UiObject2 obj2 = device.findObject(toBySelector());
	if(this.getChildOrSibling().length>0){
		return null;
	}
	return obj2;
}
 
private boolean swipe(UiObject2 item, String dir,float percent, int steps) throws UiObjectNotFoundException {
    dir = dir.toLowerCase();
    if ("u".equals(dir) || "up".equals(dir)) item.swipe(Direction.UP,percent,steps);
    else if ("d".equals(dir) || "down".equals(dir)) item.swipe(Direction.DOWN,percent,steps);
    else if ("l".equals(dir) || "left".equals(dir)) item.swipe(Direction.LEFT,percent,steps);
    else if ("r".equals(dir) || "right".equals(dir)) item.swipe(Direction.RIGHT,percent,steps);
    return true;
}
 
@Override
public List<ObjInfo> finds(Selector obj) throws NotImplementedException {
    List<ObjInfo> objs = new ArrayList<>();
    List<UiObject2> obj2s = device.findObjects(obj.toBySelector());
    for(int i=0;i<obj2s.size();i++){
        objs.add(ObjInfo.getObjInfo(obj2s.get(i)));
    }
    return objs;
}
 
源代码26 项目: incubator-weex-playground   文件: BenchmarkTest.java
private UiObject2 loadPageForFPS() {
  BenchmarkActivity benchmarkActivity = loadWeexPage();
  return mUiDevice.wait(Until.findObject(By.desc(BenchmarkActivity.ROOT)), WAIT_TIMEOUT);
}
 
源代码27 项目: UIAutomatorWD   文件: AlertController.java
private static void acceptAlert() throws Exception {
    UiObject2 alertButton = getAlertButton("accept");
    if (alertButton != null) {
        alertButton.click();
    }
}
 
源代码28 项目: UIAutomatorWD   文件: AlertController.java
private static void dismissAlert() throws Exception {
    UiObject2 alertButton = getAlertButton("dismiss");
    if (alertButton != null) {
        alertButton.click();
    }
}
 
源代码29 项目: UIAutomatorWD   文件: Element.java
Element(String id, UiObject2 element) {
	this.element = element;
	this.id = id;
}
 
源代码30 项目: UIAutomatorWD   文件: Element.java
public UiObject2 getUiObject() {
	return this.element;
}
 
 类所在包
 类方法
 同包方法