Leaving abstraction out of the test layer can in time lead to duplication of the logic as well as having to spend more time than what is required to script new scenarios. This post will discuss how the test layer can be abstracted and decomposed using TestNG.
High-level Steps
- Start with designing and implementing test cases so that they are atomic
- Encapsulate the test layer by scripting the test steps and adding assertions required to validate the component. Then put a mechanism in place to read the test data from an object that can be passed from one test class to another as required.
- Finally, use the Factory feature in TestNG to compose the larger scripts using the test classes.
- Validate that the dropdown page loads the dropdown and that the values are as expected.
- Validate that the dropdown allows the user to select the option he wants to select.
Test Class 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dumiduh.elements; | |
import com.dumiduh.constants.Constants; | |
import com.dumiduh.function.DropDownPageFunctions; | |
import com.dumiduh.models.TestData; | |
import com.dumiduh.utils.JSONUtil; | |
import com.dumiduh.utils.TestBase; | |
import org.testng.Assert; | |
import org.testng.annotations.AfterClass; | |
import org.testng.annotations.BeforeClass; | |
import org.testng.annotations.Test; | |
/** | |
* The objective of this test class is to demonstrate how a dropdown maybe handled. | |
*/ | |
public class DropDownUsageTest extends TestBase { | |
@BeforeClass | |
public static void setup() { | |
instantiateDriver(); | |
driver.get(Constants.DROPDOWN_PAGE_URL); | |
} | |
@Test | |
public static void dropDownUsageTest() { | |
TestData testData = DropDownTest.data; | |
System.out.println("::\t"+ testData.getDropDownSelection()); | |
DropDownPageFunctions dropDownPageFunctions = new DropDownPageFunctions(driver); | |
dropDownPageFunctions.selectValueFromDropDown(testData.getDropDownSelection()); | |
//Asserts to see if the dropdown selection has been set. | |
Assert.assertTrue(dropDownPageFunctions.isTheGivenValueSelected(testData.getDropDownSelection())); | |
} | |
@AfterClass | |
public static void cleanUp(){ | |
driver.quit(); | |
} | |
} |
Test Class 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dumiduh.elements; | |
import com.dumiduh.function.DropDownPageFunctions; | |
import com.dumiduh.models.TestData; | |
import com.dumiduh.utils.JSONUtil; | |
import com.dumiduh.utils.TestBase; | |
import org.testng.Assert; | |
import org.testng.annotations.AfterClass; | |
import org.testng.annotations.BeforeClass; | |
import org.testng.annotations.Test; | |
import static com.dumiduh.constants.Constants.DROPDOWN_PAGE_URL; | |
public class DropDownPageElementsTest extends TestBase { | |
@BeforeClass | |
public static void setup() { | |
instantiateDriver(); | |
} | |
@Test | |
public static void dropDownPageElementTest() { | |
TestData data = DropDownTest.data; | |
driver.get(DROPDOWN_PAGE_URL); | |
DropDownPageFunctions dropdown = new DropDownPageFunctions(driver); | |
Assert.assertTrue(dropdown.isTheDropDownHeadingDisplayed()); | |
Assert.assertTrue(dropdown.isTheDropDownDisplayed()); | |
Assert.assertEquals(data.getNumberOfOptions(), dropdown.getTheListOfOptions() | |
.size()); | |
if (dropdown.getTheListOfOptions() | |
.containsAll(data.getListOfOptions())) { | |
Assert.assertTrue(true); | |
} | |
} | |
@AfterClass | |
public void cleanUp() { | |
DropDownTest.data.setDropDownSelection("Option 2"); | |
driver.quit(); | |
} | |
} |
Factory Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dumiduh.elements; | |
import com.dumiduh.models.TestData; | |
import com.dumiduh.utils.JSONUtil; | |
import org.testng.annotations.Factory; | |
public class DropDownTest { | |
static TestData data = JSONUtil.readAGivenTestDataItem("dropdownendtoend"); | |
@Factory | |
public static Object[] DropDownTest() { | |
return new Object[]{ | |
new DropDownPageElementsTest(), | |
new DropDownUsageTest()}; | |
} | |
} |
[1] - https://the-internet.herokuapp.com/dropdown
[repo] - https://github.com/handakumbura/SeleniumAutomationEmployerProfile/tree/feature/atomic_test_cases