In XCTest UI Testing, how to check the background color of button, label , views?
Asked Answered
R

1

10

I am trying to validate if the background color can be obtained in XCTest UI Testing, I am looking to compare the background color with the set value, so that i don't have to rely on image comparison

Remuneration answered 29/7, 2016 at 21:1 Comment(0)
W
13

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.

Whited answered 30/7, 2016 at 17:31 Comment(6)
Say on the tap of a button i change the color of a view, can this be validated with XCTest Unit tests?Remuneration
You can validate that the background color is changed when the @IBAction method is called.Whited
but in unit testing you cannot automatically tap for the action to be triggered , there is no record mode available.Remuneration
@abhisheksaatal Oletha meant you have to instantiate your view controller on an Unit Test, call method that must change the button's background and check it through XCTAssertTrue(button.backgroundColor == UIColor.redColor()) for example.Evangelia
Yes, though I would advise using XCTAssertEqual for better error messaging in this scenario.Whited
Yes though i accept that it is one of the way for doing it, but it will be too hectic to compare color of each and every UIView and assert on it, it would require lot of time to do so, however image comparison can be done easily. XCTest UITesting does not provide a way to do it, XCTest unit test will require a lot of effort to do it.Remuneration

© 2022 - 2024 — McMap. All rights reserved.