Javascript is a programming language used primarily by Web browsers to create a dynamic and interactive experience for the user. Most of the functions and applications that make the Internet indispensable to modern life are coded in some form of JavaScript.
In LEAPWORK, we can handle complex requirements with ease using the Run Web Javascript block. A Run Web JavaScript building block is used to execute a piece of JavaScript in an open browser window. This can, for instance, be used to trigger events on a web page, such as “exit intent”. Please note that this block only works with browser windows that were previously opened using the Start Web Browser block or their “child” windows.
Let us consider a small requirement of how to return multiple values from a function in Javascript:
Let's say, a web page having multiple row items out of which we need to pull more than one value. Below is the solution in LEAPWORK.
Understanding the solution:
- In the first "Run Web Javascript" block, the document method querySelector() returns the first element within the document that matches the specified selector, or group of selectors and stores it in variable 'a'.
- Var b contains the collection of all table rows, i.e. <tr> elements in a table.
- The return statement i.e b.length returns the row count and it is available as an output parameter via the Result connector. This Result connector is connected to the maximum connector of the Loop block in order to define the number of times the iteration process should be executed.
- The second "Run Web Javascript" block executes in iterations and it contains a function with the name 'array' that returns an array of elements based on some validations.
- Once the values are returned from the array function, they are typecasted to string type for every 'i' th iteration so that the final result can be logged in using the "Log message" block.
- Once done with all of the iterations, the user may proceed with the next set of logic by triggering the 'Completed' (green) connector of the Loop block.
Javascript Code view:
First Javascript block
var a = document.querySelector("div#DeltaPlaceHolderMain
table.ms-v4propertysheetspacing tbody tr td table tbody:nth-child(2)
tr:nth-child(1) td:nth-child(4) table tbody");
var b = a.rows;
return b.length;
Second Javascript block
function array(name,num){
var obj = [name,num];
return obj;
}
var i = arguments[0];
var display = b[i].querySelector("td:nth-child(1) input").checked;
if(display == true)
{
var name = b[i].querySelector("td:nth-child(3) label").textContent;
var pos = b[i].querySelector("td:nth-child(4) select option:checked").value;
var x= array(name,num);
var text = "";
text += x[i] + " ";
}
return text;
OUTPUT
Visit Run Web JavaScript documentation to know more about the block.
For any clarification, please contact our Priority Support.
Comments
0 comments
Please sign in to leave a comment.