问题描述
我实际上正在使用Selenium IDE来测试网站的许多部分,所以我是新手。我一直在寻找验证网页元素的CSS样式属性(例如字体颜色,背景颜色等)的方法。
我找到了许多使用命令“ VerifyAttribute”的文档,但是这些文档是3至6年前的,而实际版本没有此命令,而且我似乎找不到任何等效的文档。
我尝试了“ VerifyElementPresent”和类似的命令,但是尽管我添加了css属性(例如background-color =#333333),但测试始终通过,因为该元素存在于页面中,无论我在“值”框。
所以我的问题是:
1-是否可以使用Selenium IDE验证CSS样式属性和值?
2-如果是,我应该使用哪个命令,以及如何编写正确的期望值?
预先感谢您的帮助!
解决方法
您可以使用execute <script>
执行JavaScript命令。基本上,您想获取元素的style
属性或根据情况使用getComputedStyle
。
以下示例适用于https://github.com/:
首先是execute
步骤
Command : execute
Target : return getComputedStyle(document.querySelector(".Header-old")).backgroundColor
Value : backgroundColor
后跟提取的值上的assert
:
Command : assert
Target : backgroundColor
Value : "rgb(36,41,46)"
,
rgba_value = element.value_of_css_property('color')
其中元素由driver.find_element(xpath-value)找到的
rgb_value = change_css_output_to_rgb_value(rgba_value)
icon_color_name = get_colour_name(rgb_value)