Test Overview
This test automates login functionality using a hybrid framework combining data-driven and keyword-driven approaches. It verifies successful login by checking the presence of a logout button.
This test automates login functionality using a hybrid framework combining data-driven and keyword-driven approaches. It verifies successful login by checking the presence of a logout button.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class HybridLoginTest { WebDriver driver; @BeforeClass public void setup() { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); driver = new ChromeDriver(); driver.manage().window().maximize(); } @DataProvider(name = "loginData") public Object[][] loginData() { return new Object[][] { {"user1", "pass1"}, {"user2", "pass2"} }; } @Test(dataProvider = "loginData") public void testLogin(String username, String password) { driver.get("https://example.com/login"); // Keyword-driven steps performAction("enterText", "id=username", username); performAction("enterText", "id=password", password); performAction("click", "id=loginBtn", null); // Assertion WebElement logoutBtn = driver.findElement(By.id("logoutBtn")); Assert.assertTrue(logoutBtn.isDisplayed(), "Logout button should be visible after login"); } public void performAction(String action, String locator, String value) { By byLocator = getBy(locator); WebElement element = driver.findElement(byLocator); switch (action) { case "enterText": element.sendKeys(value); break; case "click": element.click(); break; default: throw new IllegalArgumentException("Invalid action: " + action); } } public By getBy(String locator) { String[] parts = locator.split("=", 2); switch (parts[0]) { case "id": return By.id(parts[1]); case "name": return By.name(parts[1]); case "xpath": return By.xpath(parts[1]); default: throw new IllegalArgumentException("Invalid locator type: " + parts[0]); } } @AfterClass public void teardown() { if (driver != null) { driver.quit(); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test setup: ChromeDriver initialized and browser window maximized | Chrome browser opened, ready for navigation | - | PASS |
| 2 | Navigate to https://example.com/login | Login page loaded with username, password fields and login button | Page URL is correct | PASS |
| 3 | Find username field by id and enter 'user1' | Username input filled with 'user1' | Username field contains 'user1' | PASS |
| 4 | Find password field by id and enter 'pass1' | Password input filled with 'pass1' | Password field contains 'pass1' | PASS |
| 5 | Find login button by id and click | Login form submitted, page loading user dashboard | - | PASS |
| 6 | Find logout button by id to verify login success | Logout button visible on dashboard page | Logout button is displayed | PASS |
| 7 | Repeat steps 2-6 for second data set: username 'user2', password 'pass2' | Second login attempt successful, logout button visible | Logout button is displayed | PASS |
| 8 | Test teardown: Close browser and quit driver | Browser closed, WebDriver session ended | - | PASS |