Like many aspects of programming, it's not that hard to get started, and even to write something that looks like a test. The real difficulty lies in test engineering.
If you know Java, particularly if you've used JUnit before, you can get started with Selenium in about ten minutes. If you know Maven, you can simply add the dependencies to your pom.xml as with any library, browse some of the basic documentation, and be well on your way to automating your tests. Copy and paste ad nauseum and you will rapidly have a suite of tests... that are fairly fragile, difficult to maintain, and likely to be abandoned before too long.
Engineering automated tests is a whole topic in itself, but you'll want to educate yourself on common pitfalls and the DRY principle if you want your tests to be maintainable. The Webdriver/Selenium community has rallied around the idea of PageObjects, which is a major concept that helps simplify test maintenance by, in essence, creating a layer of objects whose responsibility is knowing how to interact with your application, leaving the tests the responsibility of defining correct behavior. This means that when the application changes, you only have to adjust a few lines of code, not dozens or hundreds of test cases. If you do nothing else, read up on PageObjects.
My test scripts can be as simple as:
/**
* Runs before every test to ensure we're on the right page.
*/
@Before
public void SetUp() {
//Navigate to the Location page
locationPage = (LocationPage) PageFactory.initElements(driver, LocationPage.class).navigateTo();
}
/**
* Test: Verify that when given a valid zip code, the location page accepts it as valid.
*
* Given I am on the location page
* When I enter a valid zip code
* Then the page should recognize that the zip code is valid
*
* @throws Exception if the location page does not respond to the zip code entered
*/
@Test
public void testValidZipcodeIsValid() throws Exception {
assertTrue(locationPage.zipcodeIsValid("94044"));
}
(and yes, I just noticed I forgot to type the Exception, oopsie)
Or a more complex example:
/**
* Test:
* Given that I am on the product page
* When I click the “A-Z” button
* Then the tires should be sorted alphabetically in ascending order
* When I click the “Z-A” button
* Then the tires should be sorted alphabetically in descending order
*/
@Test
public void sortAlpha() {
Map<Criteria, Boolean> criteriaList = new HashMap<Criteria, Boolean>();
criteriaList.put(Criteria.MULTISIZE, false);
Vehicle vehicle = Vehicle.getRandomVehicleByCriteria(criteriaList);
logger.info("Using vehicle " + vehicle);
vehiclePage.selectVehicle(vehicle);
verifyPage = (VerifyPage) vehiclePage.clickNext();
tireCoachPage = (TireCoachPage) verifyPage.clickNext();
productPage = (ProductPage) tireCoachPage.clickNext();
productPage.sortBy(Sort.AtoZ);
verifyTiresSortedAlphabetically(true);
productPage.sortBy(Sort.ZtoA);
verifyTiresSortedAlphabetically(false);
}