WebDriver executeAsyncScript vs executeScript [英] WebDriver executeAsyncScript vs executeScript

查看:1393
本文介绍了WebDriver executeAsyncScript vs executeScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

executeAsyncScript和executeScript有什么区别?我如何使用window.onload等事件?我试过这样的事情

What is the difference between executeAsyncScript and executeScript? How can i use event such as window.onload? I tried something like this

((JavascriptExecutor) driver).executeAsyncScript("window.onload = function() {alert('Hello')}"); 

但当然它不起作用...
所以如果有人知道它是如何工作的请写一个例子

But of course it did not work... So if anyone knows how it works please write an example

推荐答案

我使用 executeScript 。提供的示例:

String cssSelector="...blablabla...";
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("document.getElementById(\'"+cssSelector +"\').click();");
js.executeScript(stringBuilder.toString());

关于警报的详细信息,已知问题。您可以此处获取详细信息

Concerning details on alerts there is known issue. you can get details here

根据文件的不同之处是:

In accordance with documentation difference is:


executeScript

public java.lang.Object executeScript(java.lang.String script,
                             java.lang.Object... args)

从界面复制的说明:JavascriptExecutor
在当前所选框架的上下文中执行JavaScript或窗口。提供的脚本片段将作为
的主体执行匿名函数。在脚本中,使用document来引用
当前文档。请注意,一旦
脚本执行完毕,局部变量将不可用,但全局变量将持续
。如果脚本有返回值(即如果脚本包含
a return语句),则将采取以下步骤:

Description copied from interface: JavascriptExecutor Executes JavaScript in the context of the currently selected frame or window. The script fragment provided will be executed as the body of an anonymous function. Within the script, use document to refer to the current document. Note that local variables will not be available once the script has finished executing, though global variables will persist. If the script has a return value (i.e. if the script contains a return statement), then the following steps will be taken:


  • 对于HTML元素,此方法返回WebElement

  • 对于小数,返回Double

  • 对于非小数数字,返回Long

  • 对于布尔值,返回布尔值

  • 对于所有其他情况,返回一个String。

  • 对于数组,按照上述规则返回包含每个对象的List。我们支持嵌套列表。

  • 除非值为null或没有返回值,否则返回null

  • For an HTML element, this method returns a WebElement
  • For a decimal, a Double is returned
  • For a non-decimal number, a Long is returned
  • For a boolean, a Boolean is returned
  • For all other cases, a String is returned.
  • For an array, return a List with each object following the rules above. We support nested lists.
  • Unless the value is null or there is no return value, in which null is returned

参数必须是上述任意组合的数字,布尔值,字符串,WebElement或List
。如果
参数不符合这些条件,则抛出异常。参数将通过arguments魔术变量提供给JavaScript的
,好像
函数通过Function.apply调用

Arguments must be a number, a boolean, a String, WebElement, or a List of any combination of the above. An exception will be thrown if the arguments do not meet these criteria. The arguments will be made available to the JavaScript via the "arguments" magic variable, as if the function were called via "Function.apply"

指定者:接口JavascriptExecutor中的executeScript
参数:script - 要执行args的JavaScript -
脚本的参数。可能为空返回:Boolean,Long,String,List
或WebElement之一。或null。

Specified by: executeScript in interface JavascriptExecutor Parameters: script - The JavaScript to execute args - The arguments to the script. May be empty Returns: One of Boolean, Long, String, List or WebElement. Or null.

executeAsyncScript

public java.lang.Object executeAsyncScript(java.lang.String script,
                                  java.lang.Object... args)

从接口复制的描述:JavascriptExecutor
在当前选定的框架或窗口的上下文中执行异步JavaScript。与执行同步
JavaScript不同,使用此方法执行的脚本必须通过调用提供的回调显式地通知
它们已完成。这个回调是
总是作为最后一个参数注入到执行函数中。
传递给回调函数的
第一个参数将被用作
脚本的结果。此值的处理方式与
同步案例的处理方式相同。

Description copied from interface: JavascriptExecutor Execute an asynchronous piece of JavaScript in the context of the currently selected frame or window. Unlike executing synchronous JavaScript, scripts executed with this method must explicitly signal they are finished by invoking the provided callback. This callback is always injected into the executed function as the last argument. The first argument passed to the callback function will be used as the script's result. This value will be handled in the same way as the synchronous case.

示例#1:在被测浏览器中执行休眠。

 long start = System.currentTimeMillis();
   ((JavascriptExecutor) driver).executeAsyncScript(
       "window.setTimeout(arguments[arguments.length - 1], 500);");
   System.out.println(
       "Elapsed time: " + (System.currentTimeMillis() - start));  

示例#2:将测试与AJAX应用程序同步:

 WebElement composeButton = driver.findElement(By.id("compose-button"));
   composeButton.click();
   ((JavascriptExecutor) driver).executeAsyncScript(
       "var callback = arguments[arguments.length - 1];" +
       "mailClient.getComposeWindowWidget().onload(callback);");
   driver.switchTo().frame("composeWidget");
   driver.findElement(By.id("to")).sendKeys("bog@example.com");

示例#3:注入XMLHttpRequest并等待结果:

 Object response = ((JavascriptExecutor) driver).executeAsyncScript(
       "var callback = arguments[arguments.length - 1];" +
       "var xhr = new XMLHttpRequest();" +
       "xhr.open('GET', '/resource/data.json', true);" +
       "xhr.onreadystatechange = function() {" +
       "  if (xhr.readyState == 4) {" +
       "    callback(xhr.responseText);" +
       "  }" +
       "}" +
       "xhr.send();");
   JSONObject json = new JSONObject((String) response);
   assertEquals("cheese", json.getString("food"));

脚本参数必须是数字,布尔值,字符串,WebElement,
或者以上任意组合的清单。如果参数不符合这些条件,则会抛出
的异常。参数将是
通过arguments变量提供给JavaScript。

Script arguments must be a number, a boolean, a String, WebElement, or a List of any combination of the above. An exception will be thrown if the arguments do not meet these criteria. The arguments will be made available to the JavaScript via the "arguments" variable.

指定者:接口JavascriptExecutor中的executeAsyncScript
参数:script - 要执行的JavaScript。 args - 脚本的参数
。可能是空的。返回:Boolean,Long,String,
List,WebElement或null之一。

Specified by: executeAsyncScript in interface JavascriptExecutor Parameters: script - The JavaScript to execute. args - The arguments to the script. May be empty. Returns: One of Boolean, Long, String, List, WebElement, or null.

详细文档此处

这篇关于WebDriver executeAsyncScript vs executeScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆