简体   繁体   中英

How can I click buttons in Control Center?

I'm a newbie when it comes to Swift and UITesting. I have a test case that must turn off the internet connection of the device but since I can't do that, I tried to access the control center by swiping the bottom most element available in the app

let app = XCUIApplication()
app.buttons["Device"].swipeUp()
app.buttons["Device"].swipeUp()

Now that shows the control center, but when I try to click the airplane mode or the Wi-Fi button, it fails.

app.otherElements["Airplane Mode"].tap()
app.otherElements["Wi-Fi"]

See Screenshot Here

Please help me how to do that or if not, a workaround to turn on or off the internet connection of my device, thanks!

There is no reliable way to do this using UI Testing. In UI Testing you can see the view hierarchy of only App under test. Control Center is outside your App, so you do not have access to its view hierarchy.

If you would like to really do this, one way is it hard code the coordinates and then perform tap actions. Since UI Testing does not have a mechanism to specify coordinated directly, best way to do this is using coordinateWithNormalizedOffset(normalizedOffset: CGVector).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM