【问题标题】:From an XCUITest how can I check the on/off state of a UISwitch?从 XCUITest 如何检查 UISwitch 的开/关状态?
【发布时间】:2017-10-28 14:38:54
【问题描述】:

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

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

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

【问题讨论】:

    标签: objective-c xcode-ui-testing uiswitch


    【解决方案1】:

    我在这篇博文中发现了与 Swift 语言类似的情况。 Xcode UITests: How to check if a UISwitch is on

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

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

    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) 测试开关的状态是打开还是关闭,然后切换其状态

    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),我能够在测试用例运行之间强制所有 UISwitches 使用默认状态,并避免状态恢复干扰。

    【讨论】:

    • @JohnAlexanderBetts 我没有使用任何第三方库。这都是开箱即用的 Xcode 东西。
    • 很长一段时间我试图通过 app.buttons 访问开关,但它实际上是 app.switches。
    【解决方案2】:

    Swift 5 版本:

    XCTAssert((activationSwitch.value as? String) == "1")
    

    您也可以使用XCUIElement 扩展名

    import XCTest
    
    extension XCUIElement {
        var isOn: Bool? {
            return (self.value as? String).map { $0 == "1" }
        }
    }
    
    // ...
    
    XCTAssert(activationSwitch.isOn == true)
    
    

    【讨论】:

    • 您知道如何为 ios 12 执行此操作吗?从我在模拟器上看到的情况来看,switch 元素的值似乎总是为零。我还没有找到解决方法,请参阅forums.developer.apple.com/thread/124763
    【解决方案3】:

    对于斯威夫特

    XCUIElement添加扩展断言直接切换isOn状态。

    extension XCUIElement {
        
        func assert(isOn: Bool) {
            guard let intValue = value as? String else {
                return XCTAssert(false, "The value of element could not cast to String")
            }
            
            XCTAssertEqual(intValue, isOn ? "1" : "0")
        }
    }
    

    用法

    yourSwitch.assert(isOn: true)
    

    【讨论】:

      【解决方案4】:

      斯威夫特 5: 不确定这是否对任何人有用,但我刚刚开始使用 XCTest,并且基于@drshock 对这个问题的回复,我创建了一个简单的函数,我添加到我的 XCTestCase 中,该函数仅在关闭时打开开关。

          let app = XCUIApplication()
      
          func turnSwitchOnIfOff(id: String) {
      
              let myControl : NSString = app.switches.element(matching: .switch, identifier: id).value as! NSString
      
              if myControl == "0" {
      
                  app.switches.element(matching: .switch, identifier: id).tap()
      
              }
      
          }
      

      然后在我的测试中,当我想打开一个开关是否关闭时,我使用它,其中 id 是开关可访问性部分中的标识符字符串。

          turnSwitchOnIfOff(id: "accessibilityIdentifierString")
      

      【讨论】:

        【解决方案5】:

        定义

        extension XCUIElement {
            var isOn: Bool {
                (value as? String) == "1"
            }
        }
        

        然后

        XCAssertTrue(someSwitch.isOn)
        

        【讨论】:

          猜你喜欢
          • 2013-10-17
          • 2018-04-04
          • 1970-01-01
          • 1970-01-01
          • 2011-04-12
          • 2020-02-29
          • 2021-10-26
          • 2011-03-19
          • 1970-01-01
          相关资源
          最近更新 更多