In this following article we will see how can we run custom JavaScript in selenium web driver. I am using c#(VS2010) here. As there are many uses of JS ,this will be a long document. I am providing the basic ideas. I will update incrementally.
To run java script , we need a Java script executor
private IJavaScriptExecutor jsExecuter;
In the method we will initialize that
jsExecuter = (IJavaScriptExecutor)driver;
Note : Here we are casting the driver.I use Firefox driver. Firefox driver implements remote driver which implements java script executor. By this we can specify the JavaScript executor functions.
-To Run JavaScript(asynchronously) in the currently selected frame or window.
jsExecuter.ExecuteAsyncScript("my JavaScript as text");
-To Run JavaScript in the currently selected frame or window.
jsExecuter.ExecuteScript("my JavaScript as text");
Note : both takes script as string, arguments as parameter and provides output as object (object type based on how we use that or what we use as the script). Both of these functions also execute java script with parameters. For that just add another parameter as value[ExecuteScript("script", "Value")]. For return of the executers
-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 is returned.
For JavaScript, we always have to remind that the text that we will use as java script, is fully a java script code . If we need a function output from that java script, we have to use " return" before the script. For example, if we want title
jsExecuter.ExecuteScript("return document.title");
To run java script , we need a Java script executor
private IJavaScriptExecutor jsExecuter;
In the method we will initialize that
jsExecuter = (IJavaScriptExecutor)driver;
Note : Here we are casting the driver.I use Firefox driver. Firefox driver implements remote driver which implements java script executor. By this we can specify the JavaScript executor functions.
-To Run JavaScript(asynchronously) in the currently selected frame or window.
jsExecuter.ExecuteAsyncScript("my JavaScript as text");
-To Run JavaScript in the currently selected frame or window.
jsExecuter.ExecuteScript("my JavaScript as text");
Note : both takes script as string, arguments as parameter and provides output as object (object type based on how we use that or what we use as the script). Both of these functions also execute java script with parameters. For that just add another parameter as value[ExecuteScript("script", "Value")]. For return of the executers
-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 is returned.
For JavaScript, we always have to remind that the text that we will use as java script, is fully a java script code . If we need a function output from that java script, we have to use " return" before the script. For example, if we want title
jsExecuter.ExecuteScript("return document.title");
No comments:
Post a Comment