As a Selenium programmer you may require to create new methods or functions that doesn't exist.
One of the best method is by using UserExtension.js. In future posts I will explain about extending the existing selenium class.
Add following libraries to your project, attaching the snap shot.
Create new java class using following code. This example is created on google.com, so that any one can execute the code with out any issues.
What is this program about ?
1. Created new Method "MyClick".
2. Two new functions to calculate page response time (timerStart, timerStop).
3. Open the google page, search for an item and display page response time in Milli seconds for the results page.
Initially I had lot of issues while working with user-extensions, I want to bring clarity to my users by providing this example. There are differences between IDE and RC extensions, read my timer extension post for better understanding.
package package1; import static org.testng.AssertJUnit.*; import org.testng.annotations.*; import com.thoughtworks.selenium.*; public class Sample2 { private static final String Timeout = "30000"; private static final String BASE_URL = "http://google.com/"; private static final String BASE_URL_1 = "/"; private Selenium selenium; private HttpCommandProcessor proc; @BeforeClass protected void setUp()throws Exception { proc = new HttpCommandProcessor("localhost", 4444, "*iexplore", BASE_URL); selenium = new DefaultSelenium(proc); selenium.start(); selenium.windowFocus(); selenium.windowMaximize(); selenium.windowFocus(); } @AfterClass(alwaysRun=true) protected void tearDown() throws Exception { selenium.stop(); } @Test(groups="search") public void test_GoogleSearch() throws Exception { selenium.open(BASE_URL_1); selenium.type("name=q", "selenium HQ"); System.out.println(proc.doCommand("getTimerStart",new String[] {"GooglePage"})); //selenium.click("btnG"); //selenium command proc.doCommand("myClick",new String[] {"btnG"}); //user extension for Click () selenium.waitForPageToLoad(Timeout); System.out.println (proc.doCommand("getTimerStop",new String[] {"GooglePage"})); Thread.sleep(5000); //Show the page for few seconds } @Test(groups="search") public void test_GoogleSearch1() throws Exception { selenium.open(BASE_URL_1); selenium.type("name=q", "Bharath Marrivada"); System.out.println(proc.doCommand("getTimerStart",new String[] {"GooglePage"})); //selenium.click("btnG"); //selenium command proc.doCommand("myClick",new String[] {"btnG"}); //user extension for Click () selenium.waitForPageToLoad(Timeout); System.out.println (proc.doCommand("getTimerStop",new String[] {"GooglePage"})); Thread.sleep(5000); //Show the page for few seconds } }
UserExtension.js
Selenium.prototype.doMyClick = function(inputParams) { var element = this.page().findElement(inputParams); this.page().clickElement(element); return null; }; var globalTime = new Object(); Selenium.prototype.getTimerStart = function(target) { var dt1 = new Date(); if (target == null || target == "") { return ("Target not present so timer was not started"); } else { globalTime[target] = dt1; return null; } delete dt1; }; Selenium.prototype.getTimerStop = function(target) { var dt = new Date(); if (target == null || target == "") { return ("Please specify a target"); } else if (globalTime [target] == null) { alert("called"); return ("Start time was not called for " + target); } else { return ("Time Passed for " + target + ":" + Math.floor (dt - globalTime[target]) + " msec"); delete globalTime[target]; } delete dt; };Note: Make sure you create the .js file in the selenium.jar folder.
To execute JavaScript functions from .js file, need to use http command processor "proc.doCommand".
If the function is not returning any value start the function name with "do", else use "get".
Function names are case sensitive, always start with lower case. For "do" function, remove "do" while calling the function from Java.
To start the selenium server use following command.
java -jar selenium-server.jar -userExtensions user-extensions.js
To start the Selenium server quickly, every time instead of going into command prompt, I use following text saved as .vbs file. Double click the file, your selenium server will be up and running. (Modify the folder path)
Dim oShell Set oShell = WScript.CreateObject ("WScript.Shell") oShell.run "cmd /K CD C:\selenium-remote-control-1.0.3\selenium-server-1.0.3 & java -jar selenium-server.jar -userExtensions user-extensions.js" Set oShell = Nothing
Enjoy!
For more information look at selenium documentation.
---
Hi Bharath,
ReplyDeleteYou have been doing a great job .I like your post and the snippets of code for the testing community to understand and reuse.
This surely makes it a candidate on my blog which has the best articles and posts across the web.
Do mail me at aditya_kalra@ymail.com and let me know if it is fine i add this to my blog.
My blog: http://go-gaga-over-testing.blogspot.com/
Best Regards,
Aditya Kalra