Search code examples
iosswiftxcodexcode-ui-testingios-ui-automation

Swift XCUI string assertion failing


I am using xcode 8.3.3 and writing a XCUI test.

I have the following: let address = XCUIApplication().buttons["URL"].value as! String

Looking in the debugger, I can see the value is:

enter image description here

If I set expectedURL = "\u{e2}auth.int....net" then it returns:enter image description here

If I set expectedURL = "auth.int....net" then it returns:enter image description here

How can I make the test assertion find the two strings to be equal?

Tried the following, but it doesn't replace the "\u{e2}":

let address = value.components(separatedBy: ",").first!.replacingOccurrences(of: "\u{e2}", with: "")

And also (but it doesn't replace the "\u{e2}"):

let range = Range<String.Index>(uncheckedBounds: (lower: address.startIndex, upper: address.endIndex))

let strippedAddress = address.replacingOccurrences(of:"\\u{e2}", with: "", options: .literal, range: range)

For the assertion, I am using XCTAssertEqual(address, expectedURL)


Solution

  • You can fix it by separating by alphanumeric characters and then joining by an empty string, as shown below.

    let myString = address.components(separatedBy: CharacterSet.alphanumerics.inverted).joined(separator: "")
    

    myStringis then equal to authintxxxxxxxxxnet (no "." characters), so you should just be able to change your expected URL to match.

    Hope that helps!