Master test automation fundamentals and Selenium WebDriver.
Master test automation fundamentals and Selenium WebDriver.
Understand the fundamentals of test automation and when to use it
Content by: Paras Dadhania
Software Testing & QA Specialist
Test automation is the use of software tools to execute tests automatically, compare actual results with expected results, and report the outcomes. It helps reduce manual effort and increases test coverage.
// Good candidates for automation
const automationCriteria = {
"Repeatable Tests": [
"Regression tests",
"Smoke tests",
"Sanity tests",
"Performance tests"
],
"High Volume Tests": [
"Load testing",
"Stress testing",
"Data validation tests",
"API tests"
],
"Critical Path Tests": [
"Login functionality",
"Payment processing",
"User registration",
"Core business logic"
]
};
// Tests NOT suitable for automation
const manualTestCases = [
"Usability testing",
"Exploratory testing",
"Ad-hoc testing",
"One-time tests",
"Tests requiring human judgment"
];
Test your understanding of this topic:
Learn Selenium WebDriver fundamentals for web application testing
Content by: Yash Sanghavi
Software Testing & QA Specialist
// Maven Dependencies
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.15.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.8.0</version>
</dependency>
</dependencies>
// Basic WebDriver Setup
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class WebDriverSetup {
public static void main(String[] args) {
// Set Chrome driver path
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Chrome options
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
options.addArguments("--disable-notifications");
// Initialize WebDriver
WebDriver driver = new ChromeDriver(options);
// Navigate to website
driver.get("https://example.com");
// Close browser
driver.quit();
}
}
// Navigation Commands
driver.get("https://example.com"); // Navigate to URL
driver.navigate().to("https://google.com"); // Navigate to URL
driver.navigate().back(); // Go back
driver.navigate().forward(); // Go forward
driver.navigate().refresh(); // Refresh page
// Window Commands
driver.manage().window().maximize(); // Maximize window
driver.manage().window().minimize(); // Minimize window
driver.manage().window().setSize(new Dimension(800, 600)); // Set size
// Page Commands
String title = driver.getTitle(); // Get page title
String url = driver.getCurrentUrl(); // Get current URL
String pageSource = driver.getPageSource(); // Get page source
// Element Finding
WebElement element = driver.findElement(By.id("username"));
WebElement element2 = driver.findElement(By.className("btn"));
WebElement element3 = driver.findElement(By.xpath("//input[@name='email']"));
// Element Actions
element.click(); // Click element
element.sendKeys("text"); // Type text
element.clear(); // Clear text
element.getText(); // Get text
element.isDisplayed(); // Check if displayed
element.isEnabled(); // Check if enabled
Test your understanding of this topic:
Learn how to design robust and maintainable test automation frameworks
Content by: Paras Dadhania
Software Testing & QA Specialist
// Project Structure
src/
āāā main/
ā āāā java/
ā ā āāā pages/ // Page Object classes
ā ā āāā utils/ // Utility classes
ā ā āāā config/ // Configuration
ā ā āāā drivers/ // WebDriver setup
ā āāā resources/
ā āāā testdata/ // Test data files
ā āāā config/ // Property files
āāā test/
āāā java/
ā āāā tests/ // Test classes
ā āāā listeners/ // Test listeners
āāā resources/
āāā testng.xml // TestNG configuration
// Base Test Class
public class BaseTest {
protected WebDriver driver;
protected Properties config;
@BeforeMethod
public void setUp() {
config = ConfigManager.loadConfig();
driver = WebDriverManager.getDriver(config.getProperty("browser"));
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@AfterMethod
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
// Configuration Manager
public class ConfigManager {
private static Properties config;
public static Properties loadConfig() {
if (config == null) {
config = new Properties();
try {
FileInputStream fis = new FileInputStream("src/main/resources/config/config.properties");
config.load(fis);
} catch (IOException e) {
e.printStackTrace();
}
}
return config;
}
}
Test your understanding of this topic:
Master Page Object Model pattern for maintainable and reusable test automation
Content by: Yash Sanghavi
Software Testing & QA Specialist
// Login Page Object
public class LoginPage {
private WebDriver driver;
// Page Elements
@FindBy(id = "username")
private WebElement usernameField;
@FindBy(id = "password")
private WebElement passwordField;
@FindBy(id = "loginButton")
private WebElement loginButton;
@FindBy(className = "error-message")
private WebElement errorMessage;
// Constructor
public LoginPage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
// Page Actions
public void enterUsername(String username) {
usernameField.clear();
usernameField.sendKeys(username);
}
public void enterPassword(String password) {
passwordField.clear();
passwordField.sendKeys(password);
}
public void clickLoginButton() {
loginButton.click();
}
public DashboardPage login(String username, String password) {
enterUsername(username);
enterPassword(password);
clickLoginButton();
return new DashboardPage(driver);
}
public String getErrorMessage() {
return errorMessage.getText();
}
public boolean isErrorMessageDisplayed() {
return errorMessage.isDisplayed();
}
}
// Dashboard Page Object
public class DashboardPage {
private WebDriver driver;
@FindBy(className = "welcome-message")
private WebElement welcomeMessage;
@FindBy(linkText = "Logout")
private WebElement logoutLink;
public DashboardPage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
public String getWelcomeMessage() {
return welcomeMessage.getText();
}
public LoginPage logout() {
logoutLink.click();
return new LoginPage(driver);
}
}
// Test Class using Page Objects
public class LoginTest extends BaseTest {
@Test
public void testValidLogin() {
LoginPage loginPage = new LoginPage(driver);
DashboardPage dashboardPage = loginPage.login("testuser", "password123");
Assert.assertTrue(dashboardPage.getWelcomeMessage().contains("Welcome"));
}
@Test
public void testInvalidLogin() {
LoginPage loginPage = new LoginPage(driver);
loginPage.enterUsername("invaliduser");
loginPage.enterPassword("wrongpassword");
loginPage.clickLoginButton();
Assert.assertTrue(loginPage.isErrorMessageDisplayed());
Assert.assertEquals(loginPage.getErrorMessage(), "Invalid credentials");
}
}
Test your understanding of this topic:
Learn data-driven testing techniques to test with multiple data sets
Content by: Paras Dadhania
Software Testing & QA Specialist
// Test Data Provider
@DataProvider(name = "loginData")
public Object[][] getLoginData() {
return new Object[][] {
{"validuser", "validpass", true, "Login successful"},
{"invaliduser", "validpass", false, "Invalid username"},
{"validuser", "invalidpass", false, "Invalid password"},
{"", "validpass", false, "Username required"},
{"validuser", "", false, "Password required"}
};
}
// CSV Data Provider
@DataProvider(name = "csvData")
public Object[][] getCSVData() throws IOException {
List<Object[]> data = new ArrayList<>();
BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/testdata/login_data.csv"));
String line;
while ((line = reader.readLine()) != null) {
String[] values = line.split(",");
data.add(new Object[]{values[0], values[1], Boolean.parseBoolean(values[2]), values[3]});
}
reader.close();
return data.toArray(new Object[0][]);
}
// Excel Data Provider
@DataProvider(name = "excelData")
public Object[][] getExcelData() throws IOException {
FileInputStream fis = new FileInputStream("src/test/resources/testdata/test_data.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheet("LoginData");
int rowCount = sheet.getLastRowNum();
int colCount = sheet.getRow(0).getLastCellNum();
Object[][] data = new Object[rowCount][colCount];
for (int i = 1; i <= rowCount; i++) {
XSSFRow row = sheet.getRow(i);
for (int j = 0; j < colCount; j++) {
data[i-1][j] = row.getCell(j).getStringCellValue();
}
}
workbook.close();
fis.close();
return data;
}
// Test with Data Provider
@Test(dataProvider = "loginData")
public void testLoginWithData(String username, String password, boolean expectedResult, String expectedMessage) {
LoginPage loginPage = new LoginPage(driver);
loginPage.enterUsername(username);
loginPage.enterPassword(password);
loginPage.clickLoginButton();
if (expectedResult) {
DashboardPage dashboardPage = new DashboardPage(driver);
Assert.assertTrue(dashboardPage.getWelcomeMessage().contains("Welcome"));
} else {
Assert.assertTrue(loginPage.isErrorMessageDisplayed());
Assert.assertEquals(loginPage.getErrorMessage(), expectedMessage);
}
}
// JSON Data Provider
@DataProvider(name = "jsonData")
public Object[][] getJSONData() throws IOException {
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(new File("src/test/resources/testdata/test_data.json"));
List<Object[]> data = new ArrayList<>();
for (JsonNode node : jsonNode) {
data.add(new Object[]{
node.get("username").asText(),
node.get("password").asText(),
node.get("expectedResult").asBoolean(),
node.get("expectedMessage").asText()
});
}
return data.toArray(new Object[0][]);
}
Test your understanding of this topic:
Continue your learning journey and master the next set of concepts.
Continue to Module 5