如何测试staticTexts包含使用XCTest的字符串

时间:2021-04-08 19:03:19

In Xcode UI testing, how do I test that staticTexts contains a string?

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

In the debugger, I can run something like this to print out all the content of staticTexts: po app.staticTexts . But how do I test if a string exists anywhere within all of that content?

在调试器中,我可以运行这样的东西来打印出staticTexts的所有内容:po app.staticTexts。但是,我如何测试字符串是否存在于所有内容中的任何位置?

I can check for the existence of each staticText doing something like app.staticTexts["the content of the staticText"].exists? but I have to use the exact content of that staticText. How can I use only a string which may be only a part of that content?

我可以检查每个staticText是否存在像app.staticTexts这样的东西[“staticText的内容”]。存在?但我必须使用那个staticText的确切内容。如何只使用可能只是该内容一部分的字符串?

3 个解决方案

#1


2  

First, you need to set an accessibility identifier for the static text object you want to access. This will allow you to find it without searching for the string it is displaying.

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

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

Then you can assert whether the string displayed is the string you want by writing a test by calling .label on the XCUIElement to get the contents of the displayed string:

然后,您可以通过在XCUIElement上调用.label来获取所显示字符串的内容来断言显示的字符串是否是您想要的字符串:

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

To check it contains a certain string, use range(of:), which will return nil if the string you give is not found.

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

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

#2


8  

You can use NSPredicate to filter elements.

您可以使用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
  }

With CONTAINS[c] you specify that the search is case insensitive.

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

Have a look at Apples Predicate Programming Guide

看看Apples谓词编程指南

#3


1  

I had this problem while I was building my XCTest, I had a dynamic string inside of my block of text I should verify. I had built this two functions to solve my problem:

我在构建我的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)
}

I know this post is old, but I hope this can help someone.

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

#1


2  

First, you need to set an accessibility identifier for the static text object you want to access. This will allow you to find it without searching for the string it is displaying.

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

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

Then you can assert whether the string displayed is the string you want by writing a test by calling .label on the XCUIElement to get the contents of the displayed string:

然后,您可以通过在XCUIElement上调用.label来获取所显示字符串的内容来断言显示的字符串是否是您想要的字符串:

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

To check it contains a certain string, use range(of:), which will return nil if the string you give is not found.

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

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

#2


8  

You can use NSPredicate to filter elements.

您可以使用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
  }

With CONTAINS[c] you specify that the search is case insensitive.

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

Have a look at Apples Predicate Programming Guide

看看Apples谓词编程指南

#3


1  

I had this problem while I was building my XCTest, I had a dynamic string inside of my block of text I should verify. I had built this two functions to solve my problem:

我在构建我的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)
}

I know this post is old, but I hope this can help someone.

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