Implement Code Re-usability in Selenium Automation Framework

In this post, you will learn kind of coding pattern which is very helpful in maintaining our automation code. This post is written insight to help beginners. We'll learn that instead of writing a linear script, we should create page (action) methods which in general contain actions which we are going to perform on our web software.

Let's say, we have a login functionality in our software and we have to automate it. In that case we'll create a method named login and write the commands like, entering username, password and click login in that method. Now we can use that method wherever we need it in other test cases as well. Benefits of using action methods are:
  • Code Re-usability
  • Better maintainability of code
Sample Program for Re-Usable Action Methods:

package com.techlistic.tute;

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

 * Script Topic - Action Methods 
 * Description - Action Methods are implemented for following reasons:
 * 1. Code Reusability
 * 2. Better maintainability of code
 * @author Vaneesh Behl 
public class WebdriverActionMethods {
	// Creating Object
	private WebDriver driver = new FirefoxDriver();
	private String browserUrl = "";
	private String userName = "Mark";

	// Demo Scenario 1
	public void testMethods(){
		System.out.println("Opening URL");

		System.out.println("Clicking on Link");

		System.out.println("Clicking on Submit Button");

		System.out.println("Entering User Name");

	// Demo Scenario 2 - Verifying Title of the Page
	public void testMethodScenario2() {
		System.out.println("Opening URL");

		System.out.println("Clicking Link Test");

		System.out.println("Navigating Backward");

		System.out.println("Enter Firstname");

		System.out.println("Clicking Radio Button");

		System.out.println("Selecting Profession");

		System.out.println("CLicking Submit");

		String expectedTitle = "Selenium | Home";
		String actualTitle = driver.getTitle();
		// Verifying Title
		Assert.assertTrue("Test has Failed: Values didn't match.", expectedTitle.equals(actualTitle));
		System.out.println("Test has Passed");


	/* ------------------------- Re-Usable Action Methods ---------------- 	 */

	// Open URL
	public void openURL(String URL) {
		// Opening Browser URL 


	// Clicking on Link Test
	public void clickOnLinkTest(){
		WebElement LINK_TEST = driver.findElement(By.linkText("Link Test"));;
		//driver.findElement(By.linkText("Link Test")).click();

	// Navigating Backward
	public void navigateBackward(){

	// Entering First Name
	public void enterFirstName(String username){
		WebElement FIRST_NAME_FIELD = driver.findElement("firstname"));



	// Clicking Radio Button
	public void clickingRadioButton() {
		WebElement RADIO_BUTTON = driver.findElement("sex-1"));;

	// Selecting Profession
	public void selectProfession(){
		WebElement PROFESSION = driver.findElement("profession"));;

	// Click Submit Button
	public void clickSubmit() {
		WebElement SUBMIT_BUTTON = driver.findElement("submit"));;

You can also place these action methods in some other class like And if you want to re-use any of the methods in your Selenium script, just create the object Common class and call that method. Like .,

// Create Object
Common commonObj = new Common();

//Call method

Top 25 Selenium WebDriver Commands  << Previous      ||      Next >>  10+ Selenium WebDriver Practice Assignments

Passionately working as an Automation Developer from 12+ years. Let's connect on LinkedIn


Popular posts from this blog

Top 7 Web Development Trends in the Market

17 Best Demo Websites for Automation Testing Practice

Mastering Selenium Practice: Automating Web Tables with Demo Examples

14 Best Selenium Practice Exercises for Automation Practice

Top 10 Highly Paid Indian CEOs in the USA

Automation Practice: Automate Amazon like E-Commerce Website with Selenium

Top 51 Most Important Selenium WebDriver Interview Questions

How to Automate Google Search with Selenium WebDriver

Python Behave Tutorial: A Comprehensive Guide to Behavior-Driven Development (BDD)

Selenium IDE Tutorial: How to Automate Web Testing with Easy-to-Use Interface