Java Code Examples for androidx.test.uiautomator.UiDevice#getInstance()

The following examples show how to use androidx.test.uiautomator.UiDevice#getInstance() . 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: ExecutionServer.java    From android-uiconductor with Apache License 2.0 6 votes vote down vote up
/**
 * get XML UI hierarchy.
 *
 * <p>Sample: { "width":1440, "height":2621, "xml_count": 4, "value": { "xml0": "...", "xml1":
 * "...", ... } }
 *
 * @return XML UI hierarchy JSON String.
 */
public String getDumpStrHandler(String queryParamStr) throws JSONException {
  List<String> xmls = null;
  // Get UiDevice before dumpWindowHierarchy, otherwise getWindows will return empty for the first
  // request.
  UiDevice mDevice = UiDevice.getInstance(getInstrumentation());
  xmls =
      AccessibilityNodeInfoDumper.dumpWindowHierarchy(
          queryParamStr != null && queryParamStr.toLowerCase().contains("withclassname"));
  JSONObject jsonRootObj = new JSONObject();
  JSONObject valueObj = new JSONObject();
  for (int i = 0; i < xmls.size(); i++) {
    valueObj.put(XML_KEYWORD + i, xmls.get(i));
  }
  Point devicePhysicalSize = AccessibilityNodeInfoDumper.getDevicePhysicalSize();
  jsonRootObj.put(WIDTH_KEYWORD, devicePhysicalSize.x);
  jsonRootObj.put(HEIGHT_KEYWORD, devicePhysicalSize.y);
  jsonRootObj.put(XML_COUNT_KEYWORD, xmls.size());
  jsonRootObj.put(VALUE_KEYWORD, valueObj);
  return jsonRootObj.toString();
}
 
Example 2
Source File: AccessibilityNodeInfoDumper.java    From android-uiconductor with Apache License 2.0 6 votes vote down vote up
public static Point getDevicePhysicalSize() {
  int width;
  int height;
  try {
    UiDevice mDevice = UiDevice.getInstance(getInstrumentation());
    height = mDevice.getDisplayHeight();
    width = mDevice.getDisplayWidth();
    return new Point(width, height);
  } catch (NullPointerException e) {
    Log.e(TAG, "Failed get display size, using getRealMetrics instead. " + e.getMessage());
  }
  WindowManager windowManager = (WindowManager) getInstrumentation().getContext()
      .getSystemService(Service.WINDOW_SERVICE);
  Display display =  windowManager.getDefaultDisplay();
  DisplayMetrics metrics = new DisplayMetrics();
  display.getRealMetrics(metrics);
  height = metrics.heightPixels;
  width = metrics.widthPixels;
  return new Point(width, height);

}
 
Example 3
Source File: ChangeTextBehaviorTest.java    From testing-samples with Apache License 2.0 6 votes vote down vote up
@Before
public void startMainActivityFromHomeScreen() {
    // Initialize UiDevice instance
    mDevice = UiDevice.getInstance(getInstrumentation());

    // Start from the home screen
    mDevice.pressHome();

    // Wait for launcher
    final String launcherPackage = getLauncherPackageName();
    assertThat(launcherPackage, notNullValue());
    mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT);

    // Launch the blueprint app
    Context context = getApplicationContext();
    final Intent intent = context.getPackageManager()
            .getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);    // Clear out any previous instances
    context.startActivity(intent);

    // Wait for the app to appear
    mDevice.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)), LAUNCH_TIMEOUT);
}
 
Example 4
Source File: ExampleInstrumentedTest.java    From za-Farmer with MIT License 5 votes vote down vote up
@Before
public void setUp() throws RemoteException {
    mUIDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());  //获得device对象
    mContext = InstrumentationRegistry.getContext();
    KeyguardManager km = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
    KeyguardManager.KeyguardLock kl = km.newKeyguardLock("farmer");
    //解锁
    kl.disableKeyguard();
    if (!mUIDevice.isScreenOn()) {
        mUIDevice.wakeUp();
    }

}
 
Example 5
Source File: ScreenshotTest.java    From focus-android with Mozilla Public License 2.0 5 votes vote down vote up
@Before
public void setUpScreenshots() {
    Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
    targetContext = instrumentation.getTargetContext();
    device = UiDevice.getInstance(instrumentation);

    // Use this to switch between default strategy and HostScreencap strategy
    Screengrab.setDefaultScreenshotStrategy(new UiAutomatorScreenshotStrategy());
    //Screengrab.setDefaultScreenshotStrategy(new HostScreencapScreenshotStrategy(device));

    device.waitForIdle();
}
 
Example 6
Source File: PositionHelper.java    From appium-uiautomator2-server with Apache License 2.0 4 votes vote down vote up
public static Point getDeviceAbsPos(final Point point) {
    final UiDevice d = UiDevice.getInstance(getInstrumentation());
    final Rect displayRect = new Rect(0, 0, d.getDisplayWidth(), d.getDisplayHeight());
    Logger.debug("Display bounds: " + displayRect.toShortString());
    return getAbsolutePosition(point, displayRect, ZERO_POINT, true);
}
 
Example 7
Source File: BaseStep.java    From za-Farmer with MIT License 4 votes vote down vote up
public static final UiDevice getUiDevice() {
    return UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
}
 
Example 8
Source File: Device.java    From appium-uiautomator2-server with Apache License 2.0 4 votes vote down vote up
public static UiDevice getUiDevice() {
    return UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
}
 
Example 9
Source File: PlaybackControlTest.java    From CastVideos-android with Apache License 2.0 4 votes vote down vote up
@Before
public void initState() {
    mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
}
 
Example 10
Source File: QueueingTest.java    From CastVideos-android with Apache License 2.0 4 votes vote down vote up
@Before
public void initState() {
    mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
}
 
Example 11
Source File: BasicCastUITest.java    From CastVideos-android with Apache License 2.0 4 votes vote down vote up
@Before
public void initState() {
    mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
}
 
Example 12
Source File: TestAddNumber.java    From quickstart-android with Apache License 2.0 4 votes vote down vote up
@Before public void setUp() {
  UiDevice.getInstance(getInstrumentation());
}
 
Example 13
Source File: PermissionActivityTest.java    From RxPermission with Apache License 2.0 4 votes vote down vote up
private static UiObject getButton(final String text) {
  final UiDevice device = UiDevice.getInstance(getInstrumentation());
  return device.findObject(new UiSelector().text(text));
}
 
Example 14
Source File: NativeSignInTest.java    From samples-android with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    mDevice =  UiDevice.getInstance(getInstrumentation());
}
 
Example 15
Source File: BrowserSignInTest.java    From samples-android with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    mDevice =  UiDevice.getInstance(getInstrumentation());
}
 
Example 16
Source File: AndroidTestsUtility.java    From line-sdk-android with Apache License 2.0 4 votes vote down vote up
public static boolean checkElementExists(int resourceId) {
    UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    Context context = ApplicationProvider.getApplicationContext();
    return uiDevice.wait(Until.hasObject(By.res(toResourceName(context, resourceId))), AndroidTestsConfig.TIMEOUT);
}
 
Example 17
Source File: AndroidTestsUtility.java    From line-sdk-android with Apache License 2.0 4 votes vote down vote up
public static boolean checkElementExists(String className) {
    UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    uiDevice.wait(Until.hasObject(By.clazz(className)), AndroidTestsConfig.TIMEOUT);
    UiObject2 element = uiDevice.findObject(By.clazz(className));
    return element.isEnabled() && element.isClickable() && element.isFocusable();
}
 
Example 18
Source File: AndroidTestsUtility.java    From line-sdk-android with Apache License 2.0 4 votes vote down vote up
public static boolean checkTextExists(String text) {
    UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    UiObject result = uiDevice.findObject(new UiSelector().text(text));
    return result.exists();
}
 
Example 19
Source File: ExampleTest.java    From MultiSlider with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
}
 
Example 20
Source File: DeviceAutomator.java    From device-automator with MIT License 2 votes vote down vote up
/**
 * @param matcher {@link UiObjectMatcher} used to specify the ui element to interact with.
 * @return {@link DeviceAutomator} for the supplied {@link UiObjectMatcher}. All further methods
 *         called on this instance of {@link DeviceAutomator} will interact with the supplied
 *         {@link UiObjectMatcher} only.
 */
public static DeviceAutomator onDevice(UiObjectMatcher matcher) {
    return new DeviceAutomator(UiDevice.getInstance(getInstrumentation()), matcher);
}