Swift UITesting error: Invalid escape sequence in literal. \U201c
Asked Answered
P

2

9

I am building an automation suite using Xcode 7 with swift.

My app loads with the following Alert View:

Allow "Light Alarm" to access your location while you use the app?

When I record with UI Testing and click this alert I get the following code: app.alerts["Allow \U201cLight Alarm\U201c to access your location while you use the app?"]

Note: The quotes has been replaced with \U201c

However, when I try and compile I get the following error: "Invalid escape sequence in literal"

Anyone know how to get round this?

Parson answered 22/9, 2015 at 8:53 Comment(3)
This seems to be an Xcode bug when generating code during UI testing, compare #32432568 for a similar issue.Dram
Note: The problem here is not that OP does not know how to escape unicode characters in strings. This is code created by Xcode during UI test recording.Dram
That is correct. This is an Xcode bug when generating code. Any ideas what the code created 'should' be as per the question?Parson
D
14

This seems to be a bug in Xcode when generating code during UI recording. Swift uses \u{NNNN} escape sequences in string literals, so

app.alerts["Allow \u{201c}Light Alarm\u{201c} ..."]

would be correct, or simply

app.alerts["Allow “Light Alarm“ ..."]

(Actually it should be "Allow “Light Alarm” ..." where the second quotation mark is U+201D = RIGHT DOUBLE QUOTATION MARK :)

A similar issue for UI recorded code in Objective-C was reported in Incomplete universal character name in UI Testing.

I do not know a workaround, it seems that the only thing you can do at present is to fix the code after recording (and sent a bug report to Apple).

Dram answered 22/9, 2015 at 9:41 Comment(5)
Its very weird as your quotes match perfectly what is on screen, but even though the UIAlertView is displayed I am getting a false boolean returned by the query: return app.alerts["Allow “Light Alarm” to access your location while you use the app?"].existsParson
@CharlieSeligman: You would have to use app.alerts["Allow “Light Alarm“ ..."]. – My remark "Actually it should be ..." was only meant as a gentle reminder that there are two different (right and left) quotation marks. Your code uses the left double quotation mark only.Dram
Does this mean adding tags to the views or is it enough to convert the Unicode value inserted by Swift?Depart
Any one got a work around for this? Although when I look at the xcode debugger console, the string is displayed as expected but fails when I call .exists method?Corrinacorrine
@Corrinacorrine see my answer below, try to install new version of XcodeEntire
E
0

Installing Xcode 7.3 fixed this issue for me

Entire answered 29/3, 2016 at 19:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.