我在页面对象类中有以下几行(用于处理Shadow DOM):
11 headerContent(wait:true) { $("content-header") }
12 shadowRoot0(wait:true) { js.exec("return arguments[0].shadowRoot", headerContent) }
但是,当在GebSpock测试中运行它时,它持续失败,并出现以下异常:
| Caused by: java.lang.IllegalArgumentException: The last argument to the js function must be string-like
| at geb.js.JavascriptInterface.exec(JavascriptInterface.groovy:53)
| at test.ui.modules.RightHandHeader._clinit__closure1$_closure3(RightHandHeader.groovy:12)
...
上面的代码行是用JavascriptExecutor和WebElement从下面的代码中翻译过来的,在执行时效果很好:
WebElement shadowDomHost = driver.findElement(By.cssSelector("content-header"))
WebElement root0 = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].shadowRoot", shadowDomHost)
调用js时一定有问题。
有人对这个问题有什么看法吗?向js.exec()传递参数的正确语法是什么? PS:
def exec(Object[] args) {
if (args.size() == 0) {
throw new IllegalArgumentException("there must be a least one argument")
}
def script
def jsArgs
if (args.size() == 1) {
script = args[0]
jsArgs = []
} else {
script = args.last()
jsArgs = args[0..(args.size() - 2)]
}
if (!(script instanceof CharSequence)) {
throw new IllegalArgumentException("The last argument to the js function must be string-like")
}
execjs(script.toString(), *jsArgs)
}
我相信您已经交换了js.exec()
调用中参数的顺序。基于《圣经》:
js对象也有一个exec()方法,可以用来运行JavaScript片段。它与JavascriptExecutor.executeScript()方法相同,除了它以另一种顺序接受参数…
的例子:assert js.exec(1, 2, "return arguments[0] + arguments[1];") == 3
这应该意味着这应该解决您的问题:
shadowRoot0 (wait:true) {
js.exec(headerContent.singleElement(), "return arguments[0].shadowRoot")
}