在 Xcode Instruments 的 Automation 模板中读取 UILabel 的值

Read the value of a UILabel in Automation template of Instruments in Xcode

我编写了以下脚本来点击 UI 上的按钮。每按一次都会增加 UILabel 的整数值。我想在我的脚本中读取 UILabel 的值。我怎样才能做到这一点?我是 Instruments 中这个自动化工具的新手。

var target = UIATarget.localTarget();
var appName = target.frontMostApp().mainWindow().name;
target.setDeviceOrientation(UIA_DEVICE_ORIENTATION_PORTRAIT);
target.frontMostApp().mainWindow().buttons()["Increment"].tap();

首先确保您为该 UILabel 分配了可访问性标识符。

您可以通过代码做到这一点:

self.myLabel.accessibilityIdentifier = @"uniqueId";

或通过故事板:

要获取标签值,请在 UIAutomation 脚本中使用以下行:

target.frontMostApp().mainWindow().staticTexts()["uniqueId"].value();