Skip to main content

Command Palette

Search for a command to run...

Mastering Code Reusability in Selenium

A Beginner's Guide to Inheritance

Published
•3 min read•View as Markdown
Mastering Code Reusability in Selenium

In the world of automation testing, especially with Selenium, code reusability is key. And one of the most powerful ways to achieve that is through inheritance in Java. If you're diving into Selenium WebDriver and want to write clean, maintainable scripts, this blog is for you.

What is Inheritance?

Think of inheritance as a "parent-child" relationship in programming. It allows one class (child) to inherit the properties and methods of another class (parent). By doing so, the child class gets to use the existing functionality without having to rewrite it.

In simple terms: Why reinvent the wheel when you can borrow it?

How Inheritance Works in Selenium

When writing Selenium automation tests, you might have multiple test cases that need the same setup or methods. Rather than repeating the same code, you can create a base class (the parent) that contains common methods like browser setup, teardown, and reusable functions.

Then, you create your test classes (the child) that inherit these methods and focus only on the unique test logic. It’s like creating a blueprint once and then building many houses based on that design.

Benefits of Inheritance in Selenium Automation

  • Code Reusability: Common methods (e.g., launching the browser) are written once and used across multiple test cases.

  • Clean and Organized Code: It keeps your test code DRY (Don't Repeat Yourself) and easy to maintain.

  • Flexibility: You can extend the base class with new features without disrupting the test classes.


Real-World Example: Using Inheritance in Selenium

Let’s say you need to automate login functionality across multiple test cases for an e-commerce site. Here’s how inheritance helps you keep things simple.

Step 1: Create a Base Class

The base class contains common methods like setUp() to initialize the WebDriver, tearDown() to close the browser, and a login method:

// Base class
public class BaseTest {
    WebDriver driver;

    public void setUp() {
        System.setProperty("webdriver.chrome.driver", "path_to_chromedriver");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

    public void tearDown() {
        driver.quit();
    }

    public void login(String username, String password) {
        driver.get("https://www.example.com");
        driver.findElement(By.id("username")).sendKeys(username);
        driver.findElement(By.id("password")).sendKeys(password);
        driver.findElement(By.id("loginButton")).click();
    }
}

Step 2: Create Test Classes That Inherit the Base Class

Now, instead of writing the setup, teardown, and login logic again and again, your test classes can simply inherit the base class:

// Test class 1
public class HomePageTest extends BaseTest {
    @Test
    public void verifyHomePageTitle() {
        setUp();
        login("user1", "password1");

        // Test home page title
        String title = driver.getTitle();
        Assert.assertEquals(title, "Home Page");

        tearDown();
    }
}

// Test class 2
public class ProfilePageTest extends BaseTest {
    @Test
    public void verifyUserProfile() {
        setUp();
        login("user1", "password1");

        // Test user profile page
        driver.findElement(By.id("profile")).click();
        String profileName = driver.findElement(By.id("userName")).getText();
        Assert.assertEquals(profileName, "User One");

        tearDown();
    }
}

Step 3: Enjoy the Simplicity!

As you can see, both test classes (HomePageTest and ProfilePageTest) inherit from BaseTest. They can now reuse the setup, teardown, and login methods without duplicating code. Your tests are clean, simple, and much easier to maintain!


Key Points to Remember:

  • Single Inheritance: Java allows only single inheritance, meaning a class can inherit from only one parent class.

  • Protected and Public Methods: To allow child classes to access methods, mark them as protected or public in the parent class.

  • Overriding: If needed, child classes can override the parent methods to provide their own functionality.


Conclusion

Inheritance is a simple yet powerful concept that can help you write better Selenium tests. By reusing code across multiple test cases, you not only reduce redundancy but also make your automation framework more scalable and maintainable.