The way that our tests interact with elements on the page has changed quite dramatically from Selenium 1. In Selenium 1 you would make an API call passing in a locator and any extra information that is needed. For example, to type you would use:
selenium.typeKeys("id=inputField","Selenium is cool");
This meant that the readability of the tests was quite poor since you needed to check all the parameters of the API call.
Selenium 2 has changed this. In your tests you now have an object that maps directly to the element on the page from where you can use it. We are able to map elements by telling our browser object to find the element we want and to give us a new Selenium 2 WebElement
object. The findElement()
method takes one parameter which tells the driver how to find the object.
This is stored in the static class By. By
has a number of different methods as we can see in the following list:
By.id("idOfObject")
By.linkText("TextUsedInTheLink...