简体   繁体   English

如何验证 selenium webdriver 中的文本颜色?

[英]How to verify text color in selenium webdriver?

I have a web application.我有一个 web 申请。

Written automation script with selenium webdriver.使用 selenium webdriver 编写的自动化脚本。

I have write some color code when I select some text.当我 select 一些文本时,我已经写了一些颜色代码。

Now I want to check that color is present or not.现在我想检查颜色是否存在。

How can I verify color code in selenium webdriver script?如何验证 selenium webdriver 脚本中的颜色代码?

You could use .getCssValue to get the value of color.您可以使用.getCssValue来获取颜色的值。

As you specified, if you want to verify the color, you can assert it, something like this,正如你所指定的,如果你想验证颜色,你可以断言它,像这样,

assertTrue(selenium.isElementPresent("css=td[bgcolor=#000]"));

After so many try with different script finally I am able to find my question`s answer.经过多次尝试不同的脚本后,我终于找到了我的问题的答案。

String colorString = driver.findElement(By.id("foo")).getAttribute("class");
String[] arrColor = colorString .split("#");
assertTrue(arrColor[1].equals("FFFFFF"));

Thank You all for helping me.谢谢大家帮助我。

WebElement eleSearch = driver.findElement(By.xpath("//*[@class='navsearchbar']//div[2]//div"));

String rgbFormat = eleSearch.getCssValue("background-color");

System.out.println(rgbFormat);     //In RGB Format the value will be print => rgba(254, 189, 105, 1)

String hexcolor = Color.fromString(rgbFormat).asHex(); //converted Into HexFormat
System.out.println(hexcolor);// Output of Hex code will be  => #febd69

Using Java class Color, because getCSSValue returns colour in RGBA.使用 Java class 颜色,因为 getCSSValue 返回 RGBA 中的颜色。

Assertions.assertTrue(Color.fromString("#000000").equals(Color.fromString(yourWebelement.getCssValue("color"))), "Text 'fieldName' color");
//you can also try converting rgb into hex and verify...

// Color RGB   
 color = driver.findElement(By.id("xxxxx")).getCssValue("color").trim();    
 System.out.println("RGB_Color: " + color);  

 //  RGB to HEX   
 String color_hex[];  
 color_hex = color.replace("rgba(", "").split(",");       
 String actual_hex = String.format("#%02x%02x%02x", Integer.parseInt(color_hex[0].trim()), Integer.parseInt(color_hex[1].trim()), Integer.parseInt(color_hex[2].trim()));  

// further can verify with Actual hex value with Expected hex value  
Assert.assertEquals("actual_hex should equal to: ", "#808080", actual_hex);

Green color hash code #RRGGBB : #008000 // Test Data, can replace with any specific color绿色哈希码 #RRGGBB : #008000 // 测试数据,可以替换为任何特定颜色

Can try below code :可以试试下面的代码:

String colorString = driver.findElement(By.id("foo")).getAttribute("class");
String[] arrColor = colorString .split("#");
assertTrue(arrColor[1].equals("008000"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM