Keyword Driven Framework


Keyword Driven Framework Interview with follow-up questions

1. What is a Keyword Driven Framework in Selenium?

A Keyword Driven Framework (also called Action Word Framework) externalizes test logic into keywords—action words like click, type, navigate, verifyText—stored in a data file (typically an Excel spreadsheet or JSON file). A driver script reads the keywords and dispatches to corresponding Java methods. Non-technical stakeholders can define test cases by writing sequences of keywords and targets without touching code.

How it works

A test case in the spreadsheet might look like:

TestCase Keyword Target Value
Login_Test navigate https://example.com/login
Login_Test type id=username [email protected]
Login_Test type id=password secret
Login_Test click css=button[type='submit']
Login_Test verifyText id=welcome Welcome, user

The driver script reads each row and calls the matching method:

switch (keyword) {
    case "navigate": driver.get(value); break;
    case "type": driver.findElement(getLocator(target)).sendKeys(value); break;
    case "click": driver.findElement(getLocator(target)).click(); break;
    case "verifyText": assertEquals(driver.findElement(getLocator(target)).getText(), value); break;
}

Benefits

  • Business analysts or manual testers can author test cases without writing code.
  • New test cases require only new rows in the data file, not new code.
  • Keyword library methods are reusable across all test cases.

When to use it Keyword-driven is most valuable when the team includes non-developers who need to define test scenarios, or when you need a large volume of test cases maintained outside the codebase. For fully developer-owned suites, simpler frameworks (POM + TestNG) usually suffice.

↑ Back to top

Follow-up 1

Can you explain how it works?

In a Keyword Driven Framework, test scripts are written using keywords or actions that represent specific test steps. These keywords are mapped to corresponding functions or methods in the framework. Test data is stored separately, usually in a spreadsheet or a data file. The framework reads the test data and executes the corresponding keywords, performing the desired actions on the application under test. This approach allows for easy modification and reusability of test scripts.

Follow-up 2

What are the advantages of using a Keyword Driven Framework?

There are several advantages of using a Keyword Driven Framework in Selenium:

  1. Reusability: Test scripts can be easily reused across different test cases, reducing duplication of effort.
  2. Maintainability: Test scripts are separated from test data, making it easier to update and maintain them.
  3. Modularity: Test scripts are written using keywords, making them modular and easy to understand.
  4. Collaboration: Testers with different levels of technical expertise can contribute to the test automation effort by writing test scripts using keywords.
  5. Scalability: The framework can be easily extended to accommodate new test cases or functionalities.
  6. Data-driven testing: Test data can be easily managed and modified separately from the test scripts.

Follow-up 3

Can you give an example of a Keyword Driven Framework?

Sure! Here's an example of a Keyword Driven Framework in Selenium:

Test Script:

Open Browser
Navigate to URL
Enter Username
Enter Password
Click Login Button
Verify Welcome Message
Close Browser

Keywords:

Open Browser
Navigate to URL
Enter Username
Enter Password
Click Login Button
Verify Welcome Message
Close Browser

In this example, each keyword represents a specific action or step in the test script. The framework maps these keywords to corresponding functions or methods that perform the desired actions on the application under test.

Follow-up 4

What are the challenges in implementing a Keyword Driven Framework?

Implementing a Keyword Driven Framework in Selenium can come with some challenges:

  1. Initial setup: Setting up the framework and defining the keywords and their corresponding functions can be time-consuming.
  2. Keyword coverage: Ensuring that all the required keywords are available and cover all the necessary test steps can be a challenge.
  3. Maintenance: As the application under test evolves, the keywords and their corresponding functions may need to be updated or modified.
  4. Data management: Managing and organizing test data in a separate file or spreadsheet can be complex.
  5. Training: Testers may require training to understand and use the framework effectively.
  6. Debugging: Debugging test scripts can be more challenging in a Keyword Driven Framework due to the abstraction of test steps into keywords.

2. How does a Keyword Driven Framework differ from other frameworks in Selenium?

The three common Selenium framework styles differ in where test logic, test data, and test actions live:

Data-Driven Framework

  • Test logic (steps) is fixed in code.
  • Only the test data varies—same test runs multiple times with different inputs.
  • Implemented with @DataProvider (TestNG) or @ParameterizedTest (JUnit 5).
  • Best for: login tests with many credential combinations, form validation with various input values.

Keyword-Driven Framework

  • Both test actions and test data are externalized to a file (spreadsheet/JSON).
  • Each row specifies a keyword (action) and its parameters.
  • A driver script maps keywords to method calls.
  • Best for: teams where non-developers define test cases, or when test cases change frequently without code changes.

Hybrid Framework

  • Combines data-driven and keyword-driven approaches.
  • Keywords define the actions; data files supply the inputs for those actions.
  • Provides maximum flexibility: test authors choose keywords, QA engineers or BAs supply data.
  • Best for: large enterprise suites with diverse teams.

Key difference interviewers probe

Actions fixed in code Actions in data file
Data in code Hardcoded (not a framework style) N/A
Data in file Data-Driven Keyword-Driven / Hybrid

Keyword-driven is more complex to build and maintain than data-driven, so it is only worth the investment when the stakeholder audience genuinely needs to write test cases without programmer help.

↑ Back to top

Follow-up 1

Can you compare it with the Data Driven Framework?

The Keyword Driven Framework and the Data Driven Framework are both popular approaches for test automation in Selenium, but they have some key differences.

In the Keyword Driven Framework, test cases are written using keywords or action words that represent the actions to be performed on the application under test. These keywords are stored in a separate data source, such as an Excel sheet or a CSV file, along with the test data. The framework uses these keywords to execute the test cases. This approach provides a higher level of abstraction and flexibility, making it easier to create and maintain test scripts.

On the other hand, the Data Driven Framework separates the test data from the test scripts. The test data is stored in a separate data source, such as an Excel sheet or a database, and the test scripts are designed to read the test data and execute the test cases accordingly. This approach allows for the execution of multiple test cases with different input data, making it suitable for scenarios where the same test case needs to be executed with different data sets.

Overall, the Keyword Driven Framework focuses on using keywords to represent actions, while the Data Driven Framework focuses on separating test data from test scripts.

Follow-up 2

What makes it more suitable for certain testing scenarios?

The Keyword Driven Framework is more suitable for certain testing scenarios due to its higher level of abstraction and flexibility.

One scenario where the Keyword Driven Framework is particularly useful is when the test cases need to be written by non-technical team members, such as business analysts or domain experts. These team members can easily understand and write test cases using keywords, without having to write complex code. This allows for better collaboration between technical and non-technical team members.

Another scenario where the Keyword Driven Framework is beneficial is when the application under test has a complex user interface or requires multiple steps to perform a single action. By using keywords to represent these actions, the test scripts become more readable and maintainable. Additionally, if there are any changes in the application, only the keywords need to be updated, rather than modifying the entire test script.

Overall, the Keyword Driven Framework provides a more user-friendly approach to test automation, making it suitable for scenarios where non-technical team members are involved or the application has a complex user interface.

3. What are the key components of a Keyword Driven Framework?

A Keyword Driven Framework has five main components that work together:

1. Keyword Library A set of Java methods (or functions in other languages), one per action word. Each method wraps WebDriver calls:

public void click(String locator) {
    driver.findElement(parseLocator(locator)).click();
}
public void type(String locator, String value) {
    driver.findElement(parseLocator(locator)).sendKeys(value);
}
public void verifyText(String locator, String expected) {
    assertEquals(driver.findElement(parseLocator(locator)).getText(), expected);
}

2. Test Data / Test Case File An Excel spreadsheet, CSV, or JSON file that lists test cases as sequences of keywords, targets, and values. This is what non-developers author.

3. Driver Script (Executor) Reads the test case file row by row and invokes the corresponding keyword library method. Can use a switch statement, a Map lookup, or Java reflection.

4. Element Repository A centralized store of element locators (often a properties file or a separate sheet in the spreadsheet), mapping logical names to locator strings. Keeps locators out of the test data file and centralizes maintenance.

5. Reporting Mechanism Logs the result (pass/fail/error) of each keyword execution with timestamps, screenshots on failure, and a summary report. Tools like Allure, ExtentReports, or TestNG's built-in reporter are commonly used.

These components together allow test cases to be authored in a spreadsheet while keeping executable code organized and maintainable in the keyword library.

↑ Back to top

Follow-up 1

Can you explain the role of each component?

Sure!

  1. Test Cases: Test cases define the steps or actions that need to be performed to test a specific functionality or feature of the application.

  2. Keywords: Keywords represent the actions or operations that are performed on the application. Each keyword is associated with a specific functionality or action.

  3. Test Data: Test data provides the input values or data that are required for executing the test cases. Test data can be different for each test case.

  4. Test Scripts: Test scripts are responsible for executing the test cases using the keywords and test data. Test scripts call the appropriate keywords with the required test data.

  5. Test Results: Test results represent the outcomes or results of the test execution. Test results can be used to determine whether the application is functioning correctly or not.

Follow-up 2

How do these components interact with each other?

The components of a Keyword Driven Framework interact with each other in the following way:

  1. Test Cases: Test cases are associated with specific keywords and test data. They define the sequence of actions that need to be performed.

  2. Keywords: Keywords are the building blocks of the test cases. They represent the actions or operations that are performed on the application. Test cases call the appropriate keywords to perform the required actions.

  3. Test Data: Test data provides the input values or data that are required for executing the test cases. Test data is passed to the keywords during the test execution.

  4. Test Scripts: Test scripts are responsible for executing the test cases. They call the appropriate keywords with the required test data to perform the actions defined in the test cases.

  5. Test Results: Test results are generated during the test execution. They provide the outcomes or results of the test cases and can be used for reporting or analysis purposes.

4. How do you handle errors and exceptions in a Keyword Driven Framework?

Error handling in a Keyword Driven Framework must be explicit because the driver script executes arbitrary keywords dynamically. There is no compile-time guarantee that a keyword will succeed.

Wrap each keyword execution in try-catch

for (Row row : testCaseRows) {
    String keyword = row.getKeyword();
    String target  = row.getTarget();
    String value   = row.getValue();
    try {
        keywordLibrary.execute(keyword, target, value);
        report.logPass(keyword, target);
    } catch (Exception e) {
        report.logFail(keyword, target, e.getMessage());
        takeScreenshot(keyword);
        if (isCritical(keyword)) break; // abort on critical failure
    }
}

Continue vs. abort on failure Mark certain keywords as critical (e.g., login, navigate). If a critical keyword fails, abort the test case—subsequent steps are meaningless. For non-critical keywords (e.g., a soft assertion), log the failure and continue to the next keyword.

Custom exception types Define a KeywordExecutionException that wraps the root cause. This makes error messages in reports consistent and parseable.

Screenshots on failure Capture a screenshot in the catch block using TakesScreenshot:

File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

Attach the screenshot to the report entry for that keyword.

Logging Use a structured logger (SLF4J + Logback) rather than System.out.println. Log the keyword name, target, value, and stack trace. In parallel runs, include the thread ID or test name in each log line to keep output readable.

Validation before execution Before executing, validate that the keyword exists in the library and that required parameters are not blank. This surfaces data-file authoring errors early with clear messages rather than obscure NullPointerExceptions at runtime.

↑ Back to top

Follow-up 1

Can you give an example of error handling in this framework?

Sure! Here's an example of error handling in a Keyword Driven Framework:

try:
    execute_keyword('login')
    execute_keyword('search', 'keyword')
    execute_keyword('click', 'result')
    execute_keyword('logout')
except Exception as e:
    log_error(e)
    take_screenshot()
    raise e

In this example, the keywords 'login', 'search', 'click', and 'logout' are executed one after the other. If any exception occurs during the execution of any keyword, it is caught in the except block. The error is logged, a screenshot is taken, and the exception is re-raised to halt the test execution.

Follow-up 2

What strategies can be used to minimize errors?

There are several strategies that can be used to minimize errors in a Keyword Driven Framework:

  1. Use meaningful and descriptive keyword names: By using clear and descriptive keyword names, it becomes easier to understand the purpose and functionality of each keyword. This reduces the chances of errors due to confusion or misunderstanding.

  2. Implement proper error handling: As mentioned earlier, using try-catch blocks to handle errors and exceptions is crucial. By handling errors gracefully, the framework can continue executing the remaining keywords even if an error occurs.

  3. Regularly review and update the keyword library: The keyword library should be regularly reviewed and updated to ensure that it is up-to-date and reflects the current state of the application. This helps in avoiding errors caused by outdated or incorrect keywords.

  4. Perform thorough testing: It is important to thoroughly test the keywords and the overall framework to identify and fix any potential errors. This includes both positive and negative testing scenarios.

  5. Maintain clear documentation: Clear and up-to-date documentation of the keywords, their parameters, and their expected behavior can help in minimizing errors. Testers can refer to the documentation to ensure that they are using the keywords correctly and understanding their intended functionality.

5. Can you explain how a Keyword Driven Framework can be implemented in Selenium?

A Keyword Driven Framework in Selenium maps action words from an external file to WebDriver method calls. Here is a concrete implementation sketch:

1. Define keywords in a Java map

public class KeywordLibrary {
    private WebDriver driver;
    private Map> keywords = new HashMap<>();

    public KeywordLibrary(WebDriver driver) {
        this.driver = driver;
        keywords.put("navigate", (target, value) -> driver.get(target));
        keywords.put("click",    (target, value) -> driver.findElement(locate(target)).click());
        keywords.put("type",     (target, value) -> driver.findElement(locate(target)).sendKeys(value));
        keywords.put("assertText", (target, value) ->
            assertEquals(driver.findElement(locate(target)).getText(), value));
    }

    public void execute(String keyword, String target, String value) {
        BiConsumer action = keywords.get(keyword.toLowerCase());
        if (action == null) throw new IllegalArgumentException("Unknown keyword: " + keyword);
        action.accept(target, value);
    }
}

2. Store test cases in Excel (Apache POI)

Keyword Target Value
navigate https://example.com/login
type id=username [email protected]
type id=password secret
click css=button[type='submit']
assertText id=welcome Welcome, user

3. Driver script reads and executes

Workbook wb = WorkbookFactory.create(new File("testcases.xlsx"));
Sheet sheet = wb.getSheetAt(0);
KeywordLibrary lib = new KeywordLibrary(driver);

for (Row row : sheet) {
    String keyword = row.getCell(0).getStringCellValue();
    String target  = row.getCell(1).getStringCellValue();
    String value   = row.getCell(2).getStringCellValue();
    lib.execute(keyword, target, value);
}

4. Report results Wrap each lib.execute() call in try-catch, log pass/fail, and capture screenshots on failure.

This architecture means new test cases require only new spreadsheet rows—no code changes—as long as the required keywords already exist in the library.

↑ Back to top

Follow-up 1

What tools or libraries would you use?

To implement a Keyword Driven Framework in Selenium, you can use the following tools or libraries:

  1. Selenium WebDriver: Selenium WebDriver is a powerful tool for automating web applications. It provides a rich set of APIs for interacting with web elements and performing actions on them.

  2. TestNG: TestNG is a testing framework for Java that provides advanced features like test parallelization, data-driven testing, and test configuration management. It can be used to organize and execute your test scripts.

  3. Apache POI: Apache POI is a Java library for reading and writing Microsoft Office file formats, such as Excel. It can be used to read test data from Excel files and populate them into your test scripts.

  4. Log4j: Log4j is a logging utility for Java that allows you to log messages at different levels of severity. It can be used to log the execution details of your test scripts.

These are just a few examples of the tools and libraries that can be used. Depending on your specific requirements, you may need to use additional tools or libraries.

Follow-up 2

Can you walk me through the steps of setting up this framework?

Sure! Here are the steps to set up a Keyword Driven Framework in Selenium:

  1. Identify the keywords: Start by identifying the keywords or actions that you want to use in your test scripts. These keywords can be simple actions like 'click', 'type', 'verify', etc.

  2. Create a keyword library: Create a library of keywords that maps each keyword to the corresponding Selenium action. For example, the keyword 'click' can be mapped to the Selenium command 'driver.findElement(By.xpath("xpath")).click();'.

  3. Define test data: Define the test data that will be used in your test scripts. This can include input values, expected results, etc.

  4. Create test scripts: Write test scripts using the keywords and test data. Each test script should be a sequence of keywords that represent the actions to be performed.

  5. Implement the framework: Implement the framework by creating a driver script that reads the test scripts, retrieves the keywords and test data, and executes the corresponding Selenium actions.

  6. Execute the tests: Execute the tests by running the driver script. The framework will read the test scripts, perform the actions, and validate the results.

By following these steps, you can set up a Keyword Driven Framework in Selenium and start automating your test cases.

Live mock interview

Mock interview: Keyword Driven Framework

Intermediate ~5 min Your own free AI key

Your voice and your AI key never touch our servers; the key stays in this browser and is sent only to Google. Only your round scores are saved to track progress.