android.support.test.uiautomator.UiObject2 Java Examples

The following examples show how to use android.support.test.uiautomator.UiObject2. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UiAutomatorTest.java    From android-testing-templates with Apache License 2.0 6 votes vote down vote up
@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)));
}
 
Example #2
Source File: ObjInfo.java    From android-uiautomator-server with MIT License 6 votes vote down vote up
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();
}
 
Example #3
Source File: AlertController.java    From UIAutomatorWD with MIT License 6 votes vote down vote up
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;
}
 
Example #4
Source File: MUiDevice.java    From UIAutomatorWD with MIT License 6 votes vote down vote up
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));
        }
    }
 
Example #5
Source File: MUiDevice.java    From UIAutomatorWD with MIT License 6 votes vote down vote up
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;
}
 
Example #6
Source File: BenchmarkTest.java    From incubator-weex-playground with Apache License 2.0 5 votes vote down vote up
@Repeat(TIMES)
//@Test
@SdkSuppress(minSdkVersion = 23)
public void testScrollFPS() {
  UiObject2 uiObject2 = loadPageForFPS();
  if (uiObject2 != null) {
    uiObject2.scroll(Direction.DOWN, 6, SCROLL_SPEED);
    processGfxInfo(scrollFrameSeconds);
  }
}
 
Example #7
Source File: AutomatorServiceImpl.java    From android-uiautomator-server with MIT License 5 votes vote down vote up
@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;
}
 
Example #8
Source File: AutomatorServiceImpl.java    From android-uiautomator-server with MIT License 5 votes vote down vote up
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;
}
 
Example #9
Source File: Selector.java    From android-uiautomator-server with MIT License 5 votes vote down vote up
public UiObject2 toUiObject2() {
	if (checkBySelectorNull(this)) return null;

	UiObject2 obj2 = device.findObject(toBySelector());
	if(this.getChildOrSibling().length>0){
		return null;
	}
	return obj2;
}
 
Example #10
Source File: Elements.java    From UIAutomatorWD with MIT License 5 votes vote down vote up
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");
	}
}
 
Example #11
Source File: Elements.java    From UIAutomatorWD with MIT License 5 votes vote down vote up
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");
	}
}
 
Example #12
Source File: Elements.java    From UIAutomatorWD with MIT License 5 votes vote down vote up
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;
}
 
Example #13
Source File: Elements.java    From UIAutomatorWD with MIT License 5 votes vote down vote up
public Element addElement(UiObject2 element) {
	counter++;
	final String key = counter.toString();
	Element elem = new Element(key, element);
	getElems().put(key, elem);
	return elem;
}
 
Example #14
Source File: ElementController.java    From UIAutomatorWD with MIT License 5 votes vote down vote up
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);
}
 
Example #15
Source File: MUiDevice.java    From UIAutomatorWD with MIT License 5 votes vote down vote up
/**
 * 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);
}
 
Example #16
Source File: BenchmarkTest.java    From incubator-weex-playground with Apache License 2.0 5 votes vote down vote up
@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);
  }
}
 
Example #17
Source File: MainEndToEndTest.java    From Building-Professional-Android-Applications with MIT License 5 votes vote down vote up
@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"));
}
 
Example #18
Source File: MainEndToEndTest.java    From Building-Professional-Android-Applications with MIT License 5 votes vote down vote up
@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"));
}
 
Example #19
Source File: MainEndToEndTest.java    From Building-Professional-Android-Applications with MIT License 5 votes vote down vote up
@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"));
}
 
Example #20
Source File: MainEndToEndTest.java    From Building-Professional-Android-Applications with MIT License 5 votes vote down vote up
@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"));
}
 
Example #21
Source File: MainEndToEndTest.java    From Building-Professional-Android-Applications with MIT License 5 votes vote down vote up
@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"));
}
 
Example #22
Source File: MainEndToEndTest.java    From Building-Professional-Android-Applications with MIT License 5 votes vote down vote up
@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"));
}
 
Example #23
Source File: MainEndToEndTest.java    From Building-Professional-Android-Applications with MIT License 5 votes vote down vote up
@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"));
}
 
Example #24
Source File: UIAnimatorTest.java    From Expert-Android-Programming with MIT License 5 votes vote down vote up
@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)));
}
 
Example #25
Source File: ElementController.java    From UIAutomatorWD with MIT License 5 votes vote down vote up
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);
}
 
Example #26
Source File: Element.java    From UIAutomatorWD with MIT License 4 votes vote down vote up
Element(String id, UiObject2 element) {
	this.element = element;
	this.id = id;
}
 
Example #27
Source File: Element.java    From UIAutomatorWD with MIT License 4 votes vote down vote up
public UiObject2 getUiObject() {
	return this.element;
}
 
Example #28
Source File: ItSettingsActivity.java    From SmoothClicker with MIT License 4 votes vote down vote up
/**
 * @param index - The idnex of the field in the list (start at 0)
 * @param text - The text of the field to get
 */
private void testFieldWithName( int index, String text ){

    if ( text == null || text.length() <= 0 || index < 0){
        fail("Wrong test");
    }

    final String DUMMY_TEXT = "Hello world";

    try {
        // Display the dialog
        UiObject field = mDevice.findObject(
                new UiSelector()
                        .className("android.widget.TextView")
                        .packageName(PACKAGE_APP_PATH)
                        .resourceId("android:id/title")
                        .text(text)
        );
        field.click();
        // Change the value
        field = mDevice.findObject(
                new UiSelector()
                        .className("android.widget.EditText")
                        .packageName(PACKAGE_APP_PATH)
                        .resourceId("android:id/edit")
        );
        final String BACKUP_TEXT = field.getText();
        field.setText(DUMMY_TEXT);
        // Confirm
        UiObject button = mDevice.findObject(
                new UiSelector()
                        .className("android.widget.Button")
                        .packageName(PACKAGE_APP_PATH)
                        .resourceId("android:id/button1")
        );
        button.click();
        w(1000);
        // Check the summary
        BySelector checkboxSettingsSelector = By.clazz("android.widget.TextView");
        List<UiObject2> summaries = mDevice.findObjects(checkboxSettingsSelector);
        UiObject2 field2 = summaries.get( index * 4 + 2 );
        assertEquals(DUMMY_TEXT, field2.getText());
        // Reset the value
        resetFieldWithText( DUMMY_TEXT, BACKUP_TEXT );
    } catch ( UiObjectNotFoundException uonfe ){
        uonfe.printStackTrace();
        fail(uonfe.getMessage());
    }

}
 
Example #29
Source File: AlertController.java    From UIAutomatorWD with MIT License 4 votes vote down vote up
private static void dismissAlert() throws Exception {
    UiObject2 alertButton = getAlertButton("dismiss");
    if (alertButton != null) {
        alertButton.click();
    }
}
 
Example #30
Source File: AlertController.java    From UIAutomatorWD with MIT License 4 votes vote down vote up
private static void acceptAlert() throws Exception {
    UiObject2 alertButton = getAlertButton("accept");
    if (alertButton != null) {
        alertButton.click();
    }
}