Monday, October 31, 2011

Using UI-Elements user-extension with Selenium RC

Using UI-Elements user-extension with Selenium RC, Selenium Notes, Videos, Examples By.www.laxmiroy.blogspot.com/


Please refer to my post UI-Elements - All about it to understand what UI-Elements are. In this post I wish to describe how UI-Elements can be used with Selenium RC.

There are a few things that are different here from how it is used with selenium ide
1) In Selenium IDE the UI-Map file is added to the IDE environment as selenium core extension. You may add any number of such files from the IDE->options->Selenium core extensions.
In Selenium RC the UI Map file is added either programatically or added as command line parameter when invoking selenium-server.

2)In Selenium IDE you may add as many map files as you wish. However in
Selenium RC you can add only one UI-Map file and that file must be in the same directory as selenium-server.jar. Also the name of ui-map file must be 'user-extensions.js'.

Below is code for attaching the ui-map file to the selenium server. The 'getConfiguration()' method of seleniumserver object is used to get the configuration object and the 'setUserExtension()' method is used to set the uimap file as the user-extension.
.
.
.
SeleniumServer seleniumserver;
seleniumserver=new SeleniumServer();
RemoteControlConfiguration a= seleniumserver.getConfiguration();
File uimap=new File("lib\\user-extensions.js");
a.setUserExtensions(uimap);
seleniumserver.boot();
seleniumserver.start();
.
.
.

Now what if you have multiple ui-map files? In that case you must combine all of them together to create one consolidated 'user-extension.js' file. I have written a function that does this for me.
Please note that you must remove the instruction


var myMap = new UIMap();

from the individual map files as the function adds it automatically in the combined file.

public static void createUIMAP(String masterUIMAPfilePath) throws java.io.IOException{             
RandomAccessFile file = null;
RandomAccessFile fileui = null;
String line = null;
String lineui = null;


File oldFile=new File("lib\\user-extentions.js");
if (oldFile.exists())
oldFile.delete();

FileWriter fstream = new FileWriter("lib\\user-extensions.js",true);
String theFirstLine="var myMap = new UIMap();";
fstream.write(theFirstLine, 0, theFirstLine.length());
file = new RandomAccessFile(masterUIMAPfilePath, "r");
while ((line = file.readLine()) != null) {
System.out.println(line);
fileui = new RandomAccessFile(line, "r");
while ((lineui = fileui.readLine()) != null) {
System.out.println(lineui);
fstream.write(lineui, 0, lineui.length());
fstream.write(System.getProperty("line.separator"));

}
}
fstream.close();
}
This function deletes any file with the name 'user-extension.js' that may be there in the \lib folder within your java project and creates a new 'user-extension.js' file by combining any number of child ui-map file(s) whose location(s) is mentioned in the text file masterUIMAPfile whose path is supplied as a parameter to this function.
The masterUIMapFile may look something like this (all paths are relative to the java project).

test\\Resources\\UIMaps\\admin\\uimap1.js
test\\Resources\\UIMaps\\admin\\uiMapGoogle.js
I have placed the 'uiMaster.txt' intest\Resources\UIMaps\admin\uiMaster.txt within my java project.
Now I will put this all together with the script that was created in my lastpost. The createUIMAP() function is to be called before calling the setUserExtensions() method.


import org.junit.AfterClass;
import org.junit.Test;
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import com.thoughtworks.selenium.*;
import java.io.File;
import java.io.FileWriter;
import java.io.RandomAccessFile;


public class testGoogleUIMapNew extends SeleneseTestCase {
public void setUp() throws Exception {
createUIMAP("test\\Resources\\UIMaps\\admin\\uiMaster.txt");
SeleniumServer seleniumserver;
seleniumserver=new SeleniumServer();
RemoteControlConfiguration a= seleniumserver.getConfiguration();
File uimap=new File("lib\\user-extensions.js");
a.setUserExtensions(uimap);
seleniumserver.boot();
seleniumserver.start();

setUp("http://www.google.com/", "*firefox");
}



@Test public void testGoogleuimap() throws Exception {
selenium.open("www.google.co.in");
verifyTrue(selenium.isElementPresent("ui=googleSearch_page::theBigGoogleLogo()"));
selenium.type("ui=googleSearch_page::search_Textbox()", "wikipedia");
selenium.click("ui=googleSearch_page::search_button()");
selenium.waitForPageToLoad("30000");
verifyTrue(selenium.isElementPresent("ui=googleSearch_page::searchwikipedia_textbox()"));
selenium.type("ui=googleSearch_page::searchwikipedia_textbox()", "selenium");
selenium.click("ui=googleSearch_page::searchwikipedia_button()");
selenium.waitForPageToLoad("30000");
verifyTrue(selenium.isElementPresent("ui=googleSearch_page::seleniumSearchResult_Link()")); 
selenium.click("ui=googleSearch_page::seleniumSearchResult_Link()");
selenium.waitForPageToLoad("30000");
verifyEquals(selenium.getTitle(), "Selenium (software) - Wikipedia, the free encyclopedia");
verifyTrue(selenium.isElementPresent("ui=wikipediaArticle_page::wikipediaGlobe_image()"));              
}


@AfterClass
public void tearDown(){
selenium.close();
selenium.stop();
} //end of tearDown()

public static void createUIMAP(String masterUIMAPfilePath) throws java.io.IOException{
RandomAccessFile file = null;
RandomAccessFile fileui = null;
String line = null;
String lineui = null;


File oldFile=new File("lib\\user-extentions.js");
if (oldFile.exists())
oldFile.delete();

FileWriter fstream = new FileWriter("lib\\user-extensions.js",true);
String theFirstLine="var myMap = new UIMap();";
fstream.write(theFirstLine, 0, theFirstLine.length());   
file = new RandomAccessFile(masterUIMAPfilePath, "r");
while ((line = file.readLine()) != null) {
System.out.println(line);
fileui = new RandomAccessFile(line, "r");
while ((lineui = fileui.readLine()) != null) {
System.out.println(lineui);
fstream.write(lineui, 0, lineui.length());
fstream.write(System.getProperty("line.separator"));

}
}
fstream.close();
}
}

To run this just right click and choose option 'Run As junit test'. Please remember that in my case the 'selenium-server.jar' fie is in the /lib folder.

You should ideally tuck away the createUIMAP() function and the code to start the selenium-server in a utility class in your project and just be calling them in your test script.

Also note that the testGoogleuimap() test method in this example is from my last post. You can derive this java equivalent of the IDE recording by doing Options-->format-->Java (Junit) - Selenium RCfrom Selenium IDE. I have just replaced the awfully big while constructs in the script with

selenium.waitForPageToLoad("30000");
As always junit, selenium-server and selenium-client-driver are in the classpath.
UI-Elements - All about it!

1 comment:

  1. Will this approach work with webdriver?
    I get the following error when trying the format of junt4/webdriver ie SIDE
    " ERROR: Caught exception [Error: UI locators are not supported!]"

    ReplyDelete