Please have a look at this Selenium test:
public class AutotraderTests {
private ChromeDriver driver;
private WebDriverWait wait;
private static final String URL = "https://www.autotrader.ca/";
private static final By MAKE_LIST_ID = By.id("rfMakes");
private static final By MODEL_LIST_ID = By.id("rfModel");
private static final By POSTAL_CODE_ID = By.id("locationAddressV2");
private static final String MAKE = "BMW";
private static final String MODEL = "4 Series";
private static final String POSTAL_CODE = "V6L 2Y3";
@Before
public void setUp() {
driver = new ChromeDriver();
driver.manage().window().maximize();
wait = new WebDriverWait(driver, Duration.ofSeconds(30));
}
@After
public void tearDown() {
driver.quit();
}
@Test
public void searchReturnsResultsTest() {
openHomePage();
Assert.assertEquals(getHomePageUrl(), URL);
selectMake(MAKE);
selectMake(MODEL);
typePostalCode(POSTAL_CODE);
executeSearch();
Assert.assertTrue(getResultsPageTitle().contains(MAKE));
Assert.assertTrue(getResultsPageTitle().contains(MODEL));
}
public void openHomePage() {
driver.get(URL);
}
public String getHomePageUrl() {
return driver.getCurrentUrl();
}
public String getResultsPageTitle() {
return driver.getTitle();
}
public void typePostalCode(String postalCode) {
driver.findElement(POSTAL_CODE_ID).sendKeys(postalCode);
}
public void executeSearch() {
driver.findElement(POSTAL_CODE_ID).sendKeys(Keys.ENTER);
}
public void selectModel(String model) {
Select modelList = getModelList();
modelList.selectByValue(model);
}
public Select getModelList() {
wait.until(ExpectedConditions.elementToBeClickable(MODEL_LIST_ID));
WebElement modelElement = driver.findElement(MODEL_LIST_ID);
return new Select(modelElement);
}
public void selectMake(String make) {
Select makeList = getMakeList();
makeList.selectByValue(make);
}
public Select getMakeList() {
WebElement makeElement = driver.findElement(MAKE_LIST_ID);
return new Select(makeElement);
}
}
It implements the following test case for the home page of autotrader.ca:
open home page
check that the home page url is correct
select a make
select a model that corresponds to the selected make
type a postal code
execute the search
check that the results page title contains the selected make
check that the results page title contains the selected model
The test has a problem.
It always searches using the same make, model and postal code.
This makes it static as it does the same thing all the time.
We can improve the test by changing it so that it searches all the time for a different make and model.
The test selects a make from the make dropdown list by using the MAKE constant.
It also selects a model from the model dropdown list by using the MODEL constant:
private static final String MAKE = "BMW";
private static final String MODEL = "4 Series";
Instead of using these constants, we should select a random make from the make dropdown list and a random model (that corresponds to the make) from the model dropdown list.
The random make is created by the following 2 methods:
public String getRandomMake() {
Select makeList = getMakeList();
return getRandomListOption(makeList);
}
private String getRandomListOption(Select list) {
List<WebElement> options = list.getOptions();
Random random = new Random();
int n = random.nextInt(options.size());
return options.get(n).getText();
}
getRandomMake() gets the make list and passes it to the getRandomListOption() method.
getRandomListOption() does the following:
gets all options of the list passed as parameter
generates a random integer value that is between 0 and the list option count
selects the list option with the index equal to the random integer value
'gets the text of the list option
returns the text of the list option
The random model is created the same way:
public String getRandomModel() {
Select modelList = getModelList();
return getRandomListOption(modelList);
}
public Select getModelList() {
wait.until(ExpectedConditions.elementToBeClickable(modelListId));
WebElement modelElement = driver.findElement(modelListId);
return new Select(modelElement);
}
The test is able now to search using a random make and model:
@Test
public void searchReturnsResultsAfterSelectingRandomMakeTest() {
openHomePage();
Assert.assertEquals(getHomePageUrl(), URL);
String make = getRandomMake();
selectMake(make);
String model = getRandomModel();
selectModel(model);
typePostalCode(POSTAL_CODE);
executeSearch();
Assert.assertTrue(getResultsPageTitle().contains(make));
Assert.assertTrue(getResultsPageTitle().contains(model));
}
In summary, when the test makes selections in list boxes, you do not have to select the same values all the time.
Instead, use the approach presented above to make random selections.