-
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 executeautomationAs 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').lengthWe 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
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.