Create a Test Automation Framework from scratch using Java, Selenium, TestNG, and the Page Object Model (POM) involves several steps. Here’s a guide to help you set up a robust framework:
Project Setup
- Create a Maven Project:
- Use an Integrated Development Environment (IDE) like IntelliJ IDEA or Eclipse.
- Create a new Maven project to manage dependencies easily.
Add Dependencies
Add necessary dependencies in the pom.xml file.
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.23.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.testng/testng --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.10.2</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-chrome-driver</artifactId> <version>4.23.0</version> </dependency>
Project Structure
Organize your project directories as follows:
Session 1: Basic Set for Automation
Page Object Model (POM)
Create Page Classes: Each application page will have a corresponding Page class.
Example: HomePage.java
package com.carparkingautomation.page; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.PageFactory; public class HomePage { WebDriver driver; public HomePage(WebDriver driver) { this.driver =driver; PageFactory.initElements(driver, this); } // Page Elements.. @FindBy(xpath = "//a[contains(text(), 'Car Parking Management System')]") private WebElement headerHeading; @FindBy(xpath = "//a[contains(text(), 'Home')]") private WebElement homeLink; @FindBy(xpath = "//a[contains(text(), 'SignUp')]") private WebElement signUpLink; @FindBy(xpath = "//a[contains(text(), 'Login')]") private WebElement loginLink; @FindBy(xpath = "//h1[contains(text(), 'Student Car Parking Booking System')]") private WebElement bodyText; // Method.. public void ClickToHomePage() { homeLink.click(); } public String bodyHeadingText() { return bodyText.getText(); } public String homeLinkText() { return homeLink.getText(); } public String signUpLinkText() { return signUpLink.getText(); } public String loginLinkText() { return loginLink.getText(); } public String headerHeadingText() { return headerHeading.getText(); } }
Base Test Class
Create a Base Test Class: This class will handle common setup and teardown methods.
Example: BaseTest.java
package com.carparkingautomation.base; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; public class BaseTest { public WebDriver driver; @BeforeClass public void setup() { System.setProperty("webdriver.chrome.driver", "Driver_Path"); driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("http://localhost:8081/CarParkingMgt/"); } @AfterClass public void teardown() { if(driver != null) { driver.quit(); } } }
Test Classes
Create Test Classes: Write test methods using TestNG annotations.
Example: HomeTest.java
package com.carparkingautomation.test; import org.testng.Assert; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import com.carparkingautomation.base.BaseTest; import com.carparkingautomation.page.HomePage; public class HomeTest extends BaseTest { public HomePage page; @BeforeMethod public void beforeMethod() { page = new HomePage(driver); } @Test public void navToHome() { Assert.assertTrue(driver.getTitle().contains("Home")); try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Test public void VerifyHeaderHeading() { String text = page.headerHeadingText(); System.out.println("Page heading is: "+text); Assert.assertTrue(text.contains("Car Parking Management System")); } @Test public void verifyParimaryHeaderText() { Assert.assertTrue(page.homeLinkText().contains("Home")); Assert.assertTrue(page.signUpLinkText().contains("SignUp")); Assert.assertTrue(page.loginLinkText().contains("Login")); } @Test public void verifybodyHeadingText() { Assert.assertTrue(page.bodyHeadingText().contains("Student Car Parking Booking System")); } }
Session 2: Automate Home Page
Write automation scripts to automate the below testcases from the home page.
Testcase 1:
– Navigate to the home page.
– Verify the page title name as Home.
Testcase 2:
– Navigate to the home page.
– Verify header tiles as “Car Parking Management System”.
Testcase 3:
– Navigate to the home page.
– Verify the primary header menu link for Home, SignUp, and Login.
Testcase 4:
– Navigate to the home page.
– Verify Page Body Text as “Student Car Parking Booking System”.
Session 3: Automate Login Page
Write automation scripts to automate the below testcases from the home page.
Testcase 1:
– Navigate to the Login page.
– Enter the correct username and Password
– Verify valid user can login.
Testcase 2:
– Navigate to the Login page.
– Enter InCorrect username and Password
– Verify that the InValid user is not able to login.
Session 3: Configure testng.xml and system.properties
- How to create testng.xml and run the test suite in testng.
- Read data from system.properties file.