在我的自动代码中,尝试匹配网络元素“查找最适合我的卡片”文本的背景颜色。
控制台视图:
要做到这一点,我必须首先在页面上标识该网络元素,获取颜色,并将其存储在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)
如何转换为十六进制并通过测试?
答案 0 :(得分:2)
尝试硒库
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);
您可以参考硒官方文档here
下面是针对您的案例编写的硒单元测试中的测试案例。确保您在Color.fromString("rgbString")
中传递的rgb字符串应采用函数期望的格式。
@Test
public void rgbToHex() {
String hex = "#01ff03";
String rgb = "rgb(1, 255, 3)";
assertThat(Color.fromString(rgb).asHex()).isEqualTo(hex);
}
答案 1 :(得分:-1)
从您的代码试用中,大概您会回来:
255
255
255
根据控制台视图,您会看到: #fff
。 this discussion中的@DanHerbert提到,css压缩器会将#ffffff
聪明地转换为#fff
,以优化页面加载速度为网络延迟,带宽和解析时间比处理时间要重要。
为处理这些情况,this discussion中的@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");
}
示例实现:
代码块:
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);
}