I have a UIWebView
in my application which has links inside that when clicked cause different actions to be carried out in the application. I'd like to be able to test this with automated testing so it can be added to a continuous integration build. However, the documentation is rather sparse for UIAWebView
and I've tried to no avail to get this to work.
Has anyone had any success interacting with links inside a UIWebView
so you can check that their action is correct?
The UIWebViewDelegate protocol defines methods that a delegate of a UIWebView object can optionally implement to intervene when web content is loaded. Sets the main page contents, MIME type, content encoding, and base URL. func loadHTMLString(String, baseURL: URL?)
This way of finding UI Automation elements is most useful in automated testing scenarios. To find all elements meeting specific criteria that are related to a known element, you can use FindAll. For example, you could use this method to retrieve list items or menu items from a list or menu, or to identify all controls in a dialog box.
AppQuery.WebView is used for UIWebView controls. AppQuery.Class is used for WKWebView controls. Each of these will be discussed in more detail below. UITest provides the AppQuery.WebView method to obtain a reference to a UIWebView control. The following snippet is an example:
UIWebView is older and compatible with all versions of iOS, while WKWebView is for iOS 8 and higher. Interacting with webviews is a bit more involved as UITest must first obtain a reference to the webview, and then to the DOM elements it contains.
Couldn't find a way to iterate/access links in a UIWebView but would this work for you? Access the page source with:
NSString *source = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].outerHTML"];
then look for the tags that you consider to be links (e.g. href=...)
Then you still need to be able to "press" those links. Is it enough that you navigate to the url given in the link? If so, just use loadRequest to do so. If you need more advanced behaviour (for example make sure correct javascripts are run), you'll need to do more magic to parse those and execute them with stringByEvaluatingJavaScriptFromString.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With