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

问题描述 投票:1回答:2

在我的自动代码中,尝试匹配网页元素“为我找到最好的卡片”文本的背景颜色。

控制台视图:

enter image description here

要做到这一点,我必须首先在页面上识别该web元素,获取颜色,以String的形式存储为预期值。

下面的代码也是如此:

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

在网站中,值是十六进制的,但Selenium方法将返回rgb中的值所以无论我从上面的代码行得到什么值,首先需要转换为十六进制,然后必须与assert方法进行比较。

在下面的代码行使用:

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);
}

但是当我执行它时,得到以下错误:

=> 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)

如何转换成十六进制并使测试通过?

java css selenium testng assertion
2个回答
2
投票

尝试selenium库

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);

你可以参考selenium官方文档here

以下是为您的案例编写的selenium junit测试中的测试用例。确保您在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);
  }

-1
投票

从你的代码试验中,大概是你回来了:

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

控制台视图

根据您所看到的控制台视图:#fff。在this discussion提到的@DanHerbert,正是压缩器将智能地将#ffffff转换为#fff以优化页面加载速度,因为网络延迟,带宽和解析时间比处理时间更重要。

为了处理这些情况,this discussion中的@ T.J.Crowder建议查找以#开头的字符串,然后是三对匹配的十六进制数字,并在考虑断言之前用短格式替换它们,如下所示:

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");
}

示例实施:

  • 代码块: 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"); } }
  • 控制台输出: #fff

在您的用例中,您可以将其用作:

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);
}
© www.soinside.com 2019 - 2024. All rights reserved.