I have a web view in my app that I use for account creation (I didi not write and cannot change the associated web pages).
Normally, it is easy to click on any button/link but here the string of my "Create Account" button has an font awesome in its title with makes the recorder crash and the button cannot be programmatically called.
Here is what it prints in app.webViews.allElementsBoundByIndex:
Element subtree:
→WebView, 0x1c03823c0, traits: 8589934592, {{0.0, 0.0}, {736.0, 414.0}}
...
Link, 0x1c0384100, traits: 8590065666, {{242.0, 357.0}, {252.0, 44.0}}, label: 'create account '
As you can see, the font awesome turns into which I cannot detect with something like:
webViews.staticTexts["create account "].tap()
Questions
You can use an NSPredicate to find static texts containing a partial word/phrase, using the containing(_:) method on XCUIElementQuery.
let predicate = NSPredicate(format: "label CONTAINS 'create account'")
let app = XCUIApplication()
let createAccountText = app.webViews.links.containing(predicate)
createAccountText.tap()
There is no shortcut for using starts(with: Sequence) on subscripts. Therefore, you should go through every text field and check if it's label starts with create account:
func testStartsWith() {
let app = XCUIApplication()
let staticTexts = app.webViews.staticTexts // or app.webViews.links
for i in 0..<staticTexts.count {
let text = staticTexts.element(boundBy: i)
if text.label.starts(with: "create account") {
text.tap()
break
}
}
}
Note: Since you are using WebView, there are lot more staticTexts elements than there are links elements. According to printout you provided, your create account view has link trait, so you should be able to use app.webViews.links for faster matching.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With