简体   繁体   English

如何检查 iOS gui 自动化中的元素属性?

[英]How to check element properties in iOS gui automation?

All UI Automation examples I've seen uses standard components whose state can be inspected with the JavaScript API using the value() method.我见过的所有 UI 自动化示例都使用标准组件,其 state 可以使用value()方法使用 JavaScript API 检查。 This is a bit limiting.这有点限制。 Lets say you want to check the color or alpha value and whatnot.假设您要检查颜色或 alpha 值等等。

How can I inspect the properties of a view?如何检查视图的属性?

An example: a tap on a certain element should make it "selected".一个例子:点击某个元素应该使其“被选中”。 I'd like to perform a tap on it and then verify that isSelected is TRUE.我想对其进行点击,然后验证 isSelected 是否为 TRUE。

Update:更新:

I found the withPredicate() method which should do it in theory, except it seems to only trigger on name properties:我发现理论上应该这样做的withPredicate()方法,除了它似乎只在name属性上触发:

element.withPredicate("isSelected == YES")          // always fails
element.withPredicate("name matches 'my element'")  // works

I ended up with this approach which works for my purposes:我最终采用了这种适用于我的目的的方法:

Let UIView.accessibilityValue return a JSON string with relevant properties:让 UIView.accessibilityValue 返回具有相关属性的 JSON 字符串:

- (NSString *)accessibilityValue
{
    return [NSString stringWithFormat:
            @"{'alpha':%f, 'isSelected':%@}", 
            self.alpha, self.isSelected ? @"true" : @"false"];
}

Then use eval() in the test code and check those properties.然后在测试代码中使用 eval() 并检查这些属性。 value() is shorthand for calling accessibilityValue: value() 是调用accessibilityValue 的简写:

var props = eval("(" + element.value() + ")");

if (props.isSelected) {
    UIALogger.logFail("Should not be selected");
}

UIATarget.localTarget().tap({"x":471, "y":337});

var props = eval("(" + element.value() + ")");

if (!props.isSelected) {
    UIALogger.logFail("Should be selected");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM