@@ -28,25 +28,28 @@ - (void)setUp
2828 _app = [[XCUIApplication alloc ] init ];
2929 [_app launch ];
3030
31- _window = _app.windows [@" RNCAsyncStorageExample macOS" ];
32- XCTAssert (_window.exists );
31+ NSPredicate *exists = [NSPredicate predicateWithFormat: @" exists == 1" ];
32+
33+ _window = _app.windows [@" AsyncStorageExample" ];
34+ [self waitForExpectation: exists evaluatedWithObject: _window timeout: 5 ];
3335
3436 _restartButton = _window.otherElements [@" restart_button" ].staticTexts .firstMatch ;
35- XCTAssert (_restartButton. exists ) ;
37+ [ self waitForExpectation: exists evaluatedWithObject: _restartButton timeout: 5 ] ;
3638
3739 _getSetClearButton = _window.buttons [@" testType_getSetClear" ].staticTexts .firstMatch ;
38- XCTAssert (_getSetClearButton. exists ) ;
40+ [ self waitForExpectation: exists evaluatedWithObject: _getSetClearButton timeout: 5 ] ;
3941
4042 _mergeItemButton = _window.buttons [@" testType_mergeItem" ].staticTexts .firstMatch ;
41- XCTAssert (_mergeItemButton. exists ) ;
43+ [ self waitForExpectation: exists evaluatedWithObject: _mergeItemButton timeout: 5 ] ;
4244
43- [self sendTestAppCommand: @" rnc-asyncstorage://clear-all-storage" ];
45+ [self setDelegate ];
46+ [self clear ];
47+ [self unsetDelegate ];
48+ [self clear ];
4449}
4550
4651- (void )testShouldStoreValueInAsyncStorage
4752{
48- [self sendTestAppCommand: @" rnc-asyncstorage://unset-delegate" ];
49-
5053 [_getSetClearButton click ];
5154
5255 XCUIElement *storedNumber = _window.staticTexts [@" storedNumber_text" ];
@@ -64,13 +67,11 @@ - (void)testShouldStoreValueInAsyncStorage
6467
6568 NSString *expectedText = [NSString stringWithFormat: @" %d " , tapTimes * 10 ];
6669
67- XCTAssertEqualObjects (storedNumber.label , expectedText);
70+ XCTAssertEqualObjects (storedNumber.value , expectedText);
6871}
6972
7073- (void )testShouldClearItem
7174{
72- [self sendTestAppCommand: @" rnc-asyncstorage://unset-delegate" ];
73-
7475 [_getSetClearButton click ];
7576
7677 XCUIElement *increaseBy10Button =
@@ -85,36 +86,8 @@ - (void)testShouldClearItem
8586 XCTAssertEqualObjects (storedNumber.label , @" " );
8687}
8788
88- - (NSString *)performInputWithFormat :format
89- {
90- NSString *name = arc4random_uniform (2 ) == 0 ? @" Jerry" : @" Sarah" ;
91- NSString *age = arc4random_uniform (2 ) == 0 ? @" 21" : @" 23" ;
92- NSString *eyeColor = arc4random_uniform (2 ) == 0 ? @" blue" : @" green" ;
93- NSString *shoeSize = arc4random_uniform (2 ) == 0 ? @" 9" : @" 10" ;
94-
95- XCUIElement *nameInput = _window.textFields [@" testInput-name" ];
96- [nameInput click ];
97- [nameInput typeText: name];
98-
99- XCUIElement *ageInput = _window.textFields [@" testInput-age" ];
100- [ageInput click ];
101- [ageInput typeText: age];
102-
103- XCUIElement *eyesInput = _window.textFields [@" testInput-eyes" ];
104- [eyesInput click ];
105- [eyesInput typeText: eyeColor];
106-
107- XCUIElement *showInput = _window.textFields [@" testInput-shoe" ];
108- [showInput click ];
109- [showInput typeText: shoeSize];
110-
111- return [NSString stringWithFormat: format, name, age, eyeColor, shoeSize];
112- }
113-
11489- (void )testShouldMergeItemsInAsyncStorage
11590{
116- [self sendTestAppCommand: @" rnc-asyncstorage://unset-delegate" ];
117-
11891 [_mergeItemButton click ];
11992
12093 XCUIElement *saveItemButton = _window.buttons [@" saveItem_button" ].staticTexts .firstMatch ;
@@ -128,7 +101,7 @@ - (void)testShouldMergeItemsInAsyncStorage
128101 [saveItemButton click ];
129102 [_restartButton click ];
130103 [restoreItemButton click ];
131- XCTAssertEqualObjects (storyText.label , story);
104+ XCTAssertEqualObjects (storyText.value , story);
132105 [_restartButton click ];
133106
134107 // merging here
@@ -137,16 +110,15 @@ - (void)testShouldMergeItemsInAsyncStorage
137110 [mergeItemButton click ];
138111 [_restartButton click ];
139112 [restoreItemButton click ];
140- XCTAssertEqualObjects (storyText.label , newStory);
113+ XCTAssertEqualObjects (storyText.value , newStory);
141114}
142115
143116- (void )testMergeItemDelegate
144117{
145- [self sendTestAppCommand: @" rnc-asyncstorage://set-delegate " ];
118+ [self setDelegate ];
146119
147120 [_mergeItemButton click ];
148121
149- XCUIElement *saveItemButton = _window.buttons [@" saveItem_button" ].staticTexts .firstMatch ;
150122 XCUIElement *restoreItemButton = _window.buttons [@" restoreItem_button" ].staticTexts .firstMatch ;
151123 XCUIElement *mergeItemButton = _window.buttons [@" mergeItem_button" ].staticTexts .firstMatch ;
152124 XCUIElement *storyText = _window.staticTexts [@" storyTextView" ];
@@ -157,13 +129,69 @@ - (void)testMergeItemDelegate
157129 [mergeItemButton click ];
158130 [_restartButton click ];
159131 [restoreItemButton click ];
160- XCTAssertEqualObjects (storyText.label , story);
132+ XCTAssertEqualObjects (storyText.value , story);
133+ }
134+
135+ #pragma mark - Private
136+
137+ - (void )clear
138+ {
139+ [_getSetClearButton click ];
140+ [self clickButton: @" clear_button" ];
141+ }
142+
143+ - (void )clickButton : (NSString *)identifier
144+ {
145+ NSPredicate *exists = [NSPredicate predicateWithFormat: @" exists == 1" ];
146+ XCUIElement *button = _window.buttons [identifier].staticTexts .firstMatch ;
147+ [self waitForExpectation: exists evaluatedWithObject: button timeout: 5 ];
148+ [button click ];
149+ }
150+
151+ - (NSString *)performInputWithFormat :format
152+ {
153+ NSString *name = arc4random_uniform (2 ) == 0 ? @" Jerry" : @" Sarah" ;
154+ NSString *age = arc4random_uniform (2 ) == 0 ? @" 21" : @" 23" ;
155+ NSString *eyeColor = arc4random_uniform (2 ) == 0 ? @" blue" : @" green" ;
156+ NSString *shoeSize = arc4random_uniform (2 ) == 0 ? @" 9" : @" 10" ;
157+
158+ XCUIElement *nameInput = _window.textFields [@" testInput-name" ];
159+ [nameInput click ];
160+ [nameInput typeText: name];
161+
162+ XCUIElement *ageInput = _window.textFields [@" testInput-age" ];
163+ [ageInput click ];
164+ [ageInput typeText: age];
165+
166+ XCUIElement *eyesInput = _window.textFields [@" testInput-eyes" ];
167+ [eyesInput click ];
168+ [eyesInput typeText: eyeColor];
169+
170+ XCUIElement *showInput = _window.textFields [@" testInput-shoe" ];
171+ [showInput click ];
172+ [showInput typeText: shoeSize];
173+
174+ return [NSString stringWithFormat: format, name, age, eyeColor, shoeSize];
175+ }
176+
177+ - (void )setDelegate
178+ {
179+ [_mergeItemButton click ];
180+ [self clickButton: @" setDelegate_button" ];
181+ }
182+
183+ - (void )unsetDelegate
184+ {
185+ [_mergeItemButton click ];
186+ [self clickButton: @" unsetDelegate_button" ];
161187}
162188
163- - (void )sendTestAppCommand : (NSString *)URLString
189+ - (void )waitForExpectation : (NSPredicate *)predicate
190+ evaluatedWithObject : (id )object
191+ timeout : (NSTimeInterval )timeout
164192{
165- [[ NSWorkspace sharedWorkspace ] openURL: [ NSURL URLWithString: URLString] ];
166- sleep (. 25 ) ;
193+ [self expectationForPredicate: predicate evaluatedWithObject: object handler: nil ];
194+ [ self waitForExpectationsWithTimeout: timeout handler: nil ] ;
167195}
168196
169197@end
0 commit comments