Wednesday, December 6, 2023
HomeiOS Developmentios - Learn how to write unit take a look at for...

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


I wish to do some unit take a look at concerning WKWebView evaluateJavaScript JS code. JS code runs nicely 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 modify 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, @"");
    }];
}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments