The following test searches on the home page of a site by a make, model and location:
public class AutotraderTests {
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 getHomePageUrl() {
return driver.getCurrentUrl();
}
public void openHomePage() {
driver.get(URL);
}
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 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 typePostalCode(String postalCode) {
WebElement postalCodeBox = driver.findElement(postalCodeId);
postalCodeBox.sendKeys(postalCode);
}
public void executeSearch() {
WebElement postalCodeBox = driver.findElement(postalCodeId);
postalCodeBox.sendKeys(Keys.ENTER);
}
public String getResultsPageTitle() {
return driver.getTitle();
}
}
The test is very simple:
opens the home page
selects a make
selects a model
enters a location
clicks the search button
checks on results page that the page title contains the selected make and model
The test has 3 problems as it uses all the time:
the same make value
the same model value
the same location
I showed you in a different post what to do about the make and model values, how to make random selections.
We are going to find a solution for the location now.
The solution is to
build a list of valid postal codes
select one of these postal codes randomly
This is the code to use for it:
private static final String[] LOCATION_LIST =
new String[] { "V6L 2Y3",
"V6V 2C1",
"V2X 2P8",
"J7C 2H9",
"H4T 1B1",
"H7T 2W1"
};
private String getRandomLocation() {
Random random = new Random();
int n = random.nextInt(LOCATION_LIST.length);
return locationList[n];
}
The getRandomLocation() method generates a random number that is between 0 and the length of the LOCATION_LIST array. It then gets the value of the array that has the index equal to the random number.
The location array has only 6 values, 3 of them for cities from British Columbia, Canada, the other 3 for cities from Montreal, Canada. You can have as many values as you like. Ideally, you select them from the database where all search data is stored.
Having the new method for generating a random location, the test can be changed to:
@Test
public void searchReturnsResultsForRandomLocationTest() throws InterruptedException {
openHomePage();
Assert.assertEquals(getHomePageUrl(), URL);
selectMake(MAKE);
selectModel(MODEL);
typePostalCode(getRandomLocation());
executeSearch();
Thread.sleep(3000);
String resultsPageTitle = getResultsPageTitle();
Assert.assertTrue(resultsPageTitle.contains(MAKE));
Assert.assertTrue(resultsPageTitle.contains(MODEL));
}
In summary, every time it is possible, please randomize the test data used by your Selenium test.