2

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

  1. How can I look into staticTexts with just a substring of the query (here is would be webViews.staticTexts["create acco"].tap() for example) ?
  2. Is there a better way to find and click on this button and associated link ?
standousset
  • 1,092
  • 1
  • 10
  • 25

2 Answers2

7

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()
Oletha
  • 7,324
  • 1
  • 26
  • 46
2

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.

Mladen
  • 2,070
  • 1
  • 21
  • 37