如何在 Cocoa for iPhone 中模拟鼠标点击 UIWebView?

发布于 2024-08-30 06:14:44 字数 1041 浏览 8 评论 0原文

我正在尝试为 iPhone 应用程序设置自动化单元测试。我正在使用 UIWebView 并需要模拟对不同链接的点击。我尝试过 使用 JavaScript 执行此操作,但它不会产生与我手动单击链接时相同的结果。主要问题在于设置了 target 属性的链接。

当您手动单击标准“弹出”链接(例如 )时,UIWebView 将忽略单击事件并且不会导航到任何内容。如果您随后尝试通过 JavaScript dispatchEvent() 方法自动单击同一个链接,UIWebView 将完全忽略 target 属性,并在当前页面。

我需要自动单元测试来生成与手动单击链接时完全相同的结果。

我相信这个自动化单元测试正确工作的唯一方法是在特定的 x/y 坐标(即链接所在的位置)模拟鼠标单击。由于单元测试仅在内部使用,因此私有 API 调用就可以了。

看来这应该是可能的,因为 iPhone 应用程序 isimulate 似乎做了类似的事情。

有没有办法在框架中做到这一点?

我发现了一个类似的问题,标题为模拟鼠标点击窗口而不是屏幕,但是我猜这个方法只对 OS X 有效,对 iPhone OS 无效。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

醉生梦死 2024-09-06 06:14:45

我想您可以通过直接在 UIView 上调用 touchesBegan/touchesEnded 方法来模拟触摸(检查 UIResponder 类)。

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

蹲墙角沉默 2024-09-06 06:14:45

执行此操作的正确方法是构造您自己的 UIEvent,然后通过其 -sendEvent: 方法将此事件发布到您的 UIApplication 实例。

但是,似乎没有用于构造 UIEvent 的公共 API,因此您可能不走运。

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.

宁愿没拥抱 2024-09-06 06:14:45

您可以将点击位置存储在测试中使用的数据结构中,然后按照此处所述模拟标准触摸事件此处描述

--- 刚刚发现您对该链接上的示例没有太多运气。我可以建议的唯一其他选项是在从测试运行时操作 html 以替换任何 _target 链接(您知道 UIWebView 在手动单击时可以正确处理这些链接,所以我认为对于单元测试来说,一个小边界是可以的?)。

这个答案中的很好的演练

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

难忘№最初的完美 2024-09-06 06:14:45

对于您的具体情况,在模拟器中进行测试并使用 MacOS 事件生成器来进行点击可能就足够了。

用于记录和发送事件的私人调用是 GraphicServices 的一部分/GSEvent.h 与标准使用免责声明由您自行承担风险。每个 UIEvent 实际上都是一个 UIInternalEvent,它具有对 __GSEvent 的引用,因此为了记录,您可以使用 _gsEvent 属性来获取底层事件。

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

我没有使用过任何这些东西,但看起来 GSSendSystemEvent 将是一个很好的起点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文