Skip to content

Commit fa2ffbd

Browse files
committed
Fix issue when tapping accessibility element's view not available yet
1 parent a72a9b8 commit fa2ffbd

File tree

4 files changed

+80
-40
lines changed

4 files changed

+80
-40
lines changed

CHANGELOG.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,32 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1010

1111
### Removed
1212

13-
## [0.1.7](https://github.com/openium/SwiftiumTestingKit/compare/latest...HEAD)
13+
14+
## [0.1.8](https://github.com/openium/SwiftiumTestingKit/compare/v0.1.8...HEAD)
15+
### Added
16+
17+
### Changed
18+
Fix issue when tapping accessibility element's view not available yet
19+
20+
### Removed
21+
22+
## [0.1.7](https://github.com/openium/SwiftiumTestingKit/compare/v0.1.7...HEAD)
1423
### Added
1524
Allow pilotable server to specify responses headers for all requests and responses headers for each request
1625

1726
### Changed
1827

1928
### Removed
2029

21-
## [0.1.6](https://github.com/openium/SwiftiumTestingKit/compare/latest...HEAD)
30+
## [0.1.6](https://github.com/openium/SwiftiumTestingKit/compare/v0.1.6...HEAD)
2231
### Added
2332

2433
### Changed
2534
Fixed search for text containing \n
2635

2736
### Removed
2837

29-
## [0.1.5](https://github.com/openium/SwiftiumTestingKit/compare/latest...HEAD)
38+
## [0.1.5](https://github.com/openium/SwiftiumTestingKit/compare/v0.1.5...HEAD)
3039
### Added
3140

3241
### Changed

STKTestAppTests/STKSoloTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,19 @@ class STKSoloTests: XCTestCase {
174174
XCTAssertTrue(waitForCell)
175175
XCTAssertTrue(waitForDeleteConfirmationButton)
176176
}
177+
178+
func testWaitForText_onAlertViewController_shouldFindOKButton() {
179+
// Given
180+
sut.showViewControllerInCleanWindow(viewController)
181+
let alertVC = UIAlertController(title: "title", message: "message", preferredStyle: .alert)
182+
let alertOKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
183+
alertVC.addAction(alertOKAction)
184+
viewController.present(alertVC, animated: false, completion: nil)
185+
186+
// When
187+
let waitForOK = sut.waitFor(tappableText: "OK", andTapIt: true)
188+
189+
// Expect
190+
XCTAssertTrue(waitForOK)
191+
}
177192
}

SwiftiumTestingKit/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>0.1.7</string>
18+
<string>0.1.8</string>
1919
<key>CFBundleVersion</key>
2020
<string>$(CURRENT_PROJECT_VERSION)</string>
2121
</dict>

SwiftiumTestingKit/STKSolo.swift

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import SimulatorStatusMagiciOS
1414
public class STKSolo: NSObject {
1515
public var animationSpeed: Float = 1.0
1616
public var timeToWaitForever: Double = 20
17+
static var isSingleTestRunning: Bool = {
18+
return isSingleTestRun()
19+
}()
20+
1721
var internalTimeToWaitForever: Double {
1822
var time = timeToWaitForever
1923
if isUserJenkinsOrHudson() {
@@ -83,6 +87,15 @@ public class STKSolo: NSObject {
8387
self.window = nil
8488
}
8589

90+
func waitRunLoops(timeBetweenChecks: TimeInterval) {
91+
RunLoop.current.run(mode: .default, before: Date(timeIntervalSinceNow: timeBetweenChecks / 3.0))
92+
RunLoop.current.run(mode: .common, before: Date(timeIntervalSinceNow: timeBetweenChecks / 3.0))
93+
RunLoop.current.run(mode: .tracking, before: Date(timeIntervalSinceNow: timeBetweenChecks / 3.0))
94+
if !STKSolo.isSingleTestRunning {
95+
CATransaction.flush() // prevents animations
96+
}
97+
}
98+
8699
func waitClosureToReturnTrue(_ closure: () -> Bool,
87100
withinTimeout timeout: TimeInterval,
88101
timeBetweenChecks: TimeInterval) {
@@ -92,7 +105,7 @@ public class STKSolo: NSObject {
92105
if closure() {
93106
break
94107
}
95-
RunLoop.current.run(mode: .default, before: Date(timeIntervalSinceNow: timeBetweenChecks))
108+
waitRunLoops(timeBetweenChecks: timeBetweenChecks)
96109
now = Date.timeIntervalSinceReferenceDate
97110
} while (now - start) < timeout
98111
}
@@ -136,8 +149,14 @@ public class STKSolo: NSObject {
136149
let cleanedText = accessibilityCleaned(text: tappableText)
137150
let element = waitForAccessibilityElement { $0.accessibilityLabel == cleanedText }
138151
if let element = element {
139-
if let view = try? UIAccessibilityElement.viewContaining(element, tappable: true) {
152+
var view: UIView? = nil
153+
waitClosureToReturnTrue({ () -> Bool in
154+
view = try? UIAccessibilityElement.viewContaining(element, tappable: true)
155+
return view != nil
156+
}, withinTimeout: timeoutForWaitForMethods, timeBetweenChecks: timeBetweenChecks)
157+
if let view = view {
140158
testActor.tap(element, in: view)
159+
waitRunLoops(timeBetweenChecks: timeBetweenChecks)
141160
} else {
142161
return false
143162
}
@@ -216,45 +235,42 @@ public class STKSolo: NSObject {
216235
#endif
217236
}
218237

219-
extension STKSolo {
220-
221-
func isUserJenkinsOrHudson() -> Bool {
222-
var username = NSUserName()
223-
#if targetEnvironment(simulator)
224-
if username.isEmpty {
225-
let bundlePathComponents = (Bundle.main.bundlePath as NSString).pathComponents
226-
if bundlePathComponents.count >= 3 && bundlePathComponents[0] == "/" && bundlePathComponents[1] == "Users" {
227-
username = bundlePathComponents[2]
228-
}
229-
}
230-
#endif
231-
return username == "hudson" || username == "jenkins"
232-
}
233-
234-
func isSingleTestRun() -> Bool {
235-
/*
236-
XCTestConfiguration *testConfiguration = [XCTestConfiguration activeTestConfiguration];
237-
// KO when running test of only one class (testsToRun is an Array<String> with class/test names, or just class name)
238-
return [testConfiguration.testsToRun count] == 1 && [[testConfiguration.testsToRun anyObject] containsString:@"/"];
239-
*/
240-
if let plistPath = ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"],
241-
let plistBinary = FileManager.default.contents(atPath: plistPath),
242-
let unarchivedObject = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(plistBinary),
243-
let object = unarchivedObject as? NSObject,
244-
let testsToRun = object.value(forKey: "testsToRun") as? NSSet,
245-
testsToRun.count == 1,
246-
let singleTestToRun = testsToRun.anyObject() as? NSString {
247-
return singleTestToRun.contains("/")
248-
} else {
249-
return false
238+
func isUserJenkinsOrHudson() -> Bool {
239+
var username = NSUserName()
240+
#if targetEnvironment(simulator)
241+
if username.isEmpty {
242+
let bundlePathComponents = (Bundle.main.bundlePath as NSString).pathComponents
243+
if bundlePathComponents.count >= 3 && bundlePathComponents[0] == "/" && bundlePathComponents[1] == "Users" {
244+
username = bundlePathComponents[2]
250245
}
251246
}
252-
253-
func isMultipleTestsRun() -> Bool {
254-
return XCTestSuite.default.testCaseCount > 1
247+
#endif
248+
return username == "hudson" || username == "jenkins"
249+
}
250+
251+
func isSingleTestRun() -> Bool {
252+
/*
253+
XCTestConfiguration *testConfiguration = [XCTestConfiguration activeTestConfiguration];
254+
// KO when running test of only one class (testsToRun is an Array<String> with class/test names, or just class name)
255+
return [testConfiguration.testsToRun count] == 1 && [[testConfiguration.testsToRun anyObject] containsString:@"/"];
256+
*/
257+
if let plistPath = ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"],
258+
let plistBinary = FileManager.default.contents(atPath: plistPath),
259+
let unarchivedObject = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(plistBinary),
260+
let object = unarchivedObject as? NSObject,
261+
let testsToRun = object.value(forKey: "testsToRun") as? NSSet,
262+
testsToRun.count == 1,
263+
let singleTestToRun = testsToRun.anyObject() as? NSString {
264+
return singleTestToRun.contains("/")
265+
} else {
266+
return false
255267
}
256268
}
257269

270+
func isMultipleTestsRun() -> Bool {
271+
return XCTestSuite.default.testCaseCount > 1
272+
}
273+
258274
extension STKSolo: KIFTestActorDelegate {
259275
public func fail(with exception: NSException!, stopTest stop: Bool) {
260276
lastExceptions.append(exception)

0 commit comments

Comments
 (0)