【问题标题】:Karate - UI Automation - Is it possible to grab Css style attributes of web element空手道 - UI 自动化 - 是否可以获取 Web 元素的 Css 样式属性
【发布时间】:2021-05-21 13:54:57
【问题描述】:

我正在尝试访问 web 元素的以下属性

enter image description here

在空手道中有什么方法可以让我获取 UI 元素的 CSS 样式属性,就像我们在 selenium 中的 getCssValue("font-size")

我试过下面的代码

  • 打印属性('#eg01SubmitId', 'font-size')

但它给出了一个空值

【问题讨论】:

    标签: karate


    【解决方案1】:

    请使用 JS,空手道不需要所有这些花哨的 API。

    请参考script()的文档:https://github.com/intuit/karate/tree/master/karate-core#script

    然后使用普通的 DOM API-s 你可以这样做:

    * def displayStyle = script('.Popover', "_.style['display']")
    

    我参考了这篇文章:https://zellwk.com/blog/css-values-in-js/

    编辑:如果您阅读了最后一个链接,您应该已经意识到如果样式来自 CSS 类等,您应该使用 getComputedStyle()

    所以试试这个:

    * print script('.demo-btn', "function(e){ return getComputedStyle(e)['font-size'] }")
    

    【讨论】:

    • 我尝试了上述方法,但仍然得到一个空值,我的应用程序是一个角度应用程序,其中所有样式属性都是 CSS 类的一部分。
    • @PankajBhambhani 它对我有用。所以现在请弄清楚这一点,为空手道贡献代码或遵循这个过程:github.com/intuit/karate/wiki/How-to-Submit-an-Issue
    • 我也尝试过其他网站,其中 css 样式嵌入在 CSS 类中,例如orangehrm.com > Get a Free Demo button > 它不起作用,这是我的代码 - * print script('{button}Get a Free Demo', "_.style['font-size']"), another我试过的方法 - * def element = locate('{button}Get a Free Demo') * def getCss = function(m) { return getComputedStyle(m) } * print getCss(element) 但没有运气,你能帮忙吗?
    • @PankajBhambhani 不,除非你遵循流程
    • 编辑的部分解决了我的问题,非常感谢您的快速帮助,我查看了 getComputedStyle 但没有正确使用它:)
    猜你喜欢
    • 1970-01-01
    • 2020-09-08
    • 2012-06-01
    • 2021-05-04
    • 1970-01-01
    • 2019-04-25
    • 2017-12-30
    • 1970-01-01
    • 2013-10-27
    相关资源
    最近更新 更多