本文介绍了我如何在空手道UI中使用javascript executor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,空手道UI自动化确实很棒.使用Karate编写UI测试时,我很喜欢它.我遇到了一种情况,我试图获取shadowRoot元素.我读了一些与空手道相关的与javascript executor相关的类似帖子,并了解到它已经得到了解答.建议使用driver.eval.但是在空手道0.9.5中没有评估,它具有script()或scriptAll().我已经仔细阅读了两次文档,以弄清楚如何获取元素中的元素但没有运气.使用传统的selenium + java,我们可以像这样获取shadowRoot:诸如shadowRoot之类的东西位于div或body之类的父元素中.

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);

所以我在空手道UI中尝试了以下方法

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.

我敢打赌,有一种方法可以使用空手道UI来获取shadowRoot元素,但是我有点用光了选项,无法弄清楚.有人可以看看这个&帮帮我吗?

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?

-圣

推荐答案

您可以切换到XPath看看是否有帮助:

Can you switch to XPath and see if that helps:

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

否则,请以这种格式提交示例,以便我们进行调试: https://github.com/intuit/karate/tree/develop/examples/ui-test

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

在评论中发布了该环聊示例的链接后,我确定了可以使用的JS:

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'

花了一些试验和错误,并弄弄DevTools控制台来解决这个问题.因此,好消息是,您可以使用所需的任何JS,并且确实需要知道哪个 HTML元素来调用.shadowRoot.

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.

对于空手道中JS的其他示例: https://stackoverflow.com/a/60800181/143475

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

这篇关于我如何在空手道UI中使用javascript executor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 21:25
查看更多