I'm trying to write a simple UI test with Selenium, TestNG and Hamcrest. The problem is that webshop (Opencart) which I'm using as testing playground uses the same classes for displaying featured products in front page and search results. My test method:
@DataProvider(name = "searchDataIterator")
public Iterator<Object> searchDataProviderWithIterator(){
return new ArrayList<Object>(Arrays.asList("macbook", "iphone", "tv", "nokia", "hat")).iterator();
}
@Test(groups = "generic", dataProvider = "searchDataIterator", priority = 4)
public void runSearchWithDataProvider(String searchData) throws InterruptedException {
driver.findElement(By.cssSelector("#search input")).clear();
driver.findElement(By.cssSelector("#search input")).sendKeys(searchData, Keys.ENTER);
Thread.sleep(500);
assertThat(driver.findElements(By.className("product-thumb")).size(), is(greaterThan(0)));
}
Problem is that if I dont't use Thread.sleep(500); the line assertThat(driver.findElements(By.className("product-thumb")).size(), is(greaterThan(0))); catches either Featured product count from front page or results from previous search. I was told that using Therad.sleep() is very bad practice, but how to achieve same thing without it?