简体   繁体   English

如何使用 Selenium TestNG 和 Java 转换和匹配背景色 RGB(255, 255, 255) 与 #fff

[英]How to convert and match the background color RGB(255, 255, 255) with #fff using Selenium TestNG and Java

In my automated code, trying to match background color of a web-element 'Find the best card for me' text.在我的自动化代码中,尝试匹配网络元素“为我找到最好的卡片”文本的背景颜色。

Console view :控制台视图:

在此处输入图片说明

To do it, I have to identify that web-element on page first, get the color, store in String as expected value.为此,我必须首先识别页面上的 web 元素,获取颜色,将其作为预期值存储在 String 中。

Below code does the same:下面的代码做同样的事情:

WebElement slickDotButton2Presence = driver.findElement(homepageobjectsloc.slickDotButton2);
slickDotButton2Presence.click();
String findTheBestCarsForMeTextBackgroundColour = driver.findElement(homepageobjectsloc.secondBannerFindTheBestCardForMeText).getCssValue("background");

In website value is in hex, but Selenium method will return values in rgb So whatever value I got from above line of code need to convert into hex first and then have to compare with assert method.在网站中,值是十六进制的,但 Selenium 方法将返回 rgb 中的值所以我从上面的代码行得到的任何值都需要先转换为十六进制,然后必须与 assert 方法进行比较。

Used below line of code:使用下面的代码行:

try {
    String value = findTheBestCarsForMeTextBackgroundColour.trim();
    String[] rgbs = value.split("\\)")[0].split("\\(")[1].split(",");
    long r = Long.parseLong(rgbs[0]);
    long g = Long.parseLong(rgbs[1]);
    long b = Long.parseLong(rgbs[2]);
    String hex = String.format("#%02x%02x", r, g, b);
    System.out.println("=> The hex conversion is : " + hex);
    Assert.assertEquals("#fff", hex);
}

But when I execute it , getting below error:但是当我执行它时,出现以下错误:

=> The hex conversion is : #ffff
java.lang.AssertionError: expected [#ffff] but found [#fff]
    at org.testng.Assert.fail(Assert.java:94)
    at org.testng.Assert.failNotEquals(Assert.java:513)
    at org.testng.Assert.assertEqualsImpl(Assert.java:135)
    at org.testng.Assert.assertEquals(Assert.java:116)
    at org.testng.Assert.assertEquals(Assert.java:190)
    at org.testng.Assert.assertEquals(Assert.java:200)
    at tests.homepage.HomePageStepDefinitions.verify_that_Find_the_best_card_for_me_text_is_available_on_the_second_banner_in_hompage_then_click_on_it(HomePageStepDefinitions.java:795)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at cucumber.runtime.Utils$1.call(Utils.java:40)
    at cucumber.runtime.Timeout.timeout(Timeout.java:16)
    at cucumber.runtime.Utils.invoke(Utils.java:34)
    at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:38)
    at cucumber.runtime.StepDefinitionMatch.runStep(StepDefinitionMatch.java:37)
    at cucumber.runtime.Runtime.runStep(Runtime.java:300)
    at cucumber.runtime.model.StepContainer.runStep(StepContainer.java:44)
    at cucumber.runtime.model.StepContainer.runSteps(StepContainer.java:39)
    at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:44)
    at cucumber.runtime.model.CucumberFeature.run(CucumberFeature.java:165)
    at cucumber.api.testng.TestNGCucumberRunner.runCucumber(TestNGCucumberRunner.java:63)
    at cucumber.api.testng.AbstractTestNGCucumberTests.feature(AbstractTestNGCucumberTests.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
    at org.testng.TestRunner.privateRun(TestRunner.java:782)
    at org.testng.TestRunner.run(TestRunner.java:632)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
    at org.testng.SuiteRunner.run(SuiteRunner.java:268)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
    at org.testng.TestNG.run(TestNG.java:1064)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

How to convert onto hex and make test pass?如何转换为十六进制并通过测试?

Try selenium library试试硒库

import org.openqa.selenium.support.Color;
String value = findTheBestCarsForMeTextBackgroundColour.trim();
String hex = Color.fromString(value).asHex();
System.out.println("=> The hex conversion is : " + hex);
Assert.assertEquals("#fff", hex);

You can refer selenium official documentation here您可以在此处参考 selenium 官方文档

Below is the test case in selenium junit tests written for your case.以下是为您的案例编写的 selenium junit 测试中的测试案例。 Make sure the rgb String you are passing in Color.fromString("rgbString") should be in format the function expects.确保您在Color.fromString("rgbString")中传递的 rgb String 应该是函数期望的格式。

  @Test
  public void rgbToHex() {
    String hex = "#01ff03";
    String rgb = "rgb(1, 255, 3)";
    assertThat(Color.fromString(rgb).asHex()).isEqualTo(hex);
  }

From your code trials, presumably you are getting back:从你的代码试验中,大概你回来了:

  • r -> 255 r -> 255
  • g -> 255克 -> 255
  • b -> 255 b -> 255

Console view控制台视图

As per the Console view you are seeing: #fff .根据您看到的控制台视图: #fff @DanHerbert in this discussion mentions, it is the compressors that will intelligently convert #ffffff to #fff for optimizing page-loading speed as network latency , bandwidth and parsing time matters more than processing time in general. @DanHerbert 在本次讨论中提到, 压缩器会智能地将#ffffff转换为#fff以优化页面加载速度,因为网络延迟带宽解析时间通常比处理时间更重要。

To deal with these cases, @TJCrowder in this discussion suggests to look for a string starting with # followed by three pairs of matching hex digits, and replace them with just the short form before you consider for assertion as follows:为了处理这些情况, 本讨论中的@TJCrowder 建议查找以#开头的字符串,后跟三对匹配的十六进制数字,并在考虑断言之前将它们替换为短形式,如下所示:

static String getHex(int r, int g, int b) {
    return String.format("#%02x%02x%02x", r, g, b).replaceAll("^#([a-fA-F])\\1([a-fA-F])\\2([a-fA-F])\\3$", "#$1$2$3");
}

Sample implementation:示例实现:

  • Code Block:代码块:

     package demo; public class Background_White { public static void main(String[] args) { System.out.println(getHex(255, 255, 255)); // #fff } static String getHex(int r, int g, int b) { return String.format("#%02x%02x%02x", r, g, b).replaceAll("^#([a-fA-F])\\\\1([a-fA-F])\\\\2([a-fA-F])\\\\3$", "#$1$2$3"); } }
  • Console Output:控制台输出:

     #fff

In your usecase you can use it as:在您的用例中,您可以将其用作:

try {
    String value = findTheBestCarsForMeTextBackgroundColour.trim();
    String[] rgbs = value.split("\\)")[0].split("\\(")[1].split(",");
    long r = Long.parseLong(rgbs[0]);
    long g = Long.parseLong(rgbs[1]);
    long b = Long.parseLong(rgbs[2]);
    String hex = String.format("#%02x%02x%02x", r, g, b).replaceAll("^#([a-fA-F])\\1([a-fA-F])\\2([a-fA-F])\\3$", "#$1$2$3");
    System.out.println("=> The hex conversion is : " + hex);
    Assert.assertEquals("#fff", hex);
}

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

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