Uitest Color of a Label (Not UI Label)

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:

  1. Snapshot Tests:

     import SnapshotTesting
    ...
    func test_view_matchesSnapshot() {
    let view = makeYourView()
    assertSnapshot(matching: view, as: .wait(for: 1.5, on: .image))
    }
  2. 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 UILabels, the text is available from the label property.

let app = XCUIApplication()
let lastLabel = app.staticTexts["lastLabel"]
XCTAssertEqual(lastLabel.label, "Desired text")


Related Topics



Leave a reply



Submit