Use of Base Class in a Test Automation Framework

Use of Base Class in a Test Automation Framework

In a test automation framework it is essential to have a base class which serves many uses. A base class helps us have a one place to mention browser initialization, rather than mentioning it in each test case. If we create browser initialization in each test case rather than having a base class it is very cumbersome to change each and every test script when we need to run the same test suite in different browsers. Having a base class makes us easy to tweak the test automation suite to run in different browsers with a change in the browser variable setup in a file or a simple Java class variable.

The following script will show how a base class is created which integrates the Bonogracia WebDriver manager.

package TestCase;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import Utility.ConstantVariables;
import io.github.bonigarcia.wdm.WebDriverManager;
public class BaseClass {
	  public static WebDriver driver  = null;
	  
	public static WebDriver initilize()
	   {


	      //Use Of Singleton Concept and Initilize webDriver
	      if(driver == null)
	      {
	         if(ConstantVariables.browserName.equalsIgnoreCase("chrome"))
	         {
	        	 WebDriverManager.chromedriver().setup();
	     		 driver = new ChromeDriver();
	         }
	         else if(ConstantVariables.browserName.equalsIgnoreCase("chrome headless"))
	         {
	        	 WebDriverManager.chromedriver().setup();
	        	 ChromeOptions options = new ChromeOptions(); 
	    		 options.addArguments("--headless");
	            driver=new ChromeDriver(options);
	         }
	         else if(ConstantVariables.browserName.equalsIgnoreCase("Firefox"))
	         {
	        	 WebDriverManager.firefoxdriver().setup();
	            driver=new FirefoxDriver();
	         }
	         else if(ConstantVariables.browserName.equalsIgnoreCase("IE"))
	         {
	        	 WebDriverManager.iedriver().setup();
	            driver=new EdgeDriver();
	         }
	        
	      }
	      
	      //Perform Basic Operations
	      driver.manage().deleteAllCookies();
	      driver.manage().window().maximize();
	      driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	      driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
	      return driver;
	   }
	   public static void quit()
	   {
	      driver.quit();
	      driver=null; // we destroy the driver object after quit operation
	   }
	   public static void close()
	   {
	      driver.close();
	      driver=null;  // we destroy the driver object after quit operation
	   }   
	   public  static void openurl(String URL)
	   {
	      driver.get(URL);
	   }




}

The test scripts can be implemented in the following manner

package TestCase;
import PageClasses.LoginPage;
import PageClasses.HomePage;
import Utility.ConstantVariables;
import Utility.JSONReader;
import io.github.bonigarcia.wdm.WebDriverManager;
import java.io.FileNotFoundException;
import java.io.IOException;


import org.json.simple.parser.ParseException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;




public class LoginTest {
	public static WebDriver driver  = null;
@BeforeTest
public void setup()
{
	      driver = BaseClass.initilize();
 }
	 
  @Test
  public void  Test () throws FileNotFoundException, IOException, ParseException
  {
	  		BaseClass.openurl(ConstantVariables.URl);
	      


			LoginPage.using(driver).launch().setUserName(JSONReader.ReadJSONFile("User_Name", "./Data/data.json")).setPassword(JSONReader.ReadJSONFile("Password", "./Data/data.json")).clickLogin();
     		Assert.assertTrue(HomePage.using(driver).getHomePageDashboardUserName());
     	
     	
  }

  @AfterTest
  public void teardown()
  {
	  BaseClass.close();
  }


}

The one stop place is the Constant Variables Java Class which mentions the Browser which all the test suite should be executed.

package Utility;


public class ConstantVariables {
	   public static String browserName="chrome";
	   public static String URl="https://demo.guru99.com/V4/";


}


要查看或添加评论,请登录

Kushan Shalindra Amarasiri的更多文章

社区洞察

其他会员也浏览了