开发者

Why is my UIWebView empty in my unit test?

I'm putting app-generated content into a UIWebView, and trying to test that I'm doing it correctly. Here's the HTML string I'm putting into the web view:

@"<html><head></head><body><p>Come, let me clutch thee. I have thee not, and yet I see thee still. Art thou not, fatal vision, sensible to feeling as to sight?</p></body></html>"

And it gets into the web view thus:

[self.webView loadHTMLString: [self HTMLStringForSnippet: model.body] baseURL: nil];

where model.body contains just the <p/> element, and -HTMLStringForSnippet: wraps it into well-formed HTML. In my test, I rely on the answers to this question to retrieve the H开发者_StackOverflow中文版TML content of the body via JavaScript:

- (void)testCorrectHTMLLoadedInWebView {
    UIWebView *bodyView = controller.webView;
    NSString *bodyHTML = [bodyView stringByEvaluatingJavaScriptFromString: @"document.body.innerHTML"];
    STAssertEqualObjects(bodyHTML, model.body, @"Model body should be used for the web view content");
}

While I can see by stepping through the code that the UIWebView is created correctly and is passed the correct HTML in -loadHTMLString:baseURL:, in the test bodyHTML is empty. So what do I have to do to see the actual content I expect and previously passed to the web view?


It takes a noticeable time for the UIWebView to render therefore the problem may be that you access the content before it is fully loaded.

Hook on UIWebViewDelegate's webViewDidFinishLoad: method to make sure the content is ready.

Update: Maybe WebViewJavascriptBridge will bring some help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜