Table of Contents
4.1 Introduction to Test Automation Frameworks
A Test Automation Framework is a structured approach to writing and organizing automated test cases. Frameworks simplify the process of writing and maintaining scripts, enabling testers to focus more on test logic rather than on the technicalities of automation. They ensure code reusability, reduce maintenance, and improve efficiency. A good framework should be scalable, and flexible, and should produce detailed reports of test execution.
Common features of test automation frameworks include:
- Code reusability
- Modular design
- Reporting and logging
- Integration with build tools like Jenkins or Maven
Frameworks help bridge the gap between testing requirements and the complexity of automated testing, offering a standardized method to handle repetitive tasks like running tests, reporting errors, and managing test data.
4.2 Types of Automation Frameworks
There are several types of automation frameworks, each suited to specific testing needs and workflows. The most popular ones include:
4.2.1 Data-Driven Framework
In a Data-Driven Framework, test data is separated from test scripts, allowing the same test script to be executed with different sets of input data. This is useful when you need to test an application with various input combinations without rewriting the script for each combination.
- How it works: Test data is stored externally in files such as Excel sheets, CSV files, or databases. The script reads the data from the external source, executes the test cases, and returns the result.
Example: Imagine testing a login form where you need to test multiple combinations of usernames and passwords. Instead of writing separate test cases, you can store the username/password pairs in an Excel sheet and loop through them in a single script.
4.2.2 Keyword-Driven Framework
A Keyword-Driven Framework separates test automation into two parts: keywords and the actual test logic. Keywords represent high-level actions like “click,” “input text,” or “verify element.” Each keyword is associated with specific steps to perform a particular task.
- How it works: Testers write the keywords in a spreadsheet or XML file, while the underlying code maps the keywords to specific Selenium commands.
Example: For a login test, the keywords might be “Enter username,” “Enter password,” and “Click login button.” These keywords correspond to functions in your code that interact with the web elements.
4.2.3 Hybrid Framework
A Hybrid Framework combines the features of multiple frameworks, such as Data-Driven and Keyword-Driven frameworks, to leverage the best of both worlds. This provides flexibility, enabling testers to use the most appropriate approach for different types of tests.
- How it works: For example, you might use a data-driven approach to provide input values and a keyword-driven approach to define the actions. The hybrid framework can be highly customizable to suit complex testing needs.
4.2.4 Behavior-Driven Development (BDD) Framework
Behavior-Driven Development (BDD) frameworks focus on creating tests that are readable by non-technical stakeholders, including business analysts and product managers. It uses natural language constructs like “Given,” “When,” and “Then” to describe test scenarios, making it easy for anyone to understand.
- How it works: The most popular BDD framework is Cucumber. Test scenarios are written in Gherkin syntax, and then mapped to the underlying code that implements the test logic.
Example: A login test case would be written in Gherkin syntax as:
Given I am on the login page
When I enter my username and password
Then I should be logged in successfully
4.3 Setting Up TestNG Framework
TestNG is a powerful testing framework for Java, designed to simplify a wide range of testing needs, including unit, functional, and integration testing. TestNG offers features like parallel test execution, setting priorities for tests, and generating detailed HTML reports.
Steps to Set Up TestNG:
- Install TestNG in Eclipse:
- Open Eclipse and go to the “Eclipse Marketplace.”
- Search for “TestNG” and install the plugin.
- Restart Eclipse after installation.
- Create a TestNG Class:
- In your project, right-click and choose New > TestNG Class.
- Add your test methods and annotations like @Test, @BeforeMethod, @AfterMethod.
- Run TestNG Test:
- Right-click the class and select Run As > TestNG Test.
Sample TestNG Code:
import org.testng.annotations.Test;
public class ExampleTest {
@Test
public void testLogin() {
System.out.println("Login test case");
}
}
4.4 Building a Data-Driven Framework with Excel
In a Data-Driven framework, we use external data sources like Excel to supply test inputs. Let’s build a simple data-driven framework using Apache POI, a popular Java library to read and write Excel files.
Steps to Build:
- Add Apache POI JAR files to your project.
- Create a utility class to read data from Excel:
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
public class ExcelUtils {
public static Object[][] getExcelData(String filePath, String sheetName) throws Exception {
FileInputStream file = new FileInputStream(filePath);
XSSFWorkbook workbook = new XSSFWorkbook(file);
Sheet sheet = workbook.getSheet(sheetName);
int rowCount = sheet.getPhysicalNumberOfRows();
int colCount = sheet.getRow(0).getLastCellNum();
Object[][] data = new Object[rowCount - 1][colCount];
for (int i = 1; i < rowCount; i++) {
Row row = sheet.getRow(i);
for (int j = 0; j < colCount; j++) {
data[i - 1][j] = row.getCell(j).toString();
}
}
return data;
}
}
3. Write test cases using the data from Excel:
@Test(dataProvider = "loginData")
public void testLogin(String username, String password) {
driver.findElement(By.id("username")).sendKeys(username);
driver.findElement(By.id("password")).sendKeys(password);
driver.findElement(By.id("loginButton")).click();
}
@DataProvider(name = "loginData")
public Object[][] getData() throws Exception {
return ExcelUtils.getExcelData("path/to/excel/file.xlsx", "Sheet1");
}
4.5 Building a Hybrid Framework
A Hybrid Framework combines the features of both Data-Driven and Keyword-Driven frameworks. It is more versatile and can handle more complex test automation needs.
Building a Hybrid Framework:
- Keyword Handling: Use keywords to define actions, and combine this with data from external sources.
- Excel or XML for Keywords: Store keywords in an Excel sheet or XML file.
- Data Handling: Use data-driven techniques to supply input values.
This framework may involve creating utility classes to read both keywords and data, and then executing the corresponding Selenium WebDriver commands.
4.6 Integrating Cucumber with Selenium for BDD
Cucumber is a BDD tool that allows writing test scenarios in a natural language using Gherkin syntax. Cucumber can be integrated with Selenium to drive web browsers through behavior-driven tests.
Steps to Integrate Cucumber:
- Add Cucumber Dependencies: Add Cucumber and Selenium JAR files to your project.
- Create a Feature File: Write your scenarios using Gherkin syntax:
Feature: Login
Scenario: Successful login
Given I am on the login page
When I enter valid credentials
Then I should be redirected to the dashboard
3. Step Definitions: Map each Gherkin step to a Java method:
@Given("^I am on the login page$")
public void navigateToLoginPage() {
driver.get("https://example.com/login");
}
@When("^I enter valid credentials$")
public void enterCredentials() {
driver.findElement(By.id("username")).sendKeys("user");
driver.findElement(By.id("password")).sendKeys("password");
}
@Then("^I should be redirected to the dashboard$")
public void verifyDashboard() {
assertTrue(driver.getCurrentUrl().contains("dashboard"));
}
4. Run the Test: Execute the Cucumber test runner.
4.7 Implementing Page Object Model (POM)
The Page Object Model (POM) is a design pattern used in Selenium to maintain clean and modular code. In POM, each page of the application is represented by a separate class, encapsulating the actions that can be performed on that page.
Example:
public class LoginPage {
WebDriver driver;
@FindBy(id="username")
WebElement username;
@FindBy(id="password")
WebElement password;
@FindBy(id="loginButton")
WebElement loginButton;
public LoginPage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
public void login(String uname, String pwd) {
username.sendKeys(uname);
password.sendKeys(pwd);
loginButton.click();
}
}
4.8 Reporting and Logging in Automation Frameworks
Reporting and Logging are crucial aspects of any test automation framework, as they provide insights into test execution and help debug issues. You can use tools like ExtentReports for reporting and Log4j for logging.
Example of ExtentReports:
ExtentReports report = new ExtentReports("path/to/report.html");
ExtentTest test = report.startTest("Login Test");
test.log(LogStatus.PASS, "Login successful");
report.endTest(test);
report.flush();
Example of Log4j for Logging:
Logger logger = Logger.getLogger("MyLog");
logger.info("Test execution started");
Conclusion
A well-structured Test Automation Framework improves code reusability, maintainability, and scalability. Depending on the complexity of the project, you can choose among Data-Driven, Keyword-Driven, Hybrid, or BDD frameworks. Proper implementation of these frameworks with tools like TestNG, Cucumber, and reporting mechanisms like ExtentReports ensures robust, maintainable, and efficient automated test suites.
To learn more about SDET Click here.
Great content all points cover at one place..Thanks🙌