gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 14:25:36 27 4
gpt4 key购买 nike

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

控制台 View :

enter image description here

为此,我必须首先在页面上识别该 Web 元素,获取颜色,将其作为预期值存储在字符串中。

下面的代码做同样的事情:

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

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

在代码行下面使用:

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)

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

最佳答案

试用 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 字符串应该采用函数期望的格式。

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

关于java - 如何使用 Selenium TestNG 和 Java 转换和匹配背景颜色 RGB(255, 255, 255) 与#fff,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54761474/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com