How to simulate a mouse click on a UIWebView in Cocoa for the iPhone? - iphone

I'm trying to setup automated unit tests for an iPhone application. I'm using a UIWebView and need to simulate clicks on different links. I've tried doing this with JavaScript, but it doesn't produce the same result as when I manually click on the links. The main problem is with links that have their target property set.
When you manually click on a standard "popup" link (e.g. <a href="http://example.com" target="_blank">), the UIWebView will ignore the click event and won't navigate to anything. If you then try clicking on this very same link automatically via the JavaScript dispatchEvent() method, the UIWebView will completely ignore the target attribute and will open up the link normally in the current page.
I need an my automatic unit testing to produce the exact same results as when you manually click a link.
I believe the only way for this automated unit test to work correctly is to simulate a mouse click at a specific x/y coordinate (i.e. where the link is located). Since the unit testing will only be used internally, private API calls are fine.
It seems like this should be possible since the iPhone app isimulate seems to do something similar.
Is there any way to do this in the framework?
I found a similar question titled Simulate mouse click to window instead of screen, however I'm guessing this method is only valid for OS X, and not for iPhone OS.

I suppose you could simulate the touches by calling the touchesBegan/touchesEnded methods directly on the UIView (check the UIResponder class).

The proper way to do this would be to construct your own UIEvent and then post this event to your UIApplication instance via its -sendEvent: method.
However, there doesn't appear to be a public API for constructing a UIEvent, so you might be out of luck.

Could you store the locations of the clicks in a data structure that you use in your tests and then simulate standard touch events as described here described here
--- Just spotted that you didn't have much luck with the example on this link. The only other options I can suggest would be to manipulate the html when running from a test to replace any _target links (you know that UIWebView handles these properly when clicking manually, so I think a small bodge is ok for the unit test?).
Nice walkthrough in this answer

For your specific case, it may be sufficient to test in the simulator and use a MacOS event generator to make the clicks.
The private calls for recording and sending events are part of GraphicServices/GSEvent.h with the standard use at your own risk disclaimers. Every UIEvent is really a UIInternalEvent that has a reference to a __GSEvent, so for recording you can use the _gsEvent property to get the underlying event.
#property (nonatomic,assign) struct __GSEvent *_gsEvent;
I have not used any of this stuff, but it looks like GSSendSystemEvent would be a good place to start.

Related

Function across multiple view controllers

I am trying to make a function so that I request a 'manager override' where it presents a screen for a manager to enter their password and press an approve button. How should I do this? Will it have to be in multiple functions? Is there a way that I can call one function and it present the information back? Would a completion work for what I need? I have no ideas where to start for this.
My set up is as follows:
A view controller asks for manager approval, then a screen slides up with text boxes and an approve button. I want the approve button to trigger authenticating and dismissing the screen
Assuming you don't want a Framework target (that sounds like overkill for what you want) simply mark the function as "public" and move it outside of any class. I just tried in a sample project and it works.
It looks important - remember to keep it in a file already in the project. (My sample project didn't work with menu option File|Add|New|File.)
Now, if you really want portability, check out how to create a Framework project.

Implement swipe gestures in apple watch using watchOS2.0

Hi I need to implement a functionality in my apple watch where when user swipes down will move from one module(InterfaceController1) to another module(InterfaceController2).And when user force touch menu should pop up and tapping on any button in the menu should present a model.
I am able to implement forceTouch menu option. But where as with swipe down to move to second module, i am not able to get any doc about this.
Please let me know if someone is aware of it.
Short Answer:
No, You can't.
Long Answer:
All WKInterface objects are proxy objects(aka Remote UI) that allows you to send queries to real UI Objects.
Reminds that the bundle that contains storyboard is separated with extension bundle. In sand-box concept, Your code that running on extension bundle can't access real UI Objects directly.
So there is no way to react against of user actions except that are available with interface builder(aka sentAction).

How to Extract the information about UI elements during runtime of an iOS application?

Is there any way that one could extract the information about UI elements (of the UIView) from the application’s memory during runtime of an iOS application (iPhone). Like getting a reference to the current UIView element and find a way to enumerate all UI elements contained in that view and create an abstract graph of the UIView calls of the app dynamically?
Since it looks like you may be interested n some general suggestions, I'll give you this one: look at DCIntrospect (available on github, with good documentation).
With this open source software, you can examine any item in your UI that subclasses UIView. You can see its many properties in your console.
It is very easy to use. I tried it, and it was a very simple add to my application delegate. To enable it, you press the space bar on your keyboard.
Since it is open source, you can examine the code and see for yourself what UI element properties are available and even make changes yourself.
UIView comes with an undocumented function to do that: -recursiveDescription. It's not as pretty as DCIntrospect, but you don't have to add anything to your project. Simply do:
NSLog(#"%#", [view recursiveDescription]);
or you can call it from the debugger:
p [view recursiveDescription]
See Apple's Technical Note 2239 (scroll to the bottom) for more details, and other interesting debugging commands.

How do some AppStore browser apps implement features such as "Open in new tab" menu, loading progress, etc?

There are web browser apps in the store which do many things that seem like they use private API calls to me. Things such as:
Override the context menu for a link to add a menu item for "Open in new tab" (How to add an option to the popup actionsheet of iphone safari?)
Reporting loading progress.
Catching new "popup" windows (where link target is not '_self') and opening in a new tab (requires subclassing UIWebView and overriding private methods.)
Does anyone have an idea if these are done in some clever way that I dont know about, or have they just managed to sneak these in past app reviewers? This would surprise me since they have the whole automated-api-use scanner thing, unless it only scans more important api calls...
No real reason I'm asking this other than academics. Was just playing around with UIWebView and noticed the functionality is very limited compared to what some people have managed to do with it...
Any thoughts? Would love some insight!
Cheers
The first one can probably be achieved with Javascript - that's just a guess on my part; you use JS to communicate back to the Obj-C code to show a custom UIActionSheet.
The second is probably possible if you look deep enough:
UIWebView has an NSURLRequest object. NSURLRequest has valueForHTTPHeaderField:, which enables you to get the total content size of the request. NSURLRequest also has HTTPBodyStream, which is an NSInputStream, which should give you the total number of bytes read.
Finally, for catching "popup" windows, my guess is that they are using the delegate callbacks of UIWebView to accomplish this, with some clever string-fu to determine if it's a pop-up or not (evaluating Javascript and such).

How to implement iPhone TitleBar in Android

You know in iPhone, The four components of a typical iPhone application are
a title bar,
a navigation list,
a destination page,
and a button bar.
for The Title Bar
The title bar includes the following elements:
Back button:
Screen title:
Command button:
My question comes, although i can use UI framework tool, such as:
Phonegap,
iui,
jtquery
to develop web app, but i can't display the TitleBar effect in Android,
i can't use below code in Android:
meta content="yes" name="apple-mobile-web-app-capable"
Actually, the above can work in Apple Safari Browser, but I can't use Safari to display our Android Project, which is i don't want to see.
Does anyone know how to fulfill this effect in Android, please help me, your help will be great appreciated.
to
"It's unclear if you developing a native android application or a webapp.
– alexanderblom yesterday"
sorry that i forget to mention, i want to develop a iphone-webapp-style application in Android. Since it's too difficult, i want to use iWebkit, but it seems only work in apple iPhone OS or Safari which contain the specific engine to display the beautiful layout, like below:( -_-! i am new so i can't post images here)
(source: appshopper.com)
If I'm not mistaken, if you want to custom in any way the title bar at the top of your screen, you have to do several things :
First, read this thread about titlebar customisation, as it's quite detailled.
Then, you have to add a few more things to it to match your requirements, all in the same xml file which will be your titlebar.xml:
-a button, on which listener you assign the same keyEvent as the normal back key of every android phone.
-a textview for whatever title you want to put in.
-a button for the command button, for whatever it does in Iphone (if you tell me what it does, maybe i'll be able to give more details)
Then, as I take it, you want to have it available on all your pages, without having to request titlebar customization on every activity, I would recommend you to use the same trick as I did : Define a superclass, which all your activities will extend.
then you you requestwindowfeatures in this class, and do all your customization in this one.Finnaly, You just have to call the variable of your titlebar textview in the oncreate of all your subclasses to assign the string you want on each page. If your string is generated dynamically from, lets say a file name you load from the internetv in a background thread, you will need to define a handler to the main thread as you can't update a view from another thread than the one that has created it.
Hopes that helps. If you have any question about that, feel free to ask, i'll keep an eye on your topic, because I had quite a pain to make my titlebar work properly.
good luck