从 XCUITest 如何检查 UISwitch 的开/关状态? [英] From an XCUITest how can I check the on/off state of a UISwitch?

查看:92
本文介绍了从 XCUITest 如何检查 UISwitch 的开/关状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了一种情况,我需要能够从现有的 XCUITest 存储桶(而不是 XCTest)中检查 UISwitch 的当前开/关状态(不是它是否为用户交互启用),并切换使其达到预定状态.我已将应用程序状态恢复添加到旧的现有应用程序,现在这会干扰运行之间的许多现有测试用例,这些测试用例预期 UISwitches 处于特定默认状态.

I recently ran into a situation where I needed to be able to check the current on/off state (NOT whether it is enabled for user interaction) of a UISwitch from within an existing bucket of XCUITests, not an XCTest, and toggle it to a pre-determined state. I had added app state restoration to an old existing app and this is now interfering with a number of existing testcases between runs that expected the UISwitches in particular default states.

与 XCTest 不同,在 XCUITest 中,您无法直接访问 UISwitch 状态.

Unlike XCTest, within XCUITest you do not have access to the UISwitch state directly.

在 Objective-C 中如何为 XCUITest 确定此状态?

How is this state determined in Objective-C for an XCUITest?

推荐答案

未能在任何明显的地方找到这一点,我在这篇博文中偶然发现了 Swift 语言的类似情况.Xcode UITests:如何检查如果 UISwitch 打开

Failing to find this anywhere obvious I happened upon this blog post for a similar situation for the Swift language. Xcode UITests: How to check if a UISwitch is on

根据这些信息,我测试并验证了两种解决问题的方法.

With this information I tested and verified two approaches to solve my problem.

1) 断言状态是打开还是关闭

1) To assert if the state is on or off

XCUIElement *mySwitch = app.switches[@"My Switch Storyboard Accessibility Label"];
// cast .value to (NSString *) and test for @"0" if off state 
XCTAssertEqualObjects((NSString *)mySwitch.value, @"0", @"Switch should be off by default.");  // use @"1" to test for on state

2) 测试开关的状态是打开还是关闭,然后切换其状态

2) To test if the state of the switch is on or off then toggle its state

XCUIElement *mySwitch = app.switches[@"My Switch Storyboard Accessibility Label"];
// cast .value to (NSString *) and test for @"0" if off state 
if (![(NSString *)mySwitch.value isEqualToString:@"0"])
        [mySwitch tap];  // tap if off if it is on

使用方法 (2),我能够在测试用例运行之间强制所有 UISwitch 的默认状态并避免状态恢复干扰.

Using approach (2), I was able to force a default state for all UISwitches in between testcase runs and avoid the state restoration interference.

这篇关于从 XCUITest 如何检查 UISwitch 的开/关状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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