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.Verify Color in Selenium WebDriver

  • 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’.
  • 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.
  • 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.
  • After performing all above steps, your whole method should be similar to below code.

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
  1. Rahul
  2. Sam
    • Maharshi
  3. Binh
  4. Alekya
  5. Nipun
  6. chethan

Leave a Reply

Your email address will not be published. Required fields are marked *