Reputation: 1161
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
Upvotes: 2
Views: 4552
Reputation: 7639
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()
Upvotes: 8
Reputation: 2210
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.
Upvotes: 2