Thursday 9 February 2017

SELENIUM: How to read or get font Properties of WebElement



How to read or get  font Properties of Web Element:


Sometimes you need to read font properties like font size, font color, font family, font background color etc.. 
Using  getCssValue() method which is present in Webelement ,we can get the font properties.This method should be called on webelement only.


Syntax:
public String getCssValue()

Example:


package com.rameshsoft.rameshselenium;

import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class ReadCssProperties {
WebDriver driver = null; 
    @BeforeTest
    public void setup() throws Exception {
          System.setProperty("webdriver.gecko.driver", "C:\\Users\\ramesh\\Desktop\\java\\desk\\geckodriver-v0.11.1-win64\\geckodriver.exe"); 
  driver = new FirefoxDriver();
         driver.manage().window().maximize();
         driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
         driver.get("https://www.gmail.com");
    }

 @Test
 public void readFontProperty(){
  WebElement uname = driver.findElement(By.id("Email")); 
  String fontSize = uname.getCssValue("font-size");
  System.out.println("Font Size : "+fontSize);
  String fontColor = uname.getCssValue("color");
  System.out.println("Font Color :"+fontColor);
  String fontFamily = uname.getCssValue("font-family");
  System.out.println("Font Family : "+fontFamily);
  String fonttxtAlign = uname.getCssValue("text-align");
  System.out.println("Font Text Alignment:  "+fonttxtAlign);
  driver.quit();

 }
}

No comments:

Post a Comment