Adding custom LocatorBuilders to selenium ide

Adding custom LocatorBuilders to selenium ideSocialTwist Tell-a-Friend

Background : Selenium is an open source tool for web application testing.
Selenium IDE is a Firefox plugin allows you to record tests in browser and run them from the browser or as a java test ( or any other languages ) using Selenium Remote Control libraries.
You can extend functionality of Selenium IDE by writing Event Handlers, Locator Builders, Command Builders, Custom Format all in JavaScript.
For more about that http://wiki.openqa.org/display/SIDE/Writing+extensions.

More on Locator Builders
Locator Builders decide how HTML elements in a page are identified.
eg id attribute is a default locator builder.
So while recording a test an html element attribute selenium using the id attribute to recognize the element you changed, clicked etc.

Writing a new locator builder

You can instruct selenium to use a custom attribute to identify the elements in your page. Default Locator builders can be found at chrome://selenium-ide/content/locatorBuilders.js

Here's the js code to add a location builder for an attribute myattr

LocatorBuilders.add('myattr', function(e) {
if (e.hasAttribute('myattr')) {
if (e.getAttribute("myattr")) {
return '//' + e.nodeName + '[@myattr=\'' +
e.getAttribute("myattr") + '\']';
}
}
return null;
});


Since there are plenty of existing location builders, you need to specify the order in which they are invoked. This is how its done

LocatorBuilders.order = ['myattr','id', 'link', 'name', 'dom:name',
'xpath:link','xpath:img', 'xpath:attributes', 'xpath:href',
'dom:index', 'xpath:position'];

Complete code:
mylocator.js

LocatorBuilders.add('myattr', function(e) {
if (e.hasAttribute('myattr')) {
if (e.getAttribute("myattr")) {
return '//' + e.nodeName + '[@myattr=\'' +
e.getAttribute("myattr") + '\']';
}
}
return null;
});


LocatorBuilders.order = ['myattr','id', 'link', 'name', 'dom:name',
'xpath:link','xpath:img', 'xpath:attributes', 'xpath:href',
'dom:index', 'xpath:position'];

Steps to add the extension code to selenium IDE

  • Write locator code in a new .js file
  • Specify the path in "Selenium IDE extensions" field in Selenium IDE > Options and restart selenium IDE


Writing an xml document to a file & indent it

Writing an xml document to a file & indent itSocialTwist Tell-a-Friend

Heres a simple code snippet in java for writing an xml document created
to an xml file.
Seems quite a common task but had to google quite a bit to get it right ....



Document doc = null; // load your DOM into this document object

TransformerFactory transfactory = TransformerFactory.newInstance();

Transformer transformer = transfactory.newTransformer();

StringWriter writer = new StringWriter();
StreamResult destination = new StreamResult(writer);
DOMSource source = new DOMSource(doc);

transformer.transform(source, destination);

String xmlString = writer.toString();

OutputStream os;

byte buf[] = xmlString.getBytes();
os = new FileOutputStream("output.xml");

for(int i=0;i
os.write(buf[i]);
}

os.close();
buf = null;




Modifications to the code so that the output xml file is properly indented.



TransformerFactory transfactory = TransformerFactory.newInstance();

// code for indenting
transfactory.setAttribute("indent-number", new Integer(2));

Transformer transformer = transfactory.newTransformer();

// code for indenting
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

Kandil 2009

Kandil 2009SocialTwist Tell-a-Friend

http://photos-f.ak.fbcdn.net/hphotos-ak-snc1/hs275.snc1/10226_298034140206_810140206_9114742_6068614_n.jpg

Shantaram Waingankar wrote
at 9:26pm on October 19th, 2009
My kandil made from 212 old cd's

Radhika Wagle wrote
at 9:28pm on October 19th, 2009
wow!!...HOW!!??

Priyanka Potdar wrote
at 10:08pm on October 19th, 2009
WOW!

Jaideep Mahajan wrote
at 10:37pm on October 19th, 2009
wat wrk .....superb

Aaron D'souza (MICA India) wrote
at 10:39pm on October 19th, 2009
send me the image in full resolution

Shantaram Waingankar wrote
at 11:09pm on October 19th, 2009
i'll take a good pic tomm in hi res nd send it

Anil Raju Puliyeril wrote
at 11:56pm on October 19th, 2009
Awesome man. U did some phtotshop editing kya??
Coz the light effect etc seems to be superb...

Shantaram Waingankar wrote
at 12:03am on October 20th, 2009
dude .... come over tomorrow if u cant believe it

Anil Raju Puliyeril wrote
at 12:13am on October 20th, 2009
Hehe just joking... Too good man just upload
sm better pic so that we can relish it even more...

Gayatri Singh wrote
at 12:46am on October 20th, 2009
Hey.. That's awesome...& Happy Diwali to u !

Darshan Divecha wrote
at 1:21am on October 20th, 2009
same 2 u!

Kunal Modi wrote
at 6:29am on October 20th, 2009
finally you used the cds.. great.. Happy New Year..


Kitesh Jain wrote
at 2:08pm on October 20th, 2009
awesome man nd happy new year


Soni mami (UShenoy@aol.com) wrote
at 5:00 PM on October 24, 2009
neat way of being creative. Loved the creation.


Nikhil Patwardhan wrote
at 8:13 PM on October 24, 2009
Nice!! :-)
How was Diwali?


Vikram wrote
at 8:26 PM on October 24, 2009
Boss - this is quite amazing. very creative!


purnit chhajer wrote
at 11:06 PM on October 24, 2009
hey nice wrk yaar...it can be a nice go green venture, wat say??
where r u wrkin now? and happy diwali!!!


Kavita Dabke wrote
at 6:49 PM on Sun, Oct 25, 2009
Kandil masta ahe! Khuupach creative idea!

Vrunda Modi wrote
at 5:37 AM on October 26, 2009
I love ur Kandil.=]


Kadambari Raut wrote
at 10:52 AM on Mon, Oct 26, 2009
"jr. Edison", awsm kandil.. fantabulous!!!


Vansa wrote
at 12:31 AM on Thu, Oct 22, 2009
wow...that's really pretty,Shantaram!!!
your kandil

BlogCatalog

Travel Blogs - BlogCatalog Blog Directory