The easiest way to generate tests, is too perform the actions in your browser and allow the Selenium IDE to populate automatically. In all honesty, this will handle 99% of the features you want to test. But there are times when the browser integrated features aren't enough to test everything and you have to get your hands a little dirty.
Assert the Title Attribute of an Image
For our first example, we're going to verify the title attribute of an image. One of my favorite sites is XKCD and the comics always have a funny image title that is part of the comic. The first thing we're going to do is browse over to http://www.xkcd.com/756/. If you hold the mouse over the image, you will see the title text appear, which says: "News networks giving a greater voice to viewers because the social web is so popular are like a chef on the Titanic who, seeing the looming iceberg and fleeing customers, figures ice is the future and starts making snow cones."
Let's fire up the Selenium IDE by opening Tools > Selenium IDE. Set the base URL to http://www.xkcd.com. Now if you'll right click on the comic, then go to Show all available commands > assertElementPresent //img[@alt='Public Opinion'].
The assertElementPresent command allows you to specify an element exists using locators. We're going to cover locators next. Switch over to the Selenium IDE and select the assertElementCommand we just created. In the target text box, change the @alt='Public Opinion' value to @title=News networks giving a greater voice to viewers because the social web is so popular are like a chef on the Titanic who, seeing the looming iceberg and fleeing customers, figures ice is the future and starts making snow cones.
This means that we are going to locate the image based on the title attribute rather than the alt attribute. Now your test should look like my test in the screenshot. Run the test now by clicking on the green triangle withe three bars and the test should pass. We now have a test that ensures the title text for this particular web comic is what we think it should be.
Selenium Element Locators
Element locators within Selenium allow you to specify which element on a page is the target of a given command. There are three default locators for Selenium that don't require you to specify what type of locator you are using:
- DOM
Locators starting with "document" will use the DOM Locator.
- XPATH
Locators that start with "//" will use the XPATH locator to find a given element.
- Identifier
Locators that start with anything other than "document", "//", or a valid locator type, will default to using the identifier strategy. When using the identifier strategy, the first element with a matching id attribute will be selected. If no matching id is found, then Selenium will use the first element with a matching name attribute.




