In the last tutorial we learnt how to create your first basic script by recording and replaying in the IDE. We are now going to learn how to work with XPathes in your scripts
XPath is a really good way to navigate your site when there are no IDs on elements that you need to work with or is near the element you want to work with.
Note: Web browsers were never designed to handle large amounts of XPath so this means that your scripts may run slowly. You will notice this especially in Internet Explorer.
Start the tutorial by creating a script similar to the one in the last script. Only create a script for the first set of elements on this page. You will be working on the other elements later. I have made a number of programming faux pas which you need take account of in your script.
I have created the same elements again below and would like you to create another script to test the items at the top and the items at the bottom.
I have created an example here. My example will be different to yours and I will explain next why. XPath example 2
I have used different syntaxs in my script. Each of these is just to show you a different way of finding elements by using different kinds of syntax. There is one example where the target is defined by a number of attributes and another is defined by finding one element and then finding its sibling. Another reason for doing this is because the buttons at the top and the bottom are programmed exactly the same. It allows us to have unique identifiers for elements.
In my opinion, if an item hasn't been programmed properly, i.e., lacks an id attribute, it is best to code the Selenium test to find that item in relation to an item that *has* been programmed properly rather than to rely on meaningless indexes. This means that when reading through your script you are more likely to understand where //input[@id='BottomTotal']/following-sibling::input[contains(@value,'Add')] is compared to //li[6]/input[4]. This becomes more obvoius when you have a large number of tests in your test suite.