{"id":27914,"date":"2025-07-02T03:43:34","date_gmt":"2025-07-02T07:43:34","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=27914"},"modified":"2025-07-02T03:48:01","modified_gmt":"2025-07-02T07:48:01","slug":"building-a-selenium-framework-from-scratch-step-by-step-guide","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/building-a-selenium-framework-from-scratch-step-by-step-guide\/","title":{"rendered":"Building a Selenium Framework from Scratch: Step-by-Step Guide"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>Introduction<\/strong><\/h2>\n\n\n\n<p>In today&#8217;s fast-paced software development landscape, test automation has become essential. Among the many automation tools available, Selenium Framework stands out as the most widely adopted open-source solution for automating web applications. However, simply writing scripts isn&#8217;t enough you need a robust Selenium framework to ensure maintainability, scalability, and reusability.<\/p>\n\n\n\n<p>This provides a step-by-step guide to building a Selenium framework from scratch using Java, TestNG, Maven, and other supporting tools. Whether you&#8217;re a beginner pursuing a <a href=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\">Selenium Certification<\/a> or a professional QA engineer, this guide will help you lay the groundwork for a solid test automation strategy.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What Is a Selenium Framework?<\/strong><\/h2>\n\n\n\n<p>A Selenium Framework is a structured and reusable automation testing architecture built using the Selenium tool. It serves as the foundation for writing and managing test scripts efficiently by integrating various components like Selenium WebDriver, TestNG or JUnit, Maven, reporting tools, and utility libraries. Rather than writing isolated test cases, a framework helps organize code in a modular, maintainable, and scalable way.<\/p>\n\n\n\n<p>At its core, a Selenium framework separates the test logic from UI interactions, often using the Page Object Model (POM) or other design patterns. This separation enhances code reusability and reduces maintenance time when application elements change. Frameworks also support features like centralized configurations, logging, parallel execution, data-driven testing, and integration with <a href=\"https:\/\/www.h2kinfosys.com\/blog\/ci-cd-interview-questions\/\" data-type=\"post\" data-id=\"17321\">CI\/CD <\/a>pipelines<a href=\"https:\/\/www.h2kinfosys.com\/blog\/ci-cd-interview-questions\/\" data-type=\"post\" data-id=\"17321\">.<\/a><\/p>\n\n\n\n<p>There are several types of Selenium frameworks, including modular, data-driven, keyword-driven, hybrid, and behavior-driven development (BDD) frameworks using tools like Cucumber. Each serves different testing needs and levels of complexity.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"590\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2025\/07\/it-developers-discussing-online-software-development-information-sellable-1024x590.jpg\" alt=\"\" class=\"wp-image-27948\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2025\/07\/it-developers-discussing-online-software-development-information-sellable-1024x590.jpg 1024w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2025\/07\/it-developers-discussing-online-software-development-information-sellable-300x173.jpg 300w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2025\/07\/it-developers-discussing-online-software-development-information-sellable-768x442.jpg 768w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2025\/07\/it-developers-discussing-online-software-development-information-sellable-1536x884.jpg 1536w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2025\/07\/it-developers-discussing-online-software-development-information-sellable-2048x1179.jpg 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Mastering a Selenium framework is often a key requirement in job roles related to test automation. Therefore, those pursuing a <a href=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\">Selenium Training<\/a> or a career in QA automation benefit greatly from learning how to build and maintain these frameworks effectively. It&#8217;s an essential skill for delivering reliable, fast, and maintainable automated test solutions.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Code reusability<\/li>\n\n\n\n<li>Better test organization<\/li>\n\n\n\n<li>Easy maintenance<\/li>\n\n\n\n<li>Scalability<\/li>\n\n\n\n<li>Integration with CI\/CD pipelines<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Why Build a Custom Selenium Framework?<\/strong><\/h2>\n\n\n\n<p>Building a custom Selenium framework offers flexibility, control, and long-term efficiency in test automation. While out-of-the-box solutions and record-playback tools can handle basic tasks, they often fall short in handling complex test scenarios, scalability, and integration needs. A custom framework allows QA teams to tailor the architecture to match the project\u2019s requirements and coding standards.<\/p>\n\n\n\n<p>One of the biggest advantages of a custom framework is modularization. By using design patterns like the Page Object Model (POM), test logic and <a href=\"https:\/\/en.wikipedia.org\/wiki\/User_interface\" rel=\"nofollow noopener\" target=\"_blank\">UI<\/a> elements are separated, making the codebase easier to maintain. As the application evolves, changes can be made quickly without rewriting entire test cases. Custom frameworks also support data-driven testing, enabling testers to run the same tests with multiple sets of input data from Excel, CSV, or databases.<\/p>\n\n\n\n<p>Other benefits include integration with reporting tools, continuous integration pipelines (e.g., Jenkins), version control systems (e.g., Git), and containerization platforms like Docker. Custom frameworks also allow better parallel execution, improving test speed and efficiency.<\/p>\n\n\n\n<p>For professionals working toward a Selenium certification, the ability to build a custom Selenium framework showcases practical expertise and problem-solving skills both of which are highly valued in automation testing careers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Benefits:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Modular architecture<\/li>\n\n\n\n<li>Easy bug identification and resolution<\/li>\n\n\n\n<li>Seamless reporting<\/li>\n\n\n\n<li>Parallel execution support<\/li>\n\n\n\n<li>Integration with tools like Jenkins, Git, and Docker<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Prerequisites Before You Start<\/strong><\/h2>\n\n\n\n<p>Before building your framework, ensure you have the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Java Development Kit (JDK) installed<\/li>\n\n\n\n<li>IDE: IntelliJ IDEA, Eclipse, or similar<\/li>\n\n\n\n<li>Maven: For dependency management<\/li>\n\n\n\n<li>TestNG: For test execution management<\/li>\n\n\n\n<li>Selenium WebDriver<\/li>\n\n\n\n<li>Git: Version control system<\/li>\n\n\n\n<li>Knowledge of OOP concepts<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step 1: Set Up Your Project Environment<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Create a Maven Project<\/strong><\/h3>\n\n\n\n<p>Maven simplifies project management and dependency handling. You can generate a Maven project via the IDE or command line.<\/p>\n\n\n\n<p><strong>Sample <code>pom.xml<\/code> Dependencies:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">xmlCopy<code>&lt;dependencies&gt;\n    &lt;dependency&gt;\n        &lt;groupId&gt;org.seleniumhq.selenium&lt;\/groupId&gt;\n        &lt;artifactId&gt;selenium-java&lt;\/artifactId&gt;\n        &lt;version&gt;4.19.1&lt;\/version&gt;\n    &lt;\/dependency&gt;\n\n    &lt;dependency&gt;\n        &lt;groupId&gt;org.testng&lt;\/groupId&gt;\n        &lt;artifactId&gt;testng&lt;\/artifactId&gt;\n        &lt;version&gt;7.9.0&lt;\/version&gt;\n        &lt;scope&gt;test&lt;\/scope&gt;\n    &lt;\/dependency&gt;\n\n    &lt;dependency&gt;\n        &lt;groupId&gt;io.github.bonigarcia&lt;\/groupId&gt;\n        &lt;artifactId&gt;webdrivermanager&lt;\/artifactId&gt;\n        &lt;version&gt;5.8.0&lt;\/version&gt;\n    &lt;\/dependency&gt;\n&lt;\/dependencies&gt;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step 2: Define Your Folder Structure<\/strong><\/h2>\n\n\n\n<p>Maintain a clean directory structure for better manageability.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cssCopy<code>src\n\u251c\u2500\u2500 main\n\u2502   \u251c\u2500\u2500 java\n\u2502   \u2502   \u251c\u2500\u2500 base\n\u2502   \u2502   \u251c\u2500\u2500 pages\n\u2502   \u2502   \u251c\u2500\u2500 utils\n\u251c\u2500\u2500 test\n\u2502   \u251c\u2500\u2500 java\n\u2502   \u2502   \u251c\u2500\u2500 tests\n\u2514\u2500\u2500 resources\n    \u251c\u2500\u2500 config.properties\n    \u251c\u2500\u2500 testdata.xlsx\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Folder Description:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>base<\/strong>: Contains BaseTest and BasePage classes<\/li>\n\n\n\n<li><strong>pages<\/strong>: Contains Page Object Model classes<\/li>\n\n\n\n<li><strong>utils<\/strong>: Utility functions (e.g., Excel reader, wait conditions)<\/li>\n\n\n\n<li><strong>tests<\/strong>: TestNG test scripts<\/li>\n\n\n\n<li><strong>resources<\/strong>: Configuration and data files<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step 3: Create a Base Class for Initialization<\/strong><\/h2>\n\n\n\n<p>Your <code>BaseTest<\/code> class will handle setup and teardown using <code>@BeforeMethod<\/code> and <code>@AfterMethod<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">javaCopy<code>public class BaseTest {\n    protected WebDriver driver;\n\n    @BeforeMethod\n    public void setUp() {\n        WebDriverManager.chromedriver().setup();\n        driver = new ChromeDriver();\n        driver.get(\"https:\/\/example.com\");\n        driver.manage().window().maximize();\n    }\n\n    @AfterMethod\n    public void tearDown() {\n        if (driver != null) {\n            driver.quit();\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step 4: Implement Page Object Model (POM)<\/strong><\/h2>\n\n\n\n<p>POM separates the UI and test logic, increasing maintainability.<\/p>\n\n\n\n<p><strong>LoginPage.java<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">javaCopy<code>public class LoginPage {\n    private WebDriver driver;\n\n    @FindBy(id = \"username\")\n    WebElement username;\n\n    @FindBy(id = \"password\")\n    WebElement password;\n\n    @FindBy(id = \"loginBtn\")\n    WebElement loginBtn;\n\n    public LoginPage(WebDriver driver) {\n        this.driver = driver;\n        PageFactory.initElements(driver, this);\n    }\n\n    public void login(String user, String pass) {\n        username.sendKeys(user);\n        password.sendKeys(pass);\n        loginBtn.click();\n    }\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step 5: Create Your Test Classes Using TestNG<\/strong><\/h2>\n\n\n\n<p><strong>LoginTest.java<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">javaCopy<code>public class LoginTest extends BaseTest {\n    @Test\n    public void verifyLogin() {\n        LoginPage login = new LoginPage(driver);\n        login.login(\"admin\", \"admin123\");\n        Assert.assertEquals(driver.getTitle(), \"Dashboard\");\n    }\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step 6: Externalize Configurations<\/strong><\/h2>\n\n\n\n<p>Create a <code>config.properties<\/code> file in <code>resources<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">propertiesCopy<code>baseUrl=https:\/\/example.com\nbrowser=chrome\nusername=admin\npassword=admin123\n<\/code><\/pre>\n\n\n\n<p>Create a utility class to load configurations:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">javaCopy<code>public class ConfigLoader {\n    public static Properties loadConfig() throws IOException {\n        FileInputStream file = new FileInputStream(\"src\/test\/resources\/config.properties\");\n        Properties prop = new Properties();\n        prop.load(file);\n        return prop;\n    }\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step 7: Add Utility Functions<\/strong><\/h2>\n\n\n\n<p><strong>WaitUtils.java<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">javaCopy<code>public class WaitUtils {\n    public static void waitForVisibility(WebDriver driver, WebElement element, int timeout) {\n        new WebDriverWait(driver, Duration.ofSeconds(timeout))\n            .until(ExpectedConditions.visibilityOf(element));\n    }\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step 8: Data-Driven Testing with Excel<\/strong><\/h2>\n\n\n\n<p>Use Apache POI to read Excel test data.<\/p>\n\n\n\n<p><strong>ExcelReader.java<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">javaCopy<code>public class ExcelReader {\n    public String getCellData(String sheetName, int row, int col) {\n        try {\n            FileInputStream fis = new FileInputStream(\"src\/test\/resources\/testdata.xlsx\");\n            Workbook wb = WorkbookFactory.create(fis);\n            return wb.getSheet(sheetName).getRow(row).getCell(col).toString();\n        } catch (Exception e) {\n            return null;\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step 9: Generate Reports Using TestNG<\/strong><\/h2>\n\n\n\n<p>TestNG automatically generates HTML reports post-execution.<\/p>\n\n\n\n<p>To customize:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <strong>ExtentReports<\/strong> or <strong>Allure<\/strong> for detailed reports.<\/li>\n\n\n\n<li>Add listeners for advanced test tracking.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step 10: Parallel Execution with TestNG XML<\/strong><\/h2>\n\n\n\n<p><strong>testng.xml<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">xmlCopy<code>&lt;suite name=\"SeleniumSuite\" parallel=\"tests\" thread-count=\"2\"&gt;\n    &lt;test name=\"LoginTest\"&gt;\n        &lt;classes&gt;\n            &lt;class name=\"tests.LoginTest\"\/&gt;\n        &lt;\/classes&gt;\n    &lt;\/test&gt;\n&lt;\/suite&gt;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step 11: Integrate with Jenkins<\/strong><\/h2>\n\n\n\n<p>To automate test runs:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Install Jenkins.<\/li>\n\n\n\n<li>Create a new job.<\/li>\n\n\n\n<li>Pull the project from Git.<\/li>\n\n\n\n<li>Add Maven command: <code>mvn clean test<\/code><\/li>\n\n\n\n<li>Schedule builds and view reports from Jenkins dashboard.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step 12: Dockerize Your Selenium Grid<\/strong><\/h2>\n\n\n\n<p>Run Selenium Grid via Docker:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">bashCopy<code>docker run -d -p 4444:4444 selenium\/standalone-chrome\n<\/code><\/pre>\n\n\n\n<p>Update your test setup to use RemoteWebDriver pointing to <code>http:\/\/localhost:4444\/wd\/hub<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"682\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2025\/07\/programming-background-with-person-working-with-codes-computer-1024x682.jpg\" alt=\"\" class=\"wp-image-27944\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2025\/07\/programming-background-with-person-working-with-codes-computer-1024x682.jpg 1024w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2025\/07\/programming-background-with-person-working-with-codes-computer-300x200.jpg 300w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2025\/07\/programming-background-with-person-working-with-codes-computer-768x512.jpg 768w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2025\/07\/programming-background-with-person-working-with-codes-computer-1536x1024.jpg 1536w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2025\/07\/programming-background-with-person-working-with-codes-computer.jpg 2000w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Best Practices<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use meaningful test method names.<\/li>\n\n\n\n<li>Keep locators centralized in page classes.<\/li>\n\n\n\n<li>Implement logging (e.g., Log4j).<\/li>\n\n\n\n<li>Use assertions wisely.<\/li>\n\n\n\n<li>Keep tests independent.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Building a Selenium framework from scratch offers unparalleled control and customization for your test automation needs. With Java, TestNG, Maven, and best practices in place, you can build a powerful, reusable, and scalable automation suite that integrates seamlessly into any CI\/CD pipeline. Many <a href=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\">Selenium Online Courses<\/a> now include hands-on modules to help learners gain real-world experience in building such frameworks from the ground up.<\/p>\n\n\n\n<p>Whether you are building your first test suite or upgrading an existing one, following this guide ensures a clean, modular, and future-proof test architecture.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Key Takeaways<\/strong><\/h2>\n\n\n\n<p>Integrate CI tools like Jenkins and Docker for efficiency.<\/p>\n\n\n\n<p>A custom Selenium framework improves test maintenance and scalability.<\/p>\n\n\n\n<p>Design a clean architecture using Java, Maven, and TestNG.<\/p>\n\n\n\n<p>Apply POM and utilities for reusability.<\/p>\n\n\n\n<p>Externalize configs and use data-driven techniques.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In today&#8217;s fast-paced software development landscape, test automation has become essential. Among the many automation tools available, Selenium Framework stands out as the most widely adopted open-source solution for automating web applications. However, simply writing scripts isn&#8217;t enough you need a robust Selenium framework to ensure maintainability, scalability, and reusability. This provides a step-by-step [&hellip;]<\/p>\n","protected":false},"author":14,"featured_media":27931,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-27914","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-selenium-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/27914","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=27914"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/27914\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/27931"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=27914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=27914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=27914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}