I'm trying to close the UIFontPickerViewController
in a UI test by tapping the close button in the navigation bar. In a default iOS app, I embed the default storyboard view controller in a navigation view controller, then in code I open the font picker like this:
class ViewController: UIViewController, UIFontPickerViewControllerDelegate {
override func viewDidAppear(_ animated: Bool) {
let picker = UIFontPickerViewController(configuration: .init())
picker.delegate = self
self.present(picker, animated: true)
}
func fontPickerViewControllerDidPickFont(_ viewController: UIFontPickerViewController) {
}
}
And the UI test looks like this:
final class problemUITests: XCTestCase {
@MainActor
func testExample() throws {
let app = XCUIApplication()
app.launch()
sleep(2)
let button = app.navigationBars.element(boundBy: 1).buttons.element(boundBy: 0)
print(button.debugDescription)
XCTAssert(button.waitForExistence(timeout: 2))
button.tap()
}
}
When running the UI test, the XCTAssert
always fails and the button.tap()
also fails with an error message
Failed to tap "chiudi" Button: No matches found for Element at index 1 from input {(
NavigationBar
)}
"chiudi" means "close" in Italian, my macOS system language. It sounds to me like I correctly get the close button, but the messages Xcode prints make no sense to me.
It's particularly confusing given that the output of the print
statement shows that the button is there, but somehow fails to be tapped:
Attributes: Button, 0x104f44660, {{701.0, 320.0}, {43.0, 44.0}}, label: 'chiudi'
Element subtree:
→Button, 0x104f44660, {{701.0, 320.0}, {43.0, 44.0}}, label: 'chiudi'
Image, 0x104f44780, {{709.0, 327.5}, {30.0, 30.0}}, identifier: 'UICloseButtonBackground'
Path to element:
→Application, 0x104f35940, pid: 29803, label: 'problem'
↳Window (Main), 0x104f376a0, {{0.0, 0.0}, {1032.0, 1376.0}}
↳Other, 0x104f42e10, {{0.0, 0.0}, {1032.0, 1376.0}}
↳Other, 0x104f43100, {{0.0, 0.0}, {1032.0, 1376.0}}
↳Other, 0x104f43220, {{0.0, 0.0}, {1032.0, 1376.0}}
↳Other, 0x104f43340, {{0.0, 0.0}, {1032.0, 1376.0}}
↳Other, 0x104f43460, {{0.0, 0.0}, {1032.0, 1376.0}}
↳Other, 0x104f43580, {{0.0, 0.0}, {1032.0, 1376.0}}
↳Other, 0x104f436a0, {{0.0, 0.0}, {1032.0, 1376.0}}
↳Other, 0x104f437c0, {{0.0, 0.0}, {1032.0, 1376.0}}
↳Other, 0x104f438e0, {{0.0, 0.0}, {1032.0, 1376.0}}
↳Other, 0x104f43a00, {{0.0, 0.0}, {1032.0, 1376.0}}
↳Other, 0x104f43b20, {{0.0, 0.0}, {1032.0, 1376.0}}
↳Other, 0x104f43c40, {{276.0, 314.0}, {480.0, 748.0}}
↳Other, 0x104f43e80, {{276.0, 314.0}, {480.0, 748.0}}
↳Other, 0x104f43fa0, {{276.0, 314.0}, {480.0, 748.0}}
↳Other, 0x104f440c0, {{276.0, 314.0}, {480.0, 748.0}}
↳Other, 0x104f441e0, {{276.0, 314.0}, {480.0, 748.0}}
↳Other, 0x104f44300, {{276.0, 314.0}, {480.0, 748.0}}
↳NavigationBar, 0x104f44420, {{276.0, 314.0}, {480.0, 108.0}}, identifier: 'Scegli font'
↳Button, 0x104f44660, {{701.0, 320.0}, {43.0, 44.0}}, label: 'chiudi'
Query chain:
→Find: Target Application 'org.desairem.problem'
Output: {
Application, 0x104f781b0, pid: 29803, label: 'problem'
}
↪︎Find: Descendants matching type NavigationBar
Output: {
NavigationBar, 0x10607c0d0, {{0.0, 24.0}, {1032.0, 50.0}}, identifier: 'UIFontPickerView'
NavigationBar, 0x10607dab0, {{276.0, 314.0}, {480.0, 108.0}}, identifier: 'Scegli font'
}
↪︎Find: Element at index 1
Output: {
NavigationBar, 0x1064693a0, {{276.0, 314.0}, {480.0, 108.0}}, identifier: 'Scegli font'
}
↪︎Find: Descendants matching type Button
Output: {
Button, 0x104f714a0, {{701.0, 320.0}, {43.0, 44.0}}, label: 'chiudi'
Button, 0x104f71800, {{711.0, 378.0}, {17.0, 22.0}}, identifier: 'Dictate', label: 'Avvia dettatura'
}
↪︎Find: Element at index 0
Output: {
Button, 0x104f5d5e0, {{701.0, 320.0}, {43.0, 44.0}}, label: 'chiudi'
}