Get the current UIWebVIew element - ios

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.

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.

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.

Related

XCUITest - How to check if webview has updated its content?

I've got a webview that changes it's content when a button is pressed inside the webview. Is it possible to detect if webview has changed its content in XCUITest.
Edit: I would like a general way to know if the webview has updated without having to know the content the webview will update to–prior to the update.
Sure !
You can get your webview doing something like this:
let webview = app.webviews.firstMatch
Then, you can do some assertions on the webview content:
webview.staticTexts["TestString"].firstMatch.exists

How to auto tap a button inside webview and how to autofill a form inside webview

Need help on ios webview
I am trying to autofill a form located on any website can get path by ID or Xpath.
same with button want to know how to autotap send button after filling a form located on any website can get buttons xpath/ID
In selenium it was simple and easy dont know how to dive into webview looked everywhere
You use the javascript bridge to perform operations on elements using js. For example to tap on a button:
let jsString = "document.getElementById('myButtonID').click()"
myWebView.evaluateJavaScript(jsString, completionHandler: { result,error in
print(result);
})

How to make iOS UIWebView display a mobile website correctly?

I am new to iOS app development and I just tried using UIWebView to display a mobile website in my app and was not quite successful.
What I did is just some minimal Xcode project configuration and coding that I found during some Googling efforts:
Create a new iOS application project in Xcode using the Single View
Application template.
Drag a Web View from the Object Library to the View Controller
scene of Main.storyboard.
While holding down the Control key, drag the Web View from the View
Controller scene to the ViewController.h editor, resulting a
source code line like this:
#property (weak, nonatomic) IBOutlet UIWebView *myWebView;
Add the following code in ViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURL *url = [NSURL URLWithString:#"http://www.amazon.com"];
[self.myWebView loadRequest:[NSURLRequest requestWithURL:url]];
}
This is all what I did (and also everything those tutorials told me). After I built and ran the app (on simulator and on real iphone device), the site did load in the app's view (and did load the mobile version instead of the PC version), but it displayed the site as if the view were much larger than the actual iphone screen. Here is a screeshot (Amazon in this case but basically the same for other sites):
What should I do to make the iOS UIWebView display a mobile website correctly?
Just tried Dipen Chudasama's suggestion of disabling the "User Size Classes" option, for the first time of all those suggestions, the result did change a little bit (from left-align to sort of center-align), but still not what I am looking for. Here is the screenshot:
EDIT:
Thanks for all the suggestions given by you enthusiastic people. Loading a site like amazon.com in a UIWebView should be a rather easy task as I understand, nevertheless, I didn't succeed with any of the suggestions.
It would be great if anyone could share with me (via Github or alike) just an sample xcode project (starting from scratch with the latest version of xcode tool chain) with nothing but a UIWebView that could load amazon.com correctly. That way I can do a line by line diff and may be able to find what I did wrong in my own project.
The UIWebView has a property called "scalesPageToFit":
self.myWebView.scalesPageToFit = YES;
self.myWebView.contentMode = UIViewContentModeScaleAspectFit;
should do the trick.
You can scale the page to fit the screen width with this UIWebview property webView.scalesPageToFit = YES;
Otherwise you can run a js command in the UIWebView to do the scale:
NSString *js = [NSString stringWithFormat:#"document.body.style.zoom = 0.8;"];
[webView stringByEvaluatingJavaScriptFromString:js];
I got it..you just set iPhone size view in storyboard instead of any size, means disable Use Size Classes Check box and see, this will work..:)
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:#"http://www.amazon.com"];
[self.myWebView loadRequest:[NSURLRequest requestWithURL:url]];
}
Thats it, Try this one only.
OR
you have to set appropriate constraint for this.
I had the same issue as you #goodbyeera, and then realized I hadn't set the AutoLayout constraints on the UIWebView. Therefore, when the WebView loaded it was too big for the iphone screen. By adding the constraints to fit the UIWebView to the screen fixed it all. Hope this might help you.

Possible webview with menu buttons that load different urls?

I am new to xcode development so hang in there with my lack of knowledge. (BTW i've searched for this already with no luck).
I have a webview app I am working on, I do not want the users using the onsite menu because it's not optimal for mobile yet. So I want to offer a menu on the app that loads up various in-site url's. I have created a menu and added the back and refresh buttons so I have gotten that far but cannot figure out how to get other buttons i've created (example "categories") to load example.com/categories and hope someone can help me as this will save me much frustration. The app is a single webview so i'm not sure if that plays a role in this. Thank you in advance.
Let say that your buttons trig the same function and you have set their tag, you could do :
-(IBAction)loadButton:(id)sender
{
NSString *urlString;
switch(((UIButton*)sender).tag)
{
case 1:
urlString=#"http://www.exemple.com/categories";
break;
case 2:
//...
}
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
}

Remove UIWebView selection highlight?

So in a UIWebView, whenever you press anything, a grey selection box appears. Like this:
I know you can remove it if you have access to the source code of the website, but obviously in this example I'm using YouTube, which I cannot access. Can I remove the grey highlight within Xcode?
Many thanks!
Why not use JavaScript-injection?
[webView stringByEvaluatingJavaScriptFromString:
#"document.body.style.setProperty(\"-webkit-tap-highlight-color\", \"rgba(0,0,0,0)\");"
];

Resources