0

I am using a selenium JAVA code to print the color and background color. I am able to get RGBA and can convert to HEx..but is there a way i can actually print this color as RED ( which is what in the website) ...here is my example code..

public class FormElementsPractice {

    WebDriver driver ;
    @Before
    public void setUp() throws Exception {
        driver = utils.HelperFunctions2.createAppropriateDriver("Chrome");
        driver.manage().window().maximize();
        driver.get("http://www.seleniumframework.com/Practiceform/");
        System.out.println("***************** Execution Started ****************");
        
    }

    

    @Test
    public void FormPracticetest() throws InterruptedException {
        String color = driver.findElement(By.id("colorVar")).getCssValue("color");
        String BGcolor = driver.findElement(By.id("colorVar")).getCssValue("background-color");
        System.out.println("Color is :"+ color);
        
        String[] numbers = color.replace("rgba(", "").replace(")", "").split(",");
        int r = Integer.parseInt(numbers[0].trim());
        int g = Integer.parseInt(numbers[1].trim());
        int b = Integer.parseInt(numbers[2].trim());
        int a = Integer.parseInt(numbers[3].trim());
        System.out.println("r: "  +r +" "+ "g: " + " "+ g + "b: " + b + " " + "a :"+a);
        String hex = "#" + Integer.toHexString(r) + Integer.toHexString(g) + Integer.toHexString(b) +  Integer.toHexString(a);
        System.out.println(hex);
        
    
        
        System.out.println("Initial Color is :" + hex + "Background color is " + BGcolor);
        
        Thread.sleep(6000);
        String Changedcolor = driver.findElement(By.id("colorVar")).getAttribute("color");
        
        System.out.println("Initial Color is :" + Changedcolor);
        
    }
    
    @After
    public void tearDown() throws Exception {
        System.out.println("****************** Execution End ****************");
        
    }
}
JeyanthiRanjit
  • 161
  • 1
  • 12
  • Depends on the interface on which you are seeing the prints, i.e. the console. Some consoles support ANSI escape sequences, which offer colors. But many dont support colors at all, windows CMD for example. That being said, it solely depends on the console the user of this code has, not on Java, neither on Selenium. – Zabuzard Aug 31 '20 at 18:28
  • I'm curious if the duplicate actually answers the question. @JeyanthiRanjit, my understanding was you wanted to get the string "RED" out instead of the string "rgba(255, 0, 0)", not print "rgba(255, 0, 0)" in red color. Is that correct or am I misreading? – Kaia Aug 31 '20 at 18:34
  • yes @Keon your understanding is correct...the color which i am trying to print in the eclipse console is RED instead of RGBA(255,255,255,1)..how can i decode it and print as RED... – JeyanthiRanjit Aug 31 '20 at 18:54
  • @JeyanthiRanjit then I'd suggest you unclose the question, as the question currently set as duplicate is about printing colors in a console. – Kaia Aug 31 '20 at 18:56
  • @Keon can u help in unclosing this question..i don't know how..thanks for pointing out – JeyanthiRanjit Aug 31 '20 at 19:03
  • https://www.selenium.dev/selenium/docs/api/dotnet/html/M_OpenQA_Selenium_IWebElement_GetCssValue.htm. "Color values should be returned as hex strings. For example, a "background-color" property set as "green" in the HTML source, will return "#008000" for its value." The short answer is, Selenium automatically converts it for you, so you'll have to write something to convert it back. There are questions about naming a color in Java; this might be a good resource: https://stackoverflow.com/q/4126029/1275942 – Kaia Aug 31 '20 at 19:07

0 Answers0