首页 > 解决方案 > 如何使用 UIAutomator 关闭快速设置系统面板?

问题描述

在我的 android 测试中,我使用以下代码打开了快速设置面板:

device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
device.openQuickSettings()

我现在如何关闭或滑回快速设置面板?

标签: javaandroidkotlinandroid-uiautomator

解决方案


即使没有显式关闭它的方法,您也可以使用UiDevice对象调用pressBack()方法,API >= 16。这Simulates a short press on the BACK button(设备上的硬件后退按钮)关闭快速设置。对于某些设备,pressBack()需要两次调用。例子:

//Initialize UiDevice object
var uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())

//Open Quick Settings
uiDevice.openQuickSettings()

//Close the quick settings
uiDevice.pressBack()

/*
* If you will be using this test on multiple devices 
* and don't know if you need to pressBack() twice then,
* You can check for an object with the Description of "Airplane mode"
* Which would mean quickSettings still partly open, so press back button again
*
* var pressAgain = false
* for (uiObj in uiDevice.findObjects(By.descContains("Airplane mode")))
*    pressAgain = true          
*/

//Devices require 2 calls to pressBack()
//if(pressAgain)
//   uiDevice.pressBack()

推荐阅读