Road to verify css properties value using Webdriver

To get css values of any locator, we will create java script function with the use of “getDefaultComputedStyle”function. We will execute java script function using webdriver and fetch css properties value.
getDefaultComputedStyle () gives  default computed value of all css properties of an element.
Here I have created a sample example in which I am verifying Google home page menu bar css properties color, height and width.

package com.test;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class WebdriverCSSValue {

   private WebDriver driver;
   private String baseUrl;

   public void setUp() throws Exception {
         driver = new FirefoxDriver();                    
         baseUrl = "";
         driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

   public void testCSSvalueVerifying() {
         driver.get(baseUrl + "/");
         // js scripts
         String js = "return window.document.defaultView.getComputedStyle(" +
         String jsColor = js+".getPropertyValue('color')";
         String jsHeight = js+".getPropertyValue('height')";
         String jswidth = js+".getPropertyValue('width')";

         // execution of js scripts to fetch css values
         JavascriptExecutor jsexecuter = (JavascriptExecutor) driver;
         String color = (String) jsexecuter.executeScript(jsColor);
         String height = (String) jsexecuter.executeScript(jsHeight);
         String width = (String) jsexecuter.executeScript(jswidth);
         Assert.assertTrue(color.equals("rgb(34, 34, 34)"));
         // print css values
   public void tearDown() throws Exception {

1 comment:

  1. @ String jsColor = js+".getPropertyValue('color')";

    I get error as Not yet implemented. Why so?


Leave your comments, queries, suggestion I will try to provide solution