【问题标题】:How to test that staticTexts contains a string using XCTest如何使用 XCTest 测试 staticTexts 是否包含字符串
【发布时间】:2016-10-28 21:38:35
【问题描述】:

在 Xcode UI 测试中,如何测试 staticTexts 是否包含字符串?

在调试器中,我可以运行类似这样的操作来打印出 staticTexts 的所有内容:po app.staticTexts。但是如何测试所有内容中是否存在字符串?

我可以检查每个 staticText 的存在,比如app.staticTexts["the content of the staticText"].exists?但我必须使用该静态文本的确切内容。如何仅使用可能只是该内容一部分的字符串?

【问题讨论】:

    标签: swift xctest


    【解决方案1】:

    您可以创建一个扩展来简单地在XCUIElement 上使用它。

    extension XCUIElement {
        
        func assertContains(text: String) {
            let predicate = NSPredicate(format: "label CONTAINS[c] %@", text)
            let elementQuery = staticTexts.containing(predicate)
            XCTAssertTrue(elementQuery.count > 0)
        }
    }
    

    用法:

    // Find the label
    let yourLabel = app.staticTexts["AccessibilityIdentifierOfYourLabel"].firstMatch
    
    // assert that contains value
    yourLabel.assertContains(text: "a part of content of the staticText")
    
    

    【讨论】:

      【解决方案2】:

      我在构建 XCTest 时遇到了这个问题,我的文本块中有一个动态字符串,我应该验证它。我已经构建了这两个函数来解决我的问题:

      func waitElement(element: Any, timeout: TimeInterval = 100.0) {
          let exists = NSPredicate(format: "exists == 1")
      
          expectation(for: exists, evaluatedWith: element, handler: nil)
          waitForExpectations(timeout: timeout, handler: nil)
      }
      
      func waitMessage(message: String) {
          let predicate = NSPredicate(format: "label CONTAINS[c] %@", message)
          let result = app.staticTexts.containing(predicate)
          let element = XCUIApplication().staticTexts[result.element.label]
          waitElement(element: element)
      }
      

      我知道这篇文章很旧,但我希望这可以帮助某人。

      【讨论】:

      • 您首先使用app 然后使用XCUIApplication() 创建一个新实例是否有特定原因?
      【解决方案3】:

      您可以使用NSPredicate 过滤元素。

        let searchText = "the content of the staticText"
        let predicate = NSPredicate(format: "label CONTAINS[c] %@", searchText)
        let elementQuery = app.staticTexts.containing(predicate)
        if elementQuery.count > 0 {
          // the element exists
        }
      

      使用CONTAINS[c],您可以指定搜索不区分大小写。

      看看苹果Predicate Programming Guide

      【讨论】:

        【解决方案4】:

        首先,您需要为要访问的静态文本对象设置一个可访问性标识符。这将允许您在不搜索它显示的字符串的情况下找到它。

        // Your app code
        label.accessibilityIdentifier = "myLabel"
        

        然后你可以通过在XCUIElement上调用.label写一个测试来断言显示的字符串是否是你想要的字符串来获取显示字符串的内容:

        // Find the label
        let myLabel = app.staticTexts["myLabel"]
        // Check the string displayed on the label is correct
        XCTAssertEqual("Expected string", myLabel.label)
        

        要检查它是否包含某个字符串,请使用range(of:),如果找不到您提供的字符串,它将返回nil

        XCTAssertNotNil(myLabel.label.range(of:"expected part"))
        

        【讨论】:

        • XCTAssertNotNil(myLabel.range(of:"expected part")) --- 这会产生错误“XCUIElement has no member range”
        • @binsnoel 抱歉,我的回答中错过了 .label。立即尝试使用更新的 sn-p。
        • 如果您有两个要测试的标签怎么办?
        • 为每个标签设置不同的标识符
        猜你喜欢
        • 1970-01-01
        • 2017-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-27
        • 2011-02-11
        • 2012-02-18
        • 2014-07-23
        相关资源
        最近更新 更多