简体   繁体   中英

How do i use javascript executor in Karate UI

Firstly, Karate UI automation is really awesome tool. I am kind of enjoying it while writing the UI tests using Karate. I ran into a situation where in, i was trying to fetch the shadowRoot elements. I read few similar posts related to javascript executor with karate and learnt that it is already answered. it is recommended to use driver.eval. But in Karate 0.9.5 there is no eval, it has script() or scriptAll(). I have gone through documentation couple of times to figure out how i can fetch element inside an element but no luck. Using traditional selenium+java, we can fetch shadowRoot like this way: something like shadowRoot which sits inside a parent element like div or body.

//downloads-manager is the tagname and under that downloads-manager, a shadowRoot element exists
The HTML looks like this. it is from chrome://downloads.
<downloads-manager>
   #shadow-root(open)

</download-manager>
WebElement downloadManager =driver.findElement(By.tagName("downloads-manager");
WebElement shadowRoot= (WebElement)((JavaScriptExecutor)driver)
                                      .executeScript("return arguments[0].shadowRoot",downloadManager);

So i tried the following in Karate UI

  script("downloads-manager","return _.shadowRoot"); //js injection error

  script('downloads-manager', "function(e){ return e.shadowRoot;}"); // same injection error as mentioned above.

def shadowRoot = locate("downloads-manager").script("function(e){return e.shadowRoot};"); //returns an empty string.

I bet there is a way to get this shadowRoot element using Karate UI but i am kind of running out of options and not able to figure out this. Can someone please look into this & help me?

-San

Can you switch to XPath and see if that helps:

* def temp = script('//downloads-manager', '_.innerHTML')

Else please submit a sample in this format so we can debug: https://github.com/intuit/karate/tree/develop/examples/ui-test

EDIT: after you posted the link to that hangouts example in the comments, I figured out the JS that would work:

* driver 'http://html5-demos.appspot.com/hangouts'
* waitFor('#hangouts')
* def heading = script('hangout-module', "_.shadowRoot.querySelector('h1').textContent")
* match heading == 'Paul Irish'

It took some trial and error and fiddling with the DevTools console to figure this out. So the good news is that it is possible, you can use any JS you need, and you do need to know which HTML element to call .shadowRoot on.

EDIT: for other examples of JS in Karate: https://stackoverflow.com/a/60800181/143475

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM