Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing UIWebView contents from UI Automation in Instruments

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?

like image 232
xoebus Avatar asked Jun 08 '11 13:06

xoebus


People also ask

What is the use of uiwebviewdelegate?

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?)

How do I find UI Automation elements?

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.

How do I get a reference to a UIWebView control?

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:

What is the difference between uitest UIWebView and wkwebview?

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.


1 Answers

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.

like image 158
Mayoneez Avatar answered Oct 31 '22 19:10

Mayoneez