The Story:
In several places in our test codebase we assert different CSS values to be equal to expected values. Usually, this is a color
, background-color
, font
related style properties, or cursor
. This question is about dealing with colors.
Here is an example working test that currently passes:
describe("AngularJS home page", function () {
beforeEach(function () {
browser.get("https://angularjs.org/");
});
it("should show a blue Download button", function () {
var downloadButton = element(by.partialLinkText("Download"));
expect(downloadButton.getCssValue("background-color")).toEqual("rgba(0, 116, 204, 1)");
});
});
It checks that the Download button on the AngularJS website has 0, 116, 204, 1
RGBA value.
Now, if the color would change, the test would fail as, for example:
Expected 'rgba(0, 116, 204, 1)' to equal 'rgba(255, 116, 204, 1)'.
The Problems:
As you can see, first of all, the expectation itself is not quite readable. Unless we put a comment near it, it is not obvious what color are we expecting to see.
Also, the error message is not informative. It is unclear what an actual color is and what color are we expecting to see.
The Question:
Is it possible to improve the test itself and the error message to be more readable and informative and operate color names instead of color RGB/RGBA values?
Desired expectation:
expect(downloadButton).toHaveBackgroundColor("midnight blue");
Desired error messages:
Expect 'blue' to equal 'black'
Expect 'dark grey' to equal 'light sea green'
Currently, I'm thinking about making a custom jasmine matcher that would convert the RGB/RGBA
value to a custom Color
object that would keep the original value as well as determine the closest color. color
npm package looks very promising.
Would appreciate any hints.