- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的自动化代码中,试图匹配网络元素“为我找到最好的卡片”文本的背景颜色。
控制台 View :
为此,我必须首先在页面上识别该 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/
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我正在使用我在论坛中看到的示例来突出显示当前导航菜单。 例如,我可以让我的事件导航菜单具有 background-color:#fff,但我没有成功执行 color:#fff。为什么 backgrou
如何将 #ffffff 转换为 #fff 或将 #fff 转换为 #ffffff 以进行断言? 我正在使用 getCssValue("background")从 Selenium 返回 rgb(255
我有一个 href 链接“打开菜单”,并且该链接的颜色为灰色。 我想使用 jQuery,因为当我单击此“打开菜单”时,我想将颜色更改为 #FFF。 我尝试使用下面的代码执行此操作,但它不起作用,我正在
我有一个包含十六进制数字的字符串,我想递增该十六进制数字,直到达到我的最大数字 (FFF)。我如何遍历才能获得起始十六进制和 FFF 之间的每个数字? 我尝试将字符串转换为字节数组,但之后卡住了。 s
我正在尝试使用 google test 测试我的 C 库,但是我在使用 fff.h 模拟函数时遇到了麻烦框架。这是我的文件结构: . ├── Makefile.am ├── configure.ac
我正在处理这个页面。 http://www.careersinmusic.com/aaaa-testing-link.aspx 所以当我的主要问题是底部的不可见区域时。我希望它是白色的,但我不知道如何
我们希望在电子邮件中以表格形式显示订单详情 Header 1 Header 2 Header 3
我正在尝试解析 MP3 文件并检测可变比特率 (VBR) MP3 文件中的所有帧。我浏览了整个互联网,但找不到合适的解决方案来检测新的帧头同步。从其他帖子来看,它说我应该寻找 FFE 或 FFF,但在
日期时间中的fff和ms有什么区别? DateTime dt = DateTime.Now; string s1 = dt.ToString("yyyy-MM-dd HH:mm:ss.fff"); s
首先,是的,我知道渲染相同的颜色,我的问题是关于速度的简单查询。 这只是一个关于优化页面加载速度的有趣主题,但标题中的哪个选项会呈现得更快(即使是最微小的差异)? 我的想法是,使用简写版本 (#fff
如果我有: .foo { background-color:#fff; } LESS 将其转换为: .foo { background-color:white; } 这是为什么?浏览器处
在 PowerShell V2 中,我想计算给定字符串的总秒数和毫秒数。 我的字符串是 00:03:56,908,所需的输出是 236.908 我的工作,但笨拙的代码是 $a = "00:03:56,
我想知道是否有人可以帮助我解决这个问题。我需要验证文本框中的文本输入。我需要确保它采用 TimeSpan 可接受的格式。预期格式应为 hh:mm:ss:fff(即小时、分钟、秒和毫秒。例如 15:30
我无法显示“关闭窗口”文本。难道某处是白色的吗?! 根据开发人员的说法,“关闭”应该是要更改的正确值。 transition: "elastic", speed: 300, width
这个 SVG 有 2 个填充,一个是无,第二个是红色。当它没有悬停时,我如何做到这一点,然后当它悬停时,它会变成颜色? 顺便说一下,它现在只改变红色的 .icon{ fill: re
This Wordpress front page使用子主题。 父主题包含 CSS: body { background: #fff; } 这个首页使用 CSS: body.home {
我正在从需要存储在文本框中的数据库中提取 DateTime 值(如果您必须知道,它是一个 ASP.NET 文本框)。此 DateTime 包含毫秒。然后我需要将此值保存回数据库。 问题:当我这样做时
当使用“shorthand hexidecimal”(style="color: #FFF;")定义颜色时,是否有定义的方法来扩展shorthand? (style="color: #F0F0F0;"
我在 :hover 上遇到了 block 子元素的一些问题事件。我有一个定价 block ,主要任务是在 :hover 上获取所有文本 #fff 颜色. 因为当我尝试 :hover 元素和 它们是元素
我是一名优秀的程序员,十分优秀!