1

I'm currently developing some UIAutomation tests for an iOS App using KIF Framework. At some point, the app has to open a WebView with a login page, enter username and password and then press the sign-in button and all of these have to be automated.

My question is: knowing that at a certain moment in time the app displays that login page, how can i get the WebView UI element as in:

UIWebView *webView = // . . . ?

Also, it would be great if you could tell me how to get those two text fields (username and password), something like:

UITextField *textField = // ... ?

Thank you.

fanton
  • 720
  • 5
  • 19
  • Your answer is good and it helps me, so i thank you, but how can get that webView? That's my question. – fanton Sep 06 '13 at 09:09
  • my answer my help you in that :D, lemme know if it makes sense.. or if i'm actually addressing your problem – abbood Sep 06 '13 at 09:17
  • @FlaviusAnton check my edited answer or if you satisfied so please right tick my answer.thanks in advanced – Darshan Kunjadiya Sep 06 '13 at 09:23
  • one thing in your question doesn't make sense.. the webview login page is composed of html elements.. and you want to retrieve them as UIKit elements? those two are totally different.. – abbood Sep 06 '13 at 09:25
  • ignoring those trying to help ain't cool brah – abbood Sep 06 '13 at 11:20
  • I'm not ignoring anybody, i was just being afk. I tried you solution below, and seems to work. The problem was that the webwiev didn't have an Accessibility Label, so i've just had to set it manually. – fanton Sep 06 '13 at 12:24
  • I've managed to do what i wanted. Thanks to both of you, these were good answers. – fanton Sep 06 '13 at 19:26

2 Answers2

3

Use this code :

NSString *myText = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.textContent"];

OR For all data

NSString *html = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.outerHTML"];

OR Check this link

I Hope this code useful for you.

Community
  • 1
  • 1
Darshan Kunjadiya
  • 3,323
  • 1
  • 29
  • 31
  • Yeah, that seems great, but only after I get my hands on that webview. How can i do that, in the first place? – fanton Sep 06 '13 at 09:02
2

the idea is that you want to kif to get hold of the webview after the webview has been loaded.. for that you will have to put the notifiying code in the webview delegate function webViewDidFinishLoad

in there.. you can put an NSNotification.. if you've used KIF for a while you know that you can instantiate actions only after you have recieved an NSNotification.. this is the the method in kif:

+ (id)stepToWaitForNotificationName:(NSString*)name object:(id)object;

as for finding the UIWebView itself, you just have to give it the same accessibility label you do with any other UIElement in iOS.

abbood
  • 23,101
  • 16
  • 132
  • 246