防止重新评估 XCUIElementQuery [英] Prevent XCUIElementQuery from being reevaluated

查看:25
本文介绍了防止重新评估 XCUIElementQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WWDC Session 中关于 Xcode 中的 UI 测试,您可以了解到当您实际合成属性或读取值时会评估查询.

In the WWDC Session about UI Testing in Xcode you can learn that queries are evaluated when you actually synthesise properties or read values.

那么什么时候评估查询?因此,它们实际上并不会在您创建它们时进行评估.

So when are queries evaluated? So they are not actually evaluated just when you create them.

它们是按需或根据需要进行评估的.这意味着对于元素,当您合成事件或读取属性值时,将评估查询.

They are evaluated on-demand or as they are needed. This means that with an element, the query will be evaluated when you synthesize events or read property values.

您可以创建元素,但在您使用它之前,不会评估查询.类似地,如果您直接创建查询,则会在您获得匹配项数或调用返回所有匹配项的 API 之一时对其进行评估.届时必须对其进行评估,我们将在 UI 更改时重新评估查询.因此,您始终使用应用程序的最新视图,而不是十秒前或两分钟前的数据,具体取决于测试的长度.因此,通过这种方式,您可以认为查询和元素与 URL 有点相似.

You can create the element but until you use it, the query won't be evaluated. Similarly if you create a query directly it will be evaluated when you get the number of matches or if you call one of the APIs that returns all the matches. It will have to be evaluated at that point and we will reevaluate queries when the UI changes. So you are always working with the most current view of the application rather than data from ten seconds ago or two minutes ago, depending on the length of your test. So in this way you can think of queries and elements being somewhat similar to URLs.

这很好,可以始终确保您使用的是 UI 的最新状态,但它可能会非常慢,并且会阻止您执行需要高速的测试.请参阅以下内容:

This is great to always be sure you're working with the latest state of the UI but it can be quite slow and prevent you from executing tests that need high speed. See the following:

CFTimeInterval startTime = CACurrentMediaTime();
XCUIElement *button = [[XCUIApplication alloc] init]/*@START_MENU_TOKEN@*/.scrollViews/*[[".windows[@\"\\n[FYB Debug]: Reachability Flag Status: -R t------ networkStatus\"].scrollViews",".scrollViews"],[[[-1,1],[-1,0]]],[0]]@END_MENU_TOKEN@*/.otherElements.buttons[@"StartSDK_Button"];
[button tap];
[button tap];
[button tap];
[button tap];
[button tap];
CFTimeInterval endTime = CACurrentMediaTime();

NSLog(@"EXECUTION TIME = %f", endTime - startTime);

2017-04-20 11:04:03.381102+0800 XCTRunner[6368:854885] EXECUTION TIME = 5.619870

所以我想知道,有没有办法在测试期间暂时改变这种行为并且只查询一次元素?

So I was wondering, is there a way to change that behaviour temporarily during the tests and query the element only once?

推荐答案

我遇到了类似的问题,需要在表格视图上快速垂直滑动.没有找到使用 XCTest 框架加速事件的方法,我最终构建了一个相当棘手的解决方法:我没有通过 XCTest 框架请求点击/滑动手势,而是向定制开发的能够控制鼠标的 mac 应用程序发送请求主机的指针.如果您仅在模拟器上进行测试,它将非常好用且易于使用.

I had a similar problem needing a very fast vertical swipe on a table view. Not finding a way to speed up events using the XCTest framework I ended up building a rather tricky workaround: instead of requesting the tap/swipe gestures via XCTest framework I'm sending requests to a customly developed mac app that is able to control the mouse pointer of the hosting machine. If you're testing on simulator only it will work just great and easy to use.

重复点击 XCUIElement 的示例测试

A sample test that will repeatedly tap an XCUIElement

func testMultipleTap() {
    let app = XCUIApplication()
    app.launch()

    host.connect()

    let btn = app.buttons["mybutton"]

    let mouseClick = SBTUITunneledHostMouseClick(element: btn, completionPause: 0.05)

    let mouseCliks = Array(repeating: mouseClick, count: 3)
    host.execute(mouseCliks)
}

host 是允许您与 mac 应用程序通信的对象.

host is the object that allows you to communicate with the mac application.

该项目可在 GitHub 上找到.

The project is available on GitHub.

这篇关于防止重新评估 XCUIElementQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆