Selenium Javascriptexecutor Detailed

Source: Internet
Author: User
Tags function definition


Introduced

In the Selenium IDE, we can use the RunScript command to execute JS code snippets to assist in some of the tasks selenium inconvenient to accomplish, as well, In the Webdriver we can also use the Javascriptexecutor tool class to complete the JS code execution, the following I will elaborate on four points of the use of the tool and how it works.


Topics discussed in this article include:

1. Javascriptexecutor two ways to execute JS code.

2. Javascriptexecutor two ways to execute the JS code using the example.

3. Javascriptexecutor executes the JS principle.

4. Javascriptexecutor Common cases.


Below we turn to the above three topics to explain in detail!


1. Javascriptexecutor two ways to execute JS code

Object Executescript (String script, Object ... args);

Object Executeasyncscript (String script, Object ... args);


The Executescript method receives two parameters and a return value:

script, JavaScript snippet, this JS snippet is the full method body as the JS function, you can use the return statement as the function's return value.

args, a parameter array, which is used to pass external data to a script (JS snippet), which can be indexed by Arguments[index] in the args array, the parameter data type must be the following (number , Boolean, String, Webelement, or a list collection of data types above), of course no parameters can be left blank.

The return value, returned by the JS snippet, is returned by the return statement, the return value data type can be (webelement,double,long,boolean,string,list or map), there is no return statement, The returned data here is null.


The Executeasyncscript method receives two parameters and a return value:

script, JavaScript code fragment, this JS code fragment is as the whole method of JS function body, and Executescript main two points are different:

1. The script here must explicitly call the callback method at the end of the code to notify Webdriver that the script execution is complete, and the callback method is injected by webdriver into the last element of the arguments array , which can be obtained through arguments[arguments.length-1], and can be used to return the result of the calculation (just put back the result as a parameter to the callback function)

2. The script execution will have a time-out, default is 60s, the callback method is not called during the timeout period, and Javascriptexecutor throws a timeout exception.

args, rules with Executescript.

return value , rule same as Executescript


2. Javascriptexecutor two ways to execute the JS code using the example.

Example one      using the Executescript method, in the JS code to get the method passed in the parameter array//using the arguments index method passed in the parameter array, and returns the result of the function body calculation that is defined using return.//code passed in 3 parameters, JS Statement index second, three parameters. 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 B      Use the Executeasyncscript method, in the JS code to get the method passed in the parameter array, And by calling the callback method to return the function body calculation results//code passed in 3 parameters, JS Statement index second, three parameters. The time-out is set for the callback method call, the callback method is not called during the timeout period, and the default is to wait for the set timeout, where no return throws an exception.         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. Javascriptexecutor executes the JS principle.


How to understand Javascriptexecutor how to run JS code, you need to have a certain understanding of the JavaScript base, first of all, to list two JavaScript three definitions and call the functions of the example, It's not hard to see how webdriver is going to run JavaScript code, but also to eliminate the confusion (why you use arguments in JavaScript to receive parameters passed in by the method).

The first function is defined in the following way: Functions sum (A, b) {return a+b;  } sum (2,3);    Output 5 The second method of function definition: var sum = new function (' A ', ' B ', ' return a+b; ');   SUM (2,3)//output 5 The second way we can rewrite this is: New Function (' A ', ' B ', ' return arguments[0]+arguments[1] '). Apply (null,[1,2]); Output 5

Of course, the definition of the function is not limited to the above three kinds of writing, we focus on the second and the third method, I believe you see these two ways to use, we have understood how webdriver call JS code, but also wondering why the use of arguments to receive parameters.

All of our defined function is essentially an implementation of the function class, whereas in the definition of the functions class arguments as a local variable, all parameters are received through the arguments index, even if the parameter is 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 i=0; i<len; i++) {sum + = arguments[i];    } return sum;   } add (1,2,3,4); 10


I believe that we should be very clear about the Webdriver is how to execute the JS code principle, in fact, Webdriver is the new function to define the anonymous function to run JavaScript code.



Selenium Javascriptexecutor Detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.