Hybrid framework architecture in Selenium Java - Build an Automation Script
package tests; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import pages.LoginPage; import pages.DashboardPage; import java.time.Duration; public class LoginTest { private WebDriver driver; private WebDriverWait wait; private LoginPage loginPage; private DashboardPage dashboardPage; @BeforeClass public void setUp() { // Set path to chromedriver executable if needed driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); driver.manage().window().maximize(); loginPage = new LoginPage(driver, wait); dashboardPage = new DashboardPage(driver, wait); } @Test public void testValidLogin() { driver.get("https://example.com/login"); loginPage.enterUsername("testuser"); loginPage.enterPassword("Test@1234"); loginPage.clickLoginButton(); // Wait for dashboard page URL wait.until(ExpectedConditions.urlContains("/dashboard")); String currentUrl = driver.getCurrentUrl(); Assert.assertTrue(currentUrl.contains("/dashboard"), "URL does not contain /dashboard"); String welcomeText = dashboardPage.getWelcomeMessage(); Assert.assertEquals(welcomeText, "Welcome, testuser!", "Welcome message mismatch"); } @AfterClass public void tearDown() { if (driver != null) { driver.quit(); } } } // File: pages/LoginPage.java package pages; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.support.ui.ExpectedConditions; public class LoginPage { private WebDriver driver; private WebDriverWait wait; private By usernameField = By.id("username"); private By passwordField = By.id("password"); private By loginButton = By.id("loginBtn"); public LoginPage(WebDriver driver, WebDriverWait wait) { this.driver = driver; this.wait = wait; } public void enterUsername(String username) { wait.until(ExpectedConditions.visibilityOfElementLocated(usernameField)).clear(); driver.findElement(usernameField).sendKeys(username); } public void enterPassword(String password) { wait.until(ExpectedConditions.visibilityOfElementLocated(passwordField)).clear(); driver.findElement(passwordField).sendKeys(password); } public void clickLoginButton() { wait.until(ExpectedConditions.elementToBeClickable(loginButton)).click(); } } // File: pages/DashboardPage.java package pages; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.support.ui.ExpectedConditions; public class DashboardPage { private WebDriver driver; private WebDriverWait wait; private By welcomeMessage = By.id("welcomeMsg"); public DashboardPage(WebDriver driver, WebDriverWait wait) { this.driver = driver; this.wait = wait; } public String getWelcomeMessage() { return wait.until(ExpectedConditions.visibilityOfElementLocated(welcomeMessage)).getText(); } }
This test uses Selenium WebDriver with TestNG in a hybrid framework style.
Setup: The @BeforeClass method initializes the ChromeDriver, WebDriverWait, and page objects.
Page Objects: LoginPage and DashboardPage classes encapsulate element locators and actions, following the Page Object Model pattern.
Test: The test navigates to the login page, enters username and password, clicks login, waits for the dashboard URL, and asserts the URL and welcome message.
Teardown: The @AfterClass method closes the browser.
Explicit waits ensure elements are ready before interaction, improving reliability. Separating page logic from test logic makes maintenance easier.
Now add data-driven testing with 3 different sets of login credentials (valid and invalid) using TestNG data provider.
Practice
Hybrid framework in Selenium Java testing?Solution
Step 1: Understand Hybrid framework components
A Hybrid framework mixes keyword-driven and data-driven testing methods to leverage benefits of both.Step 2: Identify the advantage
This combination allows flexible test design and easier maintenance, improving test reuse and scalability.Final Answer:
It combines keyword-driven and data-driven approaches for better flexibility. -> Option AQuick Check:
Hybrid framework = combines keyword + data-driven [OK]
- Thinking hybrid uses only one testing approach
- Assuming hybrid requires no coding
- Believing hybrid limits browser support
Solution
Step 1: Recall data-driven testing principle
Data-driven testing stores test data externally to separate it from test logic.Step 2: Apply to Hybrid framework
Hybrid framework uses external files like Excel or CSV to manage test data for easy updates and reuse.Final Answer:
Store test data in external files like Excel or CSV. -> Option CQuick Check:
Test data external = Excel/CSV [OK]
- Hardcoding data inside scripts reduces flexibility
- Mixing test data with Selenium code causes maintenance issues
- Using random data breaks test repeatability
String keyword = "click";
if (keyword.equals("click")) {
driver.findElement(By.id("submitBtn")).click();
} else if (keyword.equals("input")) {
driver.findElement(By.id("nameField")).sendKeys("John");
}What will happen when this code runs?
Solution
Step 1: Check the keyword value
The variablekeywordis set to "click".Step 2: Follow the if condition
Sincekeyword.equals("click")is true, the code clicks the element with id "submitBtn".Final Answer:
The submit button will be clicked. -> Option DQuick Check:
Keyword = "click" triggers click action [OK]
- Confusing keyword values and actions
- Assuming input action runs when keyword is "click"
- Thinking locator is invalid without checking
NoSuchElementException. What is the likely cause?driver.findElement(By.name("username")).sendKeys("admin");Solution
Step 1: Understand NoSuchElementException meaning
This exception means Selenium cannot find the element using the given locator.Step 2: Analyze the locator usage
IfBy.name("username")does not match any element on the current page, the exception occurs.Final Answer:
The locatorBy.name("username")does not match any element on the page. -> Option BQuick Check:
NoSuchElementException = locator not found [OK]
- Blaming sendKeys syntax for element not found
- Ignoring driver initialization issues
- Assuming element disabled causes NoSuchElementException
Solution
Step 1: Understand Hybrid framework design goals
Hybrid framework combines keyword-driven (for actions) and data-driven (for test data) approaches.Step 2: Match design to Excel usage
Reading test steps (keywords) and data from Excel fits keyword-driven engine plus data-driven module.Final Answer:
Use a keyword-driven engine to interpret actions and a data-driven module to read test data from Excel. -> Option AQuick Check:
Hybrid = keyword engine + data-driven Excel [OK]
- Hardcoding all test steps reduces flexibility
- Ignoring keyword-driven part in Hybrid design
- Using random data breaks test consistency
Ready to prove it?
Pass the test and this pattern moves unknown
