Warning: fopen(/tmp/SghXTngBZPli-UKgXLg.tmp): failed to open stream: Disk quota exceeded in /home/executea/public_html/blog/wp-admin/includes/class-wp-filesystem-ftpext.php on line 139

Warning: unlink(/tmp/SghXTngBZPli-UKgXLg.tmp): No such file or directory in /home/executea/public_html/blog/wp-admin/includes/class-wp-filesystem-ftpext.php on line 142
Site Loader
Auckland, New Zealand
Javascript/JQuery plays a vital role in any modern web-based application for client side scripting, considering the fact, most of the times we might need to perform some operation in our test code written in Selenium using Javascript and JQuery. But the question of the day is how to do that? Well the answer is simple, as we already saw in the post of highlighting an element using Selenium, we used IJavaScriptExecutor which is an Interface to execute a Javascript or JQuery, this interface has an method called ExecuteScript, which will does the execution of script and return the value. Well to check out what are the return types of the ExecuteScript method, here is the list The ExecuteScript(String, Object[]) method executes JavaScript in the context of the currently selected frame or window. This means that “document” will refer to the current document. If the script has a return value, then the following steps will be taken:
  • For an HTML element, this method returns a IWebElement
  • For a number, a Int64 is returned
  • For a boolean, a Boolean is returned
  • For all other cases a String is returned.
  • For an array,we check the first element, and attempt to return a List(T) of that type, following the rules above. Nested lists are not supported.
  • If the value is null or there is no return value, a null reference (Nothing in Visual Basic) is returned.

Arguments must be a number (which will be converted to a Int64), a Boolean, a String or a IWebElement. 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”

How does this works? To be more concise in answer, the IJavaScriptExecutor with the help of WebDriver instance injects the javascript or JQuery code in to the tested website or AUT (Application Under Test), since it’s not new to WebDriver because it does the same while controlling an web browser. Let’s start writing a code to demonstrate how to execute a Javascript using IJavaScriptExecutor Let’s consider here is the Div element, where we need to get its text using it’s ID <div id=”Title”>Hello from executeautomation !!!</div>
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
String script = "return document.getElementById('Title');";
IWebElement exampleDiv = (IWebElement)js.ExecuteScript(script);
string values = exampleDiv.Text; // this will return "Hello from executeautomation
 As you can see from the above C# code snippet, we can get the text by passing the ID of the div using Javascript. Well you can do the same by passing a JQuery to execute using ExecuteScript method. Let’s consider we need to get the number of checkbox checked in a page we are testing. The JQuery code for doing so is shown below
$('form').find('input[type=checkbox]:checked').length
We can pass the same in ExecuteScript method as shown
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
String script = "$('document').find('input[type=checkbox]:checked').length";
Int64 exampleDiv = (Int64)js.ExecuteScript(script);
You can find more information from Selenium site Thanks, Karthik KK

Post Author: Karthik kk

One Reply to “Executing Javascript/JQuery in selenium”

  1. Hii,

    Greetings.

    In this you have mentioned basic concepts in c#. Could you please upload the same article in Java. Because we are working on selenium java. We are not able to find any websites with these type of basic jquery concepts.

Leave a Reply

Your email address will not be published. Required fields are marked *