Home IOS Development ios – Tips on how to write unit take a look at for WKWebView evaluateJavaScript

ios – Tips on how to write unit take a look at for WKWebView evaluateJavaScript

0
ios – Tips on how to write unit take a look at for WKWebView evaluateJavaScript

[ad_1]

I wish to do some unit take a look at relating to WKWebView evaluateJavaScript JS code. JS code runs properly in ViewController.m, however failed in unit take a look at with error of “Area=WKErrorDomain Code=5 “JavaScript execution returned a results of an unsupported sort” UserInfo={NSLocalizedDescription=JavaScript execution returned a results of an unsupported sort}”.

I alter the JS code to a quite simple string @"(operate(){return 5;})()", nonetheless failed.

// ViewController.m 
// works with results of 5

- (void)viewDidLoad {
    [super viewDidLoad];
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
    WKWebView *wkWebView = [[WKWebView alloc] initWithFrame:self.view.body configuration:configuration];
    wkWebView.navigationDelegate = self;
    [self.view addSubview:wkWebView];
    
    [wkWebView loadHTMLString:@"<html></html>" baseURL:nil];
    
    NSString *script = @"(operate(){return 5;})()";
    [wkWebView evaluateJavaScript:script completionHandler:^(id result, NSError * _Nullable error) {
        NSString *value = [NSString stringWithFormat:@"%@", result] ;
        NSLog(@"Worth %@ %@", worth, error);
    }];
}
// unit take a look at 
// failed with error of unsupported sort
 
- (void)testExample {
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
    WKWebView *wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0,0,300,300) configuration:configuration];

    [wkWebView loadHTMLString:@"<html></html>" baseURL:nil];
    
    NSString *script = @"(operate(){return 5;})()";
    [wkWebView evaluateJavaScript:script completionHandler:^(id result, NSError * _Nullable error) {
        NSString *value = [NSString stringWithFormat:@"%@", result] ;
        NSLog(@"Worth %@ %@", worth, error);
        XCTAssertEqualObjects(@"5", worth, @"");
    }];
}

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here