Regarding your page. Not only IDs are increasing XPath performance but also name and a single class field. So the XPathes
//div[@name='nameValue']
//div[@class='cssClass']
should also be performant as javascript-xpath uses document.getElementsByName() and document.getElementsByClassName() in this case. But I never tried this until now as our app always has some useful IDs (it's a Dojo app and Dojo adds some IDs), I only saw this in the source of javascript-xpath.
But I have another performance tipp - something that I am also doing in many cases. Following HTML:
...
....
...
Title 1
Title 2
We are having here a DOM with two forms (I did not include all the inputs that are in there, just something ...). The two forms are equal - something that is happening more and more offen in Web 2.0 applications where the end-user selects which content he wants to display on a page. A typical example is a weather widget that displays weather information for one location. But as users are interested in the weather in their hometown but also of there favorite holiday location they are placing the same widget two times on one page. This is difficult to differentiate when using XPathes. E. g. to access form elements we need to use the XPath:
//body/div[starts-with(@id, 'generatedId']/h2[.='Title 1']/../form/input[@name='zipCode']
//body/div[starts-with(@id, 'generatedId']/h2[.='Title 2']/../form/input[@name='zipCode']
But these are very inperformant. Especially if you need to fill other input fields and click a button and verify that some things happend you will run into hard performance issues on Internet Explorer. Therefore we need to first get the generated id and then continue:
String idTitle1 = selenium.getAttribute("//body/div[starts-with(@id, 'generatedId']/h2[.='Title 1']/..@id");
selenium.type("//div[id='" + idTitle1 + "']/form/input[@name='zipCode']", "12345");
...
Unfortunately that seems not to be something for your site but for the Selenium documentation.
2 comments:
Hi
I am trying to use Selenium / JUnit and the application is designed using Isomorphic / Smart Client.
I see that for some objects, the ID value is not a constant, it varies every time the page is loaded. This makes it very difficult for me to identify objects, especially when there are two buttons of the same name in a page.
As a work around, I want to find the XPath of certain objects on the run time using Selenium / JUnit.
Is there any function wherein, I will be able to find the XPath of a particular object on the application ?
Thanks
Anish
PS:- Ur blog is very good "Getting started with Selenium" blog. Keep writing.
Hi Ashis,
These are the challenges we as a automation engineers face in automation .
you can go through below blog you will get to know how we can overcome this situation.
http://automationtricks.blogspot.com/2010/05/how-to-identify-dynamic-element-in.html
Post a Comment