Skip to content Skip to sidebar Skip to footer

Quick Setting Through Uiautomator

I am trying to access one of the item in 'Quick Settings' through uiautomator. I am able to open the quick setting through device.openQuickSettings(); After this, I am unable to g

Solution 1:

Did you get these texts - BRIGHTNESS , AEROPLANE MODE from uiautomatorviewer?

Check screenshot here of uiautomatorviewer from my Nexus 4 device -

enter image description here

Here it clearly shows 'Brightness' not 'BRIGHTNESS' so it should be used like :

new UiObject(new UiSelector().text("Brightness")).click();

Same goes for 'airplane mode' as well -

new UiObject(new UiSelector().text("Airplane mode")).click();

For check - you can do one thing -

Notice that when 'Airplane Mode' is ON -> wifi shows 'WI-FI OFF' and when 'Airplane Mode' is OFF -> wifi shows just 'WI-FI'

So you can do this -

//put airplane mode ON
new UiObject(new UiSelector().text("Airplane mode")).click();

//add some delaysleep(3000);    //3sec delay//check for wifi textif(new UiObject(new UiSelector().text("Wi-Fi Off")).exists()) {
    System.out.println("Airplane Mode ON");
} else {
    System.out.println("Airplane Mode OFF");
}

Same you can check when airplane mode is put OFF.

Solution 2:

Here's how I did it in Kotlin on my Galaxy S8 running Android 7.0.

  • Used uiautomatorviewer to find the Airplane Mode element's properties in Quick Settings
  • Noticed the content-desc value of the element of Airplane,mode,Off.,Button

uiautomatorviewer screenshot

Enable Airplane Mode

privateval testApp = UiDevice.getInstance(getInstrumentation())

privatefunenableAirplaneMode() = apply {
  testApp.uiDevice.openQuickSettings()
  testApp.uiDevice.waitForIdle()

  var airplaneModeIcon = checkNotNull(testApp.uiDevice.findObject(By.desc("Airplane,mode,Off.,Button")))

  airplaneModeIcon.click()
}

Disable Airplane Mode

privateval testApp = UiDevice.getInstance(getInstrumentation())

privatefundisableAirplaneMode() = apply {
  testApp.uiDevice.openQuickSettings()
  testApp.uiDevice.waitForIdle()

  var airplaneModeIcon = checkNotNull(testApp.uiDevice.findObject(By.desc("Airplane,mode,On.,Button")))

  airplaneModeIcon.click()
}

Post a Comment for "Quick Setting Through Uiautomator"