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 = "http://www.toolsqa.com/automation-practice-form/";
	private String userName = "Mark";


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

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

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

		System.out.println("Entering User Name");
		enterFirstName(userName);
	}

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

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

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

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

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

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

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

		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 
		driver.get(URL);

	}

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

	// Navigating Backward
	public void navigateBackward(){
		driver.navigate().back();
	}

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

		FIRST_NAME_FIELD.clear();

		FIRST_NAME_FIELD.sendKeys(username);
	}

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

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

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

You can also place these action methods in some other class like Common.java. 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
commonObj.openURL("https://www.techlistic.com/p/selenium-tutorials.html");

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


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

Follow Us

YouTube Channel | Facebook Page | Telegram Channel | Quora Space
Feel free to ask queries or share your thoughts in comments or email us.

Comments

Popular posts from this blog

10+ Best Demo Websites for Selenium Automation Testing Practice

Automate Amazon like E-Commerce Website with Selenium WebDriver

Handle Static and Dynamic Web Table in Selenium WebDriver

25+ Most Important Selenium WebDriver Commands Tutorial

50+ Most Important Selenium WebDriver Interview Questions and Answers

Top 7 Web Development Trends in 2022

How to Automate Google Search with Selenium WebDriver

How to Find Broken Links on Webpage with Selenium WebDriver