Skip to content

Commit b1ab378

Browse files
jeromelebeltirodkar
authored andcommitted
Adding support for WKWebView
1 parent 37367f8 commit b1ab378

File tree

3 files changed

+49
-17
lines changed

3 files changed

+49
-17
lines changed

EarlGrey.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
/* End PBXAggregateTarget section */
2222

2323
/* Begin PBXBuildFile section */
24+
02559E992147133F0087CD4F /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 02559E982147133F0087CD4F /* WebKit.framework */; };
2425
3F38EF201F20269700EBDFFC /* GREYObjectDeallocationTracker.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F38EF1E1F20269700EBDFFC /* GREYObjectDeallocationTracker.h */; settings = {ATTRIBUTES = (Private, ); }; };
2526
3F38EF211F20269700EBDFFC /* GREYObjectDeallocationTracker.m in Sources */ = {isa = PBXBuildFile; fileRef = 3F38EF1F1F20269700EBDFFC /* GREYObjectDeallocationTracker.m */; };
2627
3F5122141EE1D37E0000CC56 /* GREYTraversal.h in Headers */ = {isa = PBXBuildFile; fileRef = 3F51220E1EE1D37E0000CC56 /* GREYTraversal.h */; settings = {ATTRIBUTES = (Private, ); }; };
@@ -252,6 +253,7 @@
252253
/* End PBXBuildFile section */
253254

254255
/* Begin PBXFileReference section */
256+
02559E982147133F0087CD4F /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = System/Library/Frameworks/WebKit.framework; sourceTree = SDKROOT; };
255257
3F38EF1E1F20269700EBDFFC /* GREYObjectDeallocationTracker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GREYObjectDeallocationTracker.h; sourceTree = "<group>"; };
256258
3F38EF1F1F20269700EBDFFC /* GREYObjectDeallocationTracker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GREYObjectDeallocationTracker.m; sourceTree = "<group>"; };
257259
3F51220E1EE1D37E0000CC56 /* GREYTraversal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = GREYTraversal.h; path = EarlGrey/Traversal/GREYTraversal.h; sourceTree = SOURCE_ROOT; };
@@ -497,6 +499,7 @@
497499
isa = PBXFrameworksBuildPhase;
498500
buildActionMask = 2147483647;
499501
files = (
502+
02559E992147133F0087CD4F /* WebKit.framework in Frameworks */,
500503
61CBDE801D4CD85200206BF7 /* CoreData.framework in Frameworks */,
501504
FD10025B1C5B473400B2DB0A /* OCHamcrest.framework in Frameworks */,
502505
FD10025A1C5B472C00B2DB0A /* IOKit.framework in Frameworks */,
@@ -527,6 +530,7 @@
527530
FD06C6751BECAE6A009032A5 /* Frameworks */ = {
528531
isa = PBXGroup;
529532
children = (
533+
02559E982147133F0087CD4F /* WebKit.framework */,
530534
61CBDE7F1D4CD85200206BF7 /* CoreData.framework */,
531535
FD1002591C5B472C00B2DB0A /* IOKit.framework */,
532536
FD6160601BF2C97800722593 /* OCHamcrest.framework */,

EarlGrey/Action/GREYActions.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,13 +368,13 @@ NS_ASSUME_NONNULL_BEGIN
368368
+ (id<GREYAction>)actionForSetPickerColumn:(NSInteger)column toValue:(NSString *)value;
369369

370370
/**
371-
* Returns an action that executes JavaScript against a UIWebView and sets the return value to
372-
* @c outResult if provided.
371+
* Returns an action that executes JavaScript against a UIWebView or WKWebView and sets the return
372+
* value to @c outResult if provided.
373373
*
374374
* @param js The Javascript code to be executed.
375375
* @param[out] outResult The result of the code execution.
376376
*
377-
* @return A GREYAction that executes JavaScript code against a UIWebView.
377+
* @return A GREYAction that executes JavaScript code against a UIWebView or WKWebView.
378378
*/
379379
+ (id<GREYAction>)actionForJavaScriptExecution:(NSString *)js
380380
output:(__strong NSString *_Nullable *_Nullable)outResult;

EarlGrey/Action/GREYActions.m

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#import "Action/GREYActions.h"
1818

19+
#import <WebKit/WebKit.h>
20+
1921
#import "Action/GREYAction.h"
2022
#import "Action/GREYActionBlock.h"
2123
#import "Action/GREYChangeStepperAction.h"
@@ -48,6 +50,8 @@
4850

4951
static Class gWebAccessibilityObjectWrapperClass;
5052
static Class gAccessibilityTextFieldElementClass;
53+
// Timeout for JavaScript execution using WKWebView.
54+
static const CFTimeInterval kJavaScriptTimeoutSeconds = 60;
5155

5256
@implementation GREYActions
5357

@@ -292,22 +296,46 @@ + (void)initialize {
292296
+ (id<GREYAction>)actionForJavaScriptExecution:(NSString *)js
293297
output:(__strong NSString **)outResult {
294298
// TODO: JS Errors should be propagated up.
295-
id<GREYMatcher> constraints = grey_allOf(grey_not(grey_systemAlertViewShown()),
296-
grey_kindOfClass([UIWebView class]),
297-
nil);
299+
id<GREYMatcher> constraints =
300+
grey_allOf(grey_not(grey_systemAlertViewShown()),
301+
grey_anyOf(grey_kindOfClass([UIWebView class]),
302+
grey_kindOfClass([WKWebView class]), nil),
303+
nil);
304+
BOOL (^performBlock)(id webView, __strong NSError **errorOrNil) = ^(
305+
id webView, __strong NSError **errorOrNil) {
306+
if ([webView isKindOfClass:[WKWebView class]]) {
307+
WKWebView *wkWebView = webView;
308+
__block NSString *resultString = nil;
309+
__block BOOL completionDone = NO;
310+
[wkWebView evaluateJavaScript:js
311+
completionHandler:^(id result, NSError *error) {
312+
resultString = [result description];
313+
completionDone = YES;
314+
}];
315+
NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:kJavaScriptTimeoutSeconds];
316+
while (!completionDone && timeoutDate.timeIntervalSinceNow > 0) {
317+
[[GREYUIThreadExecutor sharedInstance] drainUntilIdle];
318+
}
319+
if (completionDone && outResult) {
320+
*outResult = resultString;
321+
}
322+
return completionDone;
323+
} else if ([webView isKindOfClass:[UIWebView class]]) {
324+
UIWebView *uiWebView = webView;
325+
if (outResult) {
326+
*outResult = [uiWebView stringByEvaluatingJavaScriptFromString:js];
327+
} else {
328+
[uiWebView stringByEvaluatingJavaScriptFromString:js];
329+
}
330+
// TODO: Delay should be removed once webview sync is stable.
331+
[[GREYUIThreadExecutor sharedInstance] drainForTime:0.5]; // Wait for actions to register.
332+
return YES;
333+
}
334+
return NO;
335+
};
298336
return [[GREYActionBlock alloc] initWithName:@"Execute JavaScript"
299337
constraints:constraints
300-
performBlock:^BOOL (UIWebView *webView,
301-
__strong NSError **errorOrNil) {
302-
if (outResult) {
303-
*outResult = [webView stringByEvaluatingJavaScriptFromString:js];
304-
} else {
305-
[webView stringByEvaluatingJavaScriptFromString:js];
306-
}
307-
// TODO: Delay should be removed once webview sync is stable.
308-
[[GREYUIThreadExecutor sharedInstance] drainForTime:0.5]; // Wait for actions to register.
309-
return YES;
310-
}];
338+
performBlock:performBlock];
311339
}
312340

313341
+ (id<GREYAction>)actionForSnapshot:(__strong UIImage **)outImage {

0 commit comments

Comments
 (0)