使用 Xcode 7 UITest 测试元素是否可见
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33247116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Testing if an element is visible with Xcode 7 UITest
提问by MatterGoal
I want to verify if an element is visible or not depending on its .hidden
property but I don't find a valid way to do that using the new Xcode 7 UI test stuff.
我想根据元素的.hidden
属性来验证元素是否可见,但我没有找到使用新的 Xcode 7 UI 测试内容的有效方法。
I've tried with myelement.exists
and myelement.hittable
but they doesn't seems to work as I expected. I suppose they work on conjunction with the hidden
property. An hidden element shouldn't exists and is not hittable... but this is not the current behaviour (I can understand the exists
behaviour... but a hidden element should be not hittable IMO).
我已经尝试过myelement.exists
,myelement.hittable
但它们似乎没有按我预期的那样工作。我想他们与hidden
物业一起工作。隐藏元素不应该存在并且不可命中……但这不是当前的行为(我可以理解这种exists
行为……但隐藏元素不应该是可命中的 IMO)。
Is there another way to verify the "hidden" property value?
还有另一种方法来验证“隐藏”的属性值吗?
回答by Joe Masilotti
As of Xcode 7.1 Beta 3, UI Testing does not currently support validating the visibility of an element. I suggest filing a radar to bring the necessary attention to Apple.
从 Xcode 7.1 Beta 3 开始,UI 测试当前不支持验证元素的可见性。我建议提交雷达以引起对 Apple 的必要关注。
Xcode 7.1 has fixed this issue. hittable
now checks to see if the element is correct.
Xcode 7.1 已修复此问题。hittable
现在检查元素是否正确。
回答by Ohmy
1) I am testing the UI with swift in Xcode 7.3 and I using both .hittable and .exists for testing whether a label is hidden or not and they both work. I test for 'true' and 'false' to make sure either way agree with the result.
1) 我正在 Xcode 7.3 中使用 swift 测试 UI,我同时使用 .hittable 和 .exists 来测试标签是否隐藏,它们都可以工作。我测试“真”和“假”以确保任何一种方式都与结果一致。
I have a label whose static text is "Track Info" and set to be hidden when app is first loaded, then later on I press a button to show the label, and here is the result after the label is shown.
我有一个标签,其静态文本为“Track Info”,并在首次加载应用程序时设置为隐藏,然后我按下按钮显示标签,这是显示标签后的结果。
// test fails
// 测试失败
let trackInfoLabel = app.staticTexts["Track info"]
XCTAssertEqual(trackInfoLabel.exists, true)
XCTAssertEqual(trackInfoLabel.hittable, true)
// test passes
// 测试通过
XCTAssertEqual(trackInfoLabel.exists, false)
XCTAssertEqual(trackInfoLabel.hittable, false)
// test passes
// 测试通过
let trackInfoLabel = app.staticTexts["Track Info"]
XCTAssertEqual(trackInfoLabel.exists, true)
XCTAssertEqual(trackInfoLabel.hittable, true)
// test fails
// 测试失败
XCTAssertEqual(trackInfoLabel.exists, false)
XCTAssertEqual(trackInfoLabel.hittable, false)
Leter on when I press the button to hide the label, all the results turned opposite. This confirms that both properties (hittable and exists) works for label.hidden setting.
当我按下按钮隐藏标签时,所有结果都相反。这证实了两个属性(可命中和存在)都适用于 label.hidden 设置。
2) Another way to find out if an element is hidden, you can do is element.frame.size.width == 0 || element.frame.size.height == 0
2)另一种找出元素是否隐藏的方法,你可以做的是 element.frame.size.width == 0 || element.frame.size.height == 0
回答by Andrew Z
XCUIElement.hittable works for me (in my particular test case where I am checking several UIButton elements for visibility) - quite sure it is not a right way to do it though
XCUIElement.hittable 对我有用(在我的特定测试用例中,我正在检查几个 UIButton 元素的可见性)-尽管很确定这不是正确的方法
回答by alex1704
Next code worked for me. At the end of the test you can past code as follow:
下一个代码对我有用。在测试结束时,您可以通过以下代码:
while ([app.staticTexts matchingIdentifier:@"accesibilityId"].count > 0) {
sleep(1);
}
回答by da-na
I agree hittable doesn't always work for buttons (Swift 2.0, XCode 7.2)
我同意 hittable 并不总是适用于按钮(Swift 2.0,XCode 7.2)
I just discovered that if button is visible, you can find it ONLY among buttons, while if button is hidden, you can find it's tag in staticTexts as well!
我刚刚发现如果按钮是可见的,你只能在按钮中找到它,而如果按钮是隐藏的,你也可以在 staticTexts 中找到它的标签!
XCTAssertFalse(app.buttons["Log out"].exists && app.staticTexts["Log out"].exists) // This button is visible (hidden = false)
XCTAssert(app.buttons["Log in"].exists && app.staticTexts["Log in"].exists) // This one is hidden
Hacky, but works for buttons. Apple should just introduce .hidden
or .visible
along .hittable
and .exists
Hacky,但适用于按钮。苹果应该只是引进.hidden
或.visible
沿.hittable
和.exists
回答by Randja
My solution is to add accessibilityIdentifier dynamicly
我的解决方案是动态添加accessibilityIdentifier
func someMethod() {
label.isHidden = true
label. accessibilityIdentifier = "isHidden"
}
func someOtherMethod {
label.isHidden = false
label. accessibilityIdentifier = "isVisible"
}
and the in UITest you can use it as
在 UITest 中,您可以将其用作
if app.staticTexts["MyLabel"].identifier == "isHidden" {
dosomething()
}