Karate UI自动化能否获取网页元素的CSS样式属性
attribute() 方法仅支持获取HTML元素的显性声明属性,CSS计算样式不属于该范畴,因此你调用后拿到空值是正常情况。
Karate 支持直接执行前端JS调用浏览器原生API实现类似 Selenium getCssValue() 的效果,你可以通过 script() 方法调用 getComputedStyle 来获取对应样式值,示例如下:
# 定位目标元素 * def targetEle = locate('#eg01SubmitId') # 执行JS获取计算后的font-size样式 * def fontSize = script(targetEle, "function(e){ return window.getComputedStyle(e).getPropertyValue('font-size') }") # 输出验证结果 * print fontSize
相关注意事项:
- 样式属性名需传入CSS原生短横线命名格式,不要使用JS驼峰命名,比如获取字体颜色传
'color'、获取背景色传'background-color'即可 - 你可以将该逻辑封装为通用函数,简化后续复用:
# 封装通用CSS属性获取函数 * def getCssValue = function(selector, cssProp){ var ele = locate(selector) return script(ele, "e => window.getComputedStyle(e).getPropertyValue('" + cssProp + "')") } # 调用示例 * def submitFontSize = getCssValue('#eg01SubmitId', 'font-size') * print submitFontSize
内容的提问来源于stack exchange,提问作者Pankaj Bhambhani
相关产品推荐
相关产品推荐

