Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Selenium JavascriptExecutor detailed explanation

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Resource recommendations:

Video resources: series of videos related to software testing

Community Resources: automated Test Communication Group

Introduction

In Selenium IDE, we can use runScript commands to execute js code fragments to help complete some tasks that Selenium is not convenient to achieve, similarly, in WebDriver, we can also use JavascriptExecutor tool classes to complete js code execution, the following four points I will elaborate on the use of the tool and how it works.

Topics discussed in this article include:

1. Introduction of two ways for JavascriptExecutor to execute js code.

2. Examples of two ways for JavascriptExecutor to execute js code.

3. The principle that JavascriptExecutor executes js.

4. Common cases of JavascriptExecutor.

Next, we will explain the above three topics in detail.

1. Introduction of two methods of executing js code by JavascriptExecutor

Object executeScript (String script, Object... Args)

Object executeAsyncScript (String script, Object... Args)

The executeScript method receives two parameters and a return value:

The script,javascript snippet, which serves as the complete method body of the js function, and can be used as the return value of the function using the return statement.

Args, parameter array, parameter array are used to pass external data to script (js code snippet). Parameters in args array can be indexed by arguments [index] in script; parameter data types must be the following (number, boolean, String, WebElement, or List collection of the above data types), of course, no parameters can be left empty.

The return value is calculated by the js code snippet and returned through the return statement. The data type of the returned value can be (WebElement,Double,Long,Boolean,String,List or Map). If there is no return statement, the returned data is null.

The executeAsyncScript method receives two parameters and a return value:

The script,javascript snippet, which serves as the complete method body of the js function, differs from executeScript in two main ways:

1. The script here must explicitly call the callback method at the end of the code to notify webdriver of the end of the script execution; the callback method is the last element injected by webdriver into the arguments array; it can be obtained through arguments [arguments.length-1], and the callback function can be used to return the calculation result (you only need to use the return result as an argument to the callback function)

two。 The script execution will have a timeout. The default is 60s. If the callback method is not called during the timeout, JavascriptExecutor will throw a Timeout exception.

Args, the rules are the same as executeScript.

Return value, the rule is the same as executeScript

2. Examples of two ways for JavascriptExecutor to execute js code.

/ / example one uses the executeScript method to get the parameter array passed by the method in the js code / / uses the parameter array passed in the arguments index method, and uses return to return the calculation result of the defined function body. / / three parameters are passed in the code, and the second and third parameters are indexed in the js statement. JavascriptExecutor jsExec = (JavascriptExecutor) driver; String functionBody = "return arguments [1] +','+ arguments [2]"; String returnRes = (String) jsExec.executeScript (functionBody, 1, "hello", "selenium"); System.out.println (returnRes) / / example 2 uses the executeAsyncScript method to obtain the array of parameters passed by the method in the js code, and returns three parameters in the function body calculation result / / code by calling the callback method, and the second and third parameters are indexed in the js statement. / / the timeout is set for the callback method call. The callback method is not called during the timeout. By default, the timeout will wait for the set timeout. If no timeout is returned, an exception will be thrown. Driver.manage (). Timeouts (). SetScriptTimeout (2, TimeUnit.SECONDS); String script = "var res = arguments [0] +','+ arguments [1];" + "var callback = arguments [2];" + "callback ()" String returnVal = (String) driver.executeAsyncScript (script, "hello", "selenium"); System.out.println ("> >" + returnVal)

3. The principle that JavascriptExecutor executes js.

How to understand how JavascriptExecutor runs js code requires a certain understanding of the basis of javascript. First of all, I will list two examples of three definitions and calling functions in javascript. After reading the examples, it is not difficult to understand how webdriver runs javascript code. At the same time, it can also eliminate the confusion (why arguments is used in javascript to receive the parameters passed in the method).

The first method of function definition: function sum (a, b) {return aqb;} sum (2p3); / / output 5 the second way of function definition: var sum = new Function ('axiom,' breadth, 'return axib') Sum (2jue 3) / / output 5 the second way we can rewrite it is: new Function ('averse,' baked, 'return arguments [0] + arguments [1]') .apply (null, [1je 2]); / / output 5

Of course, the definition of function is not limited to the above three writing methods. Here we focus on the second and third methods. I believe that when you see these two ways of use, you already understand how webdriver calls js code, but you will also wonder why you use arguments to receive parameters.

All the function we define is essentially an implementation of the Function class, while in the definition of the Functions class, arguments is used as a local variable and all parameters are received through the arguments index. Even if the parameters are not specified in the method definition, let's take a look at the following code example:

Function add (a) {var sum = 0, len = arguments.length; for (var iTuno; I)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report