我正在测试一个表单,测试的一部分是将必填字段留空,并检查该字段是否有红色边框,表明该字段是必填字段,并且应该在继续操作之前填写。到目前为止我已经得到了以下代码:
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 */
现在我想确认/断言必填字段有红色边框和图标,而非必填字段没有红色边框或图标。
CSS:
边框颜色:#E04228;
背景:#FFF url("../img/icon-field-error.png") 无重复滚动右中心/28px 22px;
我正在使用 Eclipse 和 Selenium webdriver
最佳答案
使用getCssValue()
获取 border-bottom-color
、border-top-color
、border-left-color
和 border-right-color 的方法
CSS 属性:
assertEquals("rgba(224, 66, 40)", element.getCssValue("border-bottom-color"));
请注意,border-color
为 computed并且无法通过这种方式检索。
您还可以convert the rgba value to HEX format使其更具可读性和方便性。
<小时/>或者,您可以只检查 input
元素是否具有 error
和 input-validation-error
类:
assertEquals("block error input-validation-error", element.getAttribute("class"));
关于java - 使用 Selenium 和 Eclipse (Java) 断言表格边框的 css 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32740060/