When working with the Selenium object access via locators – string identifying the object over which it is held or other action. The most convenient and fastest locators are defined by the ID of the object (each object in the HTML page can be defined by the attribute ID, and it must be unique). Well really, if not determined ID, then at least for the form elements have attributes Name, through which is also quite convenient and easy to operate. But in general, have to work with a great variety of objects and actions and have to make a variety. For example:
Each individual case to solve these problems in their own ways, but more or less universal solution is to use XPath. What is its convenience.
And now for the practical component. Suppose we have a set of different schedules in the form of bar-chart or pie-chart, and when you click on each element of the transition on some pages. Implementing the HTML of this is:
1 <map>
2 <area href="ref1">
3 <area href="ref2">
4 <area href="ref3">
5 …
6 <area href="ref1">
7 <img src="some_img.gif">
8 </map>
like these map-block any number. In each of these blocks is an arbitrary number of elements of the area. But everywhere there are references to these area-objects we can make a bid. So how can you organize the processing of all active regions of the charts. Initially, we will know how much all these diagrams present. Assume that we already have initialized the object Selenium-a and we are already on the correct page. Accordingly, the realization of the form (below, all examples are given using the syntax of Java, but by analogy carries over to other languages, which is implemented Selenium-client):
1 int chartsCount = selenium.getXPathCount ("/ / map"). intValue ();
Now, in a loop for each diagram, we calculate the number of active regions, and then turns to click on them (for example, simply call the click for each active region). The implementation is as follows:
1 int chartsCount = selenium.getXPathCount ("/ / map"). intValue ();
2 for (int i = 1; i <= chartsCount; i + +) {
3 int areasCount = selenium.getXPathCount ("/ / map [" + i + "] / area");
4 for (int j = 1; j <= areasCount; j + +) {
5 selenium.click ("/ / map [" + i + "] / area [" + j + "]");
6 }
7 }
Note that here we have operated with the index. For example, XPath of the form "/ / map [" + i + "] / area [" + j + "]" refers to the element map, which is on this page should be under an ordinal number (index) i and is an appeal to the j-th element area of ??the map.
Another example. Suppose we have a set of links, which in the onclick handler is the same method but with different parameters. Such is often encountered in the tables where each row is a specific set of functional elements that work with this string. Suppose it is all elements with the attribute img src = "some_icon.gif" and onclick handler calls someAction, but with different parameters depending on the line. Also, let’s say each of these images is the value of the attribute alt, which indicates with any element of the action occurs. That is, in fact, HTML is as follows:
01 <table>
02 <tr>
03 <td>
04 <img src="some_icon.gif" onclick="someAction( 105 )" alt="Item 1">
05 </ td>
06 </ tr>
07 <tr>
08 <td>
09 <img src="some_icon.gif" onclick="someAction( 78 )" alt="Item 2">
10 </ td>
11 </ tr>
12 ………………………………………….
13 <tr>
14 <td>
15 <img src="some_icon.gif" onclick="someAction( 923 )" alt="Item N">
16 </ td>
17 </ tr>
18 </ table>
And suppose we should get a list of values ??of the alt attribute for all of these images. Sold it the following algorithm:
Get the number of necessary images
Loop through each image and extract its alt attribute
Software implementation of the form:
1 int count = selenium.getXPathCount ("/ / img [@ src = 'some_icon.gif' and contains (@ onclick, 'someAction')]");
2
3 String [] titles = new String () [count];
4
5 for (int i = 1; i <= count; i + +) {
6 titles [i - 1] = selenium.getAttribute ("/ / img [@ src = 'some_icon.gif' and contains (@ onclick, 'someAction')] [" + i + "] @ alt");
7 }
Please note that the record / / img [@ src = 'some_icon.gif' and contains (@ onclick, 'someAction')] ["+ i +"] are 2 expressions in square brackets. If the brackets are numbers, then this expression is perceived as an index. Otherwise, this expression-filter attributes.
These examples demonstrated the possibility of batch processing of objects similar structures using XPath as a locator. The above examples are simplified versions of those tasks that had to be solved in practice, that is, these examples are not from the ceiling. Nevertheless, in this flexibility is one drawback – for large pages with XPath is quite a sizeable time and in different browsers XPath time works differently. Therefore, if possible, to improve the possibility to test the application, it is better to specify the object ID and contact ID, but nevertheless, in some cases can be developed sufficiently flexible solution and XPath can be in this very helpful.