How to simulate mouse click on UIWebView in Cocoa for iPhone?

I am trying to set up automated unit tests for an iPhone application. I am using UIWebView and have to simulate clicks on various links. I've tried doing this with JavaScript , but it doesn't give the same result as manually clicking on links. The main problem is links, which have their own set of properties target

.

When you manually click the standard "popup" link (for example <a href="http://example.com" target="_blank">

), the UIWebView ignores the click event and will not navigate to anything. If you then try to click on that same link automatically using a JavaScript method dispatchEvent()

, the UIWebView will ignore the attribute entirely target

and open the link normally on the current page.

I need to auto-test a module to get the exact same results as when manually clicking a link.

I believe that the only way for this automated unit test to work correctly is to simulate a mouse click on a specific x / y coordinate (i.e. where the link is located). Since unit testing will only be used internally, private API calls are fine.

It seems like it should be possible, as the iPhone's isimulate app seems to do something similar.

Is there a way to do this within the framework?

I found a similar question titled Simulate a mouse click on a window instead of a screen , however I am assuming this method is only valid for OS X, not iPhone OS.

+2


a source to share


4 answers


I am guessing that you could simulate strokes by calling methods touchesBegan

/ touchesEnded

directly on UIView

(check UIResponder

).



+1


a source


The correct way to do this is to create your own UIEvent

and then dispatch that event to an instance UIApplication

using a method -sendEvent:

.



However, there doesn't seem to be a public API to build UIEvent

, so you may be out of luck.

+1


a source


Could you please store the location of the clicks in the data structure that you use in your tests, and then simulate the standard touch events described here. described here

--- Just noticed that you are out of luck with the example at this link. The only other options I can suggest is manipulating the html when run from a test to replace any _target references (you know UIWebView handles them correctly when manually clicked, so I think a small folder is fine for unit test?).

Nice walkthrough in this answer

+1


a source


In your particular case, it may be enough to test in a simulator and use the macOS event generator to make the clicks.

Private calls to capture and dispatch events are part of GraphicServices / GSEvent.h with standard usage at your own risk. Each UIEvent is indeed a UIInternalEvent that has a reference to __GSEvent, so for recording, you can use the _gsEvent property to get the main event.

@property (nonatomic,assign) struct __GSEvent *_gsEvent;

      

I haven't used any of this, but it looks like GSSendSystemEvent would be a good start.

0


a source







All Articles