1

I am trying to access the below attributes of the web element

enter image description here

Is there any way in karate where I can grab the CSS style attribute of the UI elements, like how we have in selenium getCssValue("font-size")

I have tried the below code

  • print attribute('#eg01SubmitId', 'font-size')

but it's giving an empty value

1 Answers1

1

Just use JS please, Karate does not need all these fancy API-s.

Please refer the documentation for script(): https://github.com/intuit/karate/tree/master/karate-core#script

And then using normal DOM API-s you can do this:

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

I referred this article: https://zellwk.com/blog/css-values-in-js/

EDIT: if you read the last link, you should have realized that if the styles come from a CSS class etc, you should use getComputedStyle()

so try this:

* print script('.demo-btn', "function(e){ return getComputedStyle(e)['font-size'] }")
Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • I tried the above way but still getting an empty value, my application is an angular application where all styling attributes are part of a CSS class. – Pankaj Bhambhani May 21 '21 at 02:21
  • @PankajBhambhani it works for me. so now please figure this out, contribute code to karate or follow this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue – Peter Thomas May 21 '21 at 04:09
  • I tried with other websites as well where the css styles are embedded in a CSS class e.g. https://www.orangehrm.com/ > Get a Free Demo button > it doesn't work, here is my code - * print script('{button}Get a Free Demo', "_.style['font-size']"), another way I tried - * def element = locate('{button}Get a Free Demo') * def getCss = function(m) { return getComputedStyle(m) } * print getCss(element) but no luck, could you please help ? – Pankaj Bhambhani May 21 '21 at 05:34
  • @PankajBhambhani no unless you follow the process – Peter Thomas May 21 '21 at 05:37
  • @PankajBhambhani ok I'll save you some time. please see my edit – Peter Thomas May 21 '21 at 05:55
  • 1
    The edited part solved my issue, thanks a lot for the quick help, I looked at getComputedStyle but was not using it correctly :) – Pankaj Bhambhani May 21 '21 at 06:59