java - 使用 Selenium 和 Eclipse (Java) 断言表格边框的 css 样式

标签 java css selenium assert

我正在测试一个表单,测试的一部分是将必填字段留空,并检查该字段是否有红色边框,表明该字段是必填字段,并且应该在继续操作之前填写。到目前为止我已经得到了以下代码:

driver=new FirefoxDriver();
driver.manage().window().maximize();

/** Test: Open page*/
driver.get ("URL with form");

/** the following confirms a text above the form stating what fields are mandatory and not filled */
driver.findElement(By.id("nextbutton")).click(); /** leave all fields blank and click on next below form */
assertEquals("The following fields are mandatory", driver.findElement(By.cssSelector("p")).getText());
assertEquals("Field 1 is mandatory", driver.findElement(By.xpath("//form[@id='genericform']/div[2]/ul/li")).getText());
assertEquals("Field 2 is mandatory", driver.findElement(By.xpath("//form[@id='genericform']/div[2]/ul/li[2]")).getText());
assertEquals("Field 3 is mandatory", driver.findElement(By.xpath("//form[@id='genericform']/div[2]/ul/li[3]")).getText());

assertTrue(isElementPresent(By.id("Field1.Value"))); /** confirms the first mandatory field is present */

现在我想确认/断言必填字段有红色边框和图标,而非必填字段没有红色边框或图标。 enter image description here

CSS:
边框颜色:#E04228; 背景:#FFF url("../img/icon-field-error.png") 无重复滚动右中心/28px 22px;

我正在使用 Eclipse 和 Selenium webdriver

最佳答案

使用getCssValue()获取 border-bottom-colorborder-top-colorborder-left-colorborder-right-color 的方法 CSS 属性:

assertEquals("rgba(224, 66, 40)", element.getCssValue("border-bottom-color"));

请注意,border-colorcomputed并且无法通过这种方式检索。

您还可以convert the rgba value to HEX format使其更具可读性和方便性。

<小时/>

或者,您可以只检查 input 元素是否具有 errorinput-validation-error 类:

assertEquals("block error input-validation-error", element.getAttribute("class"));

关于java - 使用 Selenium 和 Eclipse (Java) 断言表格边框的 css 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32740060/

相关文章:

python - 扫描类名相同的行元素

java - 有没有办法更改 final方法的访问修饰符范围?

java - BlackJack Java String[] 不工作

java - Spring Cloud 配置客户端是否也可以从本地加载属性?

html - 具有 4 个链接的 CSS 图像 Sprite

maven - 如何正确配置 Selenium Maven 插件以与 Xvfb 一起运行以 headless 运行

java - 获取快照始终产生相同的值

html - 更改 <h1> 颜色 - Designfolio WP 主题。谷歌字体?

html - 是否可以明确地使网格项始终出现在最后一行或最后一列?

selenium - 在 Protractor 中选择第一个可见元素