Rahul
Rahul

Reputation: 57

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 get to a particular item for example WiFi / Airplane mode. Could some body help me to select an item in this quick settings?

I tried the following

if (new UiObject(new UiSelector().text("BRIGHTNESS")).exists())
{
    new UiObject(new UiSelector().text("BRIGHTNESS")).click();
}

and

if (new UiObject(new uiSelector().className("android.widget.GridView").description("AEROPLANE MODE")).exists())
{
    new UiObject(new UiSelector().className("android.widget.GridView").description("AEROPLANE MODE")).click();
}

But no success..

Thanks


I am to access the items using following

UiScrollable scroll = new UiScrollable(new UiSelector().scrollable(true));
UiObject airplane = scroll.getChildByText(new UiSelector().className(android.widget.TextView.class.getName()), "Aeroplane mode");

But, could anybody help me how to decide in uiautomator on Aeroplane mode is set or not in this Quick Settings menu?

Upvotes: 1

Views: 3553

Answers (2)

Thomas
Thomas

Reputation: 6168

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

private val testApp = UiDevice.getInstance(getInstrumentation())

private fun enableAirplaneMode() = apply {
  testApp.uiDevice.openQuickSettings()
  testApp.uiDevice.waitForIdle()

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

  airplaneModeIcon.click()
}

Disable Airplane Mode

private val testApp = UiDevice.getInstance(getInstrumentation())

private fun disableAirplaneMode() = apply {
  testApp.uiDevice.openQuickSettings()
  testApp.uiDevice.waitForIdle()

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

  airplaneModeIcon.click()
}

Upvotes: 0

Smriti
Smriti

Reputation: 1592

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 delay
sleep(3000);    //3sec delay

//check for wifi text
if(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.

Upvotes: 1

Related Questions