The following test implements a vehicle search using a vehicle make, vehicle model and location:
public class SearchTests {
private ChromeDriver driver;
private WebDriverWait wait;
private static final String URL = "https://www.autotrader.ca/";
private static final By makeListId = By.id("rfMakes");
private static final By modelListId = By.id("rfModel");
private static final By postalCodeId = By.id("locationAddressV2");
private static final String MAKE = "BMW";
private static final String MODEL = "4 Series";
private static final String POSTAL_CODE = "V6L 2Y3";
@BeforeMethod
public void setUp() {
driver = new ChromeDriver();
driver.manage().window().maximize();
wait = new WebDriverWait(driver, Duration.ofSeconds(30));
}
@AfterMethod
public void tearDown() {
driver.quit();
}
@Test
public void searchReturnsResultsTest() throws InterruptedException {
openHomePage();
Assert.assertEquals(getHomePageUrl(), URL);
selectMake(MAKE);
selectModel(MODEL);
typePostalCode(POSTAL_CODE);
executeSearch();
Thread.sleep(3000);
String resultsPageTitle = getResultsPageTitle();
Assert.assertTrue(resultsPageTitle.contains(MAKE));
Assert.assertTrue(resultsPageTitle.contains(MODEL));
}
public String getResultsPageTitle() {
return driver.getTitle();
}
public void executeSearch() {
WebElement postalCodeBox = driver.findElement(postalCodeId);
postalCodeBox.sendKeys(Keys.ENTER);
}
public void typePostalCode(String postalCode) {
WebElement postalCodeBox = driver.findElement(postalCodeId);
postalCodeBox.sendKeys(postalCode);
}
public void selectModel(String model) {
Select modelList = getModelList();
modelList.selectByValue(model);
}
public Select getModelList() {
wait.until(ExpectedConditions.elementToBeClickable(modelListId));
WebElement modelElement = driver.findElement(modelListId);
return new Select(modelElement);
}
public void selectMake(String make) {
Select makeList = getMakeList();
makeList.selectByValue(make);
}
public Select getMakeList() {
wait.until(ExpectedConditions.elementToBeClickable(makeListId));
WebElement makeElement = driver.findElement(makeListId);
return new Select(makeElement);
}
public String getHomePageUrl() {
return driver.getCurrentUrl();
}
public void openHomePage() {
driver.get(URL);
}
}
The test is very simple and very short.
You would expect it to execute under 10 seconds.
However, on average, it executes for 25 seconds.
25 seconds? Why so much?
The test finishes fast going through its actions.
But the site keeps waiting for a long time until a lot of third-party requests are done. So the test waits for these requests to be done before completing.
Keep reading with a 7-day free trial
Subscribe to Selenium For Beginners to keep reading this post and get 7 days of free access to the full post archives.