How to test that staticTexts contains a string using XCTest
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:
// 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.
XCTAssertNotNil(myLabel.label.range(of:"expected part"))
Finding an element in UI Test with only substring Xcode
You can use an NSPredicate
to find static texts containing a partial word/phrase, using the containing(_:)
method on XCUIElementQuery
.
let predicate = NSPredicate(format: "label CONTAINS 'create account'")
let app = XCUIApplication()
let createAccountText = app.webViews.links.containing(predicate)
createAccountText.tap()
Xcode ui test: staticTexts start with
You can use a BEGINSWITH
predicate to check if an element starts with a prefix.
let app = XCUIApplication()
let faxPredicate = NSPredicate(format: "label BEGINSWITH 'Fax: '")
let faxLabel = app.staticTexts.element(matching: faxPredicate)
XCTAssert(faxLabel.exists)
Here's a working example for selecting elements with a different BEGINSWITH
predicate, a picker with multiple wheels.
How to print all the staticTexts in XCUITest
You can use something like that:
for staticText in app.staticTexts.allElementsBoundByIndex {
if staticText.label == "test" {
}
}
XCTestCase - how to assert on a NSTextView containing String?
Simply value
should do.
It's available on XCUIElementAttributes
and is of type Any?
that varies based on the type of the element.
XCTAssertEqual(prefs.textViews.firstMatch.value as! String,
"Enter text below")
Ref:
- https://developer.apple.com/documentation/xctest/xcuielementattributes
How can I locate an XCUIElement searching by partial label text in Swift?
You can find elements with a predicate. Use the containing(_ predicate: NSPredicate) -> XCUIElementQuery
method from XCUIElementQuery
.
let predicate = NSPredicate(format: "label CONTAINS[c] 'Item'")
let labels = XCUIApplication().staticTexts.containing(predicate)
Related Topics
Codable and Xmlparser in Swift
Using Foreach with a an Array of Bindings (Swiftui)
Avoid Consecutive "If Let" Declarations in Swift
How to Open Url in Safari and the Get Back to the App Under Uitests in Xcode 7
How in Swift to Convert Int16 to Two Uint8 Bytes
Detect Ethernet/Wifi Network Change
Get Rawvalue from Enum in a Generic Function
Performseguewithidentifier in Swift
How to Zip More Than 4 Publishers
Uisearchcontroller in Navigationitem iOS 11 Apple Way
Swiftui: Detect Finger Position on MAC Trackpad
Forcing Nspersistentcontainer Change Core Data
Realitykit - Set Text Programmatically of an Entity of Reality Composer
One-Way Platform Collisions in Sprite Kit
How to Highlight a Uitextview's Text Line by Line in Swift
How to Override Layerclass in Swift