Verify Color in Selenium WebDriver
If automation requirements in your project wants you to verify colors of web page elements, you have a fix for it in Selenium WebDriver. As we will see in this post, you can actually compare and verify color in Selenium using simple code. For our example, we will take Java as a language of choice with Selenium WebDriver. So without any further ado, let’s check out how to verify color in Selenium WebDriver.
For purpose of our example, we will verify color of Products link present in About Google page. Below image shows this link.
- First of all, we have to get a value of link color using getCssValue method. It can be done by using below code. In the code, Products link’s CSS attribute ‘color’ is stored in a String variable called ‘color’.
1String color = driver.findElement(By.xpath("//a[@href='products/']")).getCssValue("color"); - The above code will return value in RGB format such as “rgba(36, 93, 193, 1)”. We will convert it into more convenient Hex code using Java. Use below code for it.
123456789String[] hexValue = color.replace("rgba(", "").replace(")", "").split(",");int hexValue1=Integer.parseInt(hexValue[0]);hexValue[1] = hexValue[1].trim();int hexValue2=Integer.parseInt(hexValue[1]);hexValue[2] = hexValue[2].trim();int hexValue3=Integer.parseInt(hexValue[2]);String actualColor = String.format("#%02x%02x%02x", hexValue1, hexValue2, hexValue3); - That’s it. You will get a value of color in Hex code after above code is executed. We can add an Assert statement to verify that the color is matching with the expected color.
1Assert.assertEquals("#245dc1", actualColor); - After performing all above steps, your whole method should be similar to below code.
123456789101112131415public void GoogleAbout() throws Exception{String color = driver.findElement(By.xpath("//a[@href='products/']")).getCssValue("color");String[] hexValue = color.replace("rgba(", "").replace(")", "").split(",");int hexValue1=Integer.parseInt(hexValue[0]);hexValue[1] = hexValue[1].trim();int hexValue2=Integer.parseInt(hexValue[1]);hexValue[2] = hexValue[2].trim();int hexValue3=Integer.parseInt(hexValue[2]);String actualColor = String.format("#%02x%02x%02x", hexValue1, hexValue2, hexValue3);Assert.assertEquals("#245dc1", actualColor);}
We hope you have found the above tutorial to verify color in Selenium WebDriver helpful. We would love to hear your feedback in comments.
Comments
This is perfect. Thanks a lot
Happy to help. 🙂
Hey can u help me.
When I try to validate two colors ho does that work ?
If I understand correctly, you want to verify colors of different web page elements. If that’s the case, you can reuse the above method for detecting color.
Many thanks to Maharshi !
Every thing is fine but how will know the expected hex value in assert statement
Thanks, Great Soluition
These lines
String[] hexValue = color.replace(“rgba(“, “”).replace(“)”, “”).split(“,”);
int hexValue1=Integer.parseInt(hexValue[0]);
hexValue[1] = hexValue[1].trim();
int hexValue2=Integer.parseInt(hexValue[1]);
hexValue[2] = hexValue[2].trim();
int hexValue3=Integer.parseInt(hexValue[2]);
can be further refactored as below to get the array in int .
return Arrays.stream(color.substring(color.indexOf(“(“) + 1, color.indexOf(“)”)).split(“,”))
.map(String::trim).mapToInt(Integer::parseInt).toArray();
Why is this rgba replacing taking place? Why not simply check rgb color is the same as what is being found on the site?