UITest color of a label (not UI label)
I'm afraid you're not going to be able to do this via UI Testing. UI Testing can "see" the interface only to the extent that Accessibility exposes it. Thus you can "see" the text but you cannot "see" that this thing is a UILabel; UI Testing knows nothing of that. Thus you cannot explore its nature as a UILabel.
How to check background color of the XCUIElement in UI test cases??
Like guys replied in comments, we can't do it by UI tests, but I think we still have 2 ways to achieve this:
Snapshot Tests:
import SnapshotTesting
...
func test_view_matchesSnapshot() {
let view = makeYourView()
assertSnapshot(matching: view, as: .wait(for: 1.5, on: .image))
}Unit Tests:
func test_view_shouldHaveTextFieldWithExpectedBackgroundColor() throws {
let view = makeYourView()
let textFieldBackgroundColor = try XCTUnwrap(
view.textField.backgroundColor,
"textField.backgroundColor is nil"
)
let expectedTextFieldBackgroundColor = UIColor.white
XCTAssertEqual(
textFieldBackgroundColor.toHexString(),
expectedTextFieldBackgroundColor.toHexString(),
"textField doesn't have expected backgroundColor"
)
}
*Utility hex getters to compare two UIColors can be found here
**If your textField is private, then you can use
this great solution and our unit test should look like this:
func test_view_shouldHaveTextFieldWithExpectedBackgroundColor() throws {
let view = makeYourView()
let textField = try XCTUnwrap(
YourViewMirror(reflecting: view).textField,
"Can't find textField property"
)
let textFieldBackgroundColor = try XCTUnwrap(
textField.backgroundColor,
"textField.backgroundColor is nil"
)
let expectedTextFieldBackgroundColor = UIColor.white
XCTAssertEqual(
textFieldBackgroundColor.toHexString(),
expectedTextFieldBackgroundColor.toHexString(),
"textField doesn't have expected backgroundColor"
)
}
where
final class YourViewMirror: MirrorObject {
init(reflecting yourView: YourView) {
super.init(reflecting: yourView)
}
var textField: UITextField? {
extract()
}
}
and
class MirrorObject {
let mirror: Mirror
init(reflecting: Any) {
self.mirror = Mirror(reflecting: reflecting)
}
func extract<T>(variableName: StaticString = #function) -> T? {
return mirror.descendant("\(variableName)") as? T
}
}
In XCTest UI Testing, how to check the background color of button, label , views?
XCTest is for functional testing, rather than asserting visual requirements.
To test requirements such as background color, use a unit test to initialize the view controller in question and check the view's background color there. You don't need to rely on image comparison, and unit tests are a lot faster.
XCTest to text for UILabel text existance
For UILabel
s, the text is available from the label
property.
let app = XCUIApplication()
let lastLabel = app.staticTexts["lastLabel"]
XCTAssertEqual(lastLabel.label, "Desired text")
Related Topics
Swiftui Present View Modally via Tabview
Issue with Encoding "&" in Urls
What Is the Purpose of Firebase Appcheck
How to Make a Rounded Oval Button
Uitapgesturerecognizer Called Immediately
How to Change a View from Portrait Mode to Landscape Mode and Lock It
Creating a Rtsp Client for Live Audio and Video Broadcasting in Objective C
Allow Users to Send Messages to Multiple Users Simultaneously in a Messaging App
Why Does It Take Such a Long Time for UI to Be Updated from Background Thread
How Does Uibutton Addtarget Self Work
How to Set the Collection View Cell Size Exactly Equal to the Collection View in iOS
Can't Set Calayer's Border Color
Keep a View Always on Top (Don't Scroll with Keyboard) in Iqkeyboardmanager
Attempting to Segue from Objective-C to Swift Vc
How to Fix Uilabel Text Spacing
Horizontal Paging Uicollectionview with Automatic Item Size in a Vertical Stack View