Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium WebDriver get border color

Hi all i am trying to get border color of an extjs 4.2 form control text field using getCssValue method. But i am not able to fetch it. it is returning me blank. Below is my code snippet u can try this as is.

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestClass 
{
    public static void main(String[] args) throws InterruptedException
    {
        WebDriver driver=new FirefoxDriver();
        Thread.sleep(2000);
        driver.get("http://docs.sencha.com/extjs/4.2.1/extjs-build/examples/form/dynamic.html");
        Thread.sleep(2000);
        WebElement element=driver.findElement(By.xpath(".//input[@name='first']"));
        Thread.sleep(2000);
        element.sendKeys("");
        element.sendKeys(Keys.TAB);
        Thread.sleep(2000);
        System.out.println("'"+element.getCssValue("border-color")+"'");
    }
}

Field, Xpath and CSS attribute highlighted in black border

Webdriver version 2.33 (Java binding)

FF 22

like image 434
Mrunal Gosar Avatar asked Dec 12 '13 04:12

Mrunal Gosar


1 Answers

enter image description here How to get border color or other css values look in Computed there are all values that you can get:

getCssValue("border-bottom-color")

returns rgba(209, 219, 223, 1) and need to clear it (this will work for rgba and rgb):

String rgb[] = driver.findElement(By.name("login[email]")).getCssValue("border-bottom-color").replaceAll("(rgba)|(rgb)|(\\()|(\\s)|(\\))","").split(",");

Now our rgb is in array using this method to parse it

String hex = String.format("#%s%s%s", toBrowserHexValue(Integer.parseInt(rgb[0])), toBrowserHexValue(Integer.parseInt(rgb[1])), toBrowserHexValue(Integer.parseInt(rgb[2])));

private static String toBrowserHexValue(int number) {
        StringBuilder builder = new StringBuilder(Integer.toHexString(number & 0xff));
        while (builder.length() < 2) {
            builder.append("0");
        }
        return builder.toString().toUpperCase();
    }

From this rgba(209, 219, 223, 1) we got this #D1DBDF

P.S. Source of parsing int rgb to hex

like image 115
Andrian Durlestean Avatar answered Oct 10 '22 01:10

Andrian Durlestean