Your tests could be brittle because of various reasons.
1. Synchronization- DO NOT USE Thread.sleep
. You should consider the waiting mechanism in your tests.
There are two types of waits in WebDriver. Implicit wait and Explicit
wait.
a. Implicit wait- For example below WebDriver
will internally poll at max for 30 seconds before throwing NoSuchElementFoundException
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
b. Explicit wait- Here you are telling WebDriver to wait for a certain condition to satisfy. For example, below I am waiting for the link Account
to be available to click. Once its available WebDriver will return me the WebElement
so that I can click on it. Take a look at some already implemented useful ExpectedConditions
WebDriverWait wait = new WebDriverWait(driver,30/*Timeout in seconds*/);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Account")));
element.click();
2. Data Dependency- Make sure your tests are independent of each other and they don't share data. Tests can collide if they are sharing data which makes them brittle
3. Use CSS over Xpaths - Find my answer here as to why
4. A layer of abstraction- Make sure you have abstracted test logic from page logic. Use PageObjects and PageFactory technics for better maintenance of your suite
Finally read Simon Stewarts' blog on Automated Web Testing: Traps for the Unwary for details.