This is a simple test that verifies, after a keyword search is executed, that the results are sorted correctly:
public class SearchTests {
private WebDriver driver;
private String[] testData = new String[] { "java",
"price-ascending",
"fr" ,
"sort-type=price-asc" };
@BeforeMethod
public void setUp() {
driver = new ChromeDriver();
}
@AfterMethod
public void tearDown() {
driver.quit();
}
@Test
public void results_Can_Be_Sorted_By_Price_Ascending_Test() {
HomePage homePage = new HomePage(driver);
homePage.open();
Assert.assertTrue(homePage.isDisplayed());
homePage.setLanguage(testData[2]);
ResultsPage resultsPage = homePage.search(testData[0]);
Assert.assertTrue(resultsPage.isDisplayed());
resultsPage.changeSortOrder(testData[1]);
Assert.assertTrue(resultsPage.areResultsSortedBy(testData[1]));
Assert.assertTrue(resultsPage.getUrl().contains(testData[3]));
}
}
The test class defines an array that stores all test data needed in the test:
search keyword
sort order
language
url sort-type parameter
Each element of the array is being used when needed by adding the proper index after the array name.
This is an incorrect use of an array.
Multiple things are wrong here.
An array should be used as a collection for multiple values that represent the same thing. Like an array of names. Or an array of products. Or an array of prices.
Since our array is of strings and since you can save any type of value in a string variable, we end up with an array that includes a keyword, a sort order, a url parameter and a language.
This is the first problem.
But, what happens if by mistake we change the elements order in the array?
Instead of
private String[] testData = new String[] { "java",
"price-ascending",
"fr" ,
"sort-type=price-asc" };
we have
private String[] testData = new String[] { "sort-type=price-asc",
"fr",
"price-ascending" ,
"java" };
The test code will no longer work as it assumes that
the keyword is the 1st array element
the sort order is the 2nd array element
the language is the 3rd element
the url parameter is the 4th element
This is the second problem.
Also, code like this is not exactly easy to understand:
resultsPage.changeSortOrder(testData[1]);
testData[1]? What is that?
Third problem :)
The solution is simple.
Get rid of the array and use constants for keyword, sort order, language and url parameter:
public class SearchTests {
private WebDriver driver;
private static final String KEYWORD = "java";
private static final String PRICE_ASCENDING_ORDER = "price-ascending";
private static final String LANGUAGE = "fr";
private static final String SORT_TYPE = "sort-type=price-asc";
@BeforeMethod
public void setUp() {
driver = new ChromeDriver();
}
@AfterMethod
public void tearDown() {
driver.quit();
}
@Test
public void results_Can_Be_Sorted_By_Price_Ascending_Test() {
HomePage homePage = new HomePage(driver);
homePage.open();
Assert.assertTrue(homePage.isDisplayed());
homePage.setLanguage(LANGUAGE);
ResultsPage resultsPage = homePage.search(KEYWORD);
Assert.assertTrue(resultsPage.isDisplayed());
resultsPage.changeSortOrder(PRICE_ASCENDING_ORDER);
Assert.assertTrue(resultsPage.areResultsSortedBy(PRICE_ASCENDING_ORDER));
Assert.assertTrue(resultsPage.getUrl().contains(SORT_TYPE));
}
}
One thing is certain.
The test code is much clearer than before.