Selenium Automation framework

How to Create Base Class in Selenium for Better script

All of us know the importance of Automation framework which can reduce the huge amount to rework.This article will walk you through How to Create Base Class in Selenium for Better script and trust me you will love this feature.

Long back we have covered Data Driven framework and Page Object model as well, so you can combine all the concepts now with Base class and you can create a healthy  framework as well.Base Class in Selenium

What is Base Class in Selenium

  • Base class in the main class which will take care of Browser setup, loading configuration file and other reusable methods like screenshot, handling sync issues and many more.
  • Using Base class we can avoid code duplication.
  • Reuse code as much we can.

 

Preconditions before jumping into advance concepts.

  1. Get ready with basic tutorials.
  2. TestNG should be installed and little knowledge on TestNG Framework.
  3. Understanding of Inheritance in JAVA.

 

 

How Base class works in Selenium

1-When we create base class and if TestCases extends BaseClass then we can use all the methods of Baseclass.

2- Before calling actual @Test Base class methods will get executed and Depends on annotations it will call the respective methods.

3- We can extend this class in all test cases and we can call custom methods as well directly.

 

                           YouTube Video for Base Class in Selenium

 

Base Class in Selenium

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Reporter;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;

public class BaseClass 
{
        
        
	WebDriver driver;
	
	@BeforeClass
	public void setupApplication()
	{
		
		Reporter.log("=====Browser Session Started=====", true);
		
                driver=new ChromeDriver();
		
		driver.manage().window().maximize();
		
		driver.get("http://enterprise.demo.orangehrmlive.com/symfony/web/index.php/auth/login");
		
		Reporter.log("=====Application Started=====", true);
	}
	
	
	@AfterClass
	public void closeApplication()
	{
		driver.quit();
		Reporter.log("=====Browser Session End=====", true);
		
	}
	
	
	
}

 

Actual Test script for Selenium Framework

package pages;

import org.openqa.selenium.By;
import org.testng.annotations.Test;

public class InvalidLogin extends BaseClass
{

	@Test(description="This TC will perform valid login")
	public void loginToApplication() throws Throwable
	{
		driver.findElement(By.name("txtUsername")).sendKeys("Admin1");
		
		driver.findElement(By.id("txtPassword")).sendKeys("admin1");
		
		driver.findElement(By.id("btnLogin")).click();
		
		driver.navigate().back();
		
		Thread.sleep(5000);
	}
	
	
	@Test(description="This TC will perform invalid login")
	public void loginToApplication1()
	{
		driver.findElement(By.name("txtUsername")).sendKeys("admin1");
		
		driver.findElement(By.id("txtPassword")).sendKeys("admin2");
		
		driver.findElement(By.id("btnLogin")).click();
	}
	
}

 

I hope you will implement the same concept in your automation framework. If you want to add anything else in this post than let me know in the comment section.

Thank you so much, Kindly share with your friends. See you in the next post.

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

39 thoughts on “How to Create Base Class in Selenium for Better script

  1. arun says:

    it is failing driver = chrome not running

    1. Hi Arun,

      You may need to debug your code line by line in order to find the actual issue. You can refer this link https://www.youtube.com/watch?v=rXXeHfvbddo

      1. Kalaiselvi says:

        Hi sir
        Now I am learning selenium with Java
        In junit we r creating base class to reduce duplication and increase code reusability.
        login base class methods only I learned from trainer..how I create other methods base classes.? Could u pls tel me.very difficult to find

          1. Kalaiselvi says:

            Ya I have seen.but I need information about base class methods creation.could u pls upload videos for that?

          2. Hi Kalaselvi, in that series I have also shown how to create base classes and other utilities as well.

  2. Mayank Singla says:

    How I can run multiple test Classes without closing the browser/website?

    1. Hi Mayank,

      Set your test cases in a proper sequential way so that application screen where your first test ends should be start screen for next test

  3. ali says:

    perfect, thx.

  4. raja says:

    hi Mukesh,
    Combine of Keyword and page factory framework is possible to automate?

    1. Hi Raja,

      Yes, it is possible. Framework building is just an approach using which you are executing your test scenario

  5. tejas says:

    hi i am tejas,i am trying to automate 7 steps of web based application, and executing 7 classes with the help of testng.xml, but my first class is executed only,and the remaining classes is giving null pointer exception.In the first step i fill a form when i click on next button,new page is loaded in the website then my script is not working.How can i handle this?

    1. Hi Tejas,

      Since you are getting NullPointerException, please debug your code because somewhere in your script flow, you haven’t initialized any object properly.

  6. Vishnu says:

    Hi Mukesh,

    Very useful article. I have learned a lot from your tutorials. Could you please tell me how can I use Explicit wait from a base class just like browser class. I tried to create a base class with explicit waits and call the same from the test class with driver object. but it is showing null pointer exception. It would be great if you could guide me through this…

    1. Hi Vishnu,

      Did you debug your code as NullPointerException is generic runtime exception. You need to check which object is getting nullify in your code.

  7. Leena says:

    Hi Mukesh,

    I m trying to use this base class in hybrid framework. Made browser action as a base class in which I have added setup and browser close methods and calling in test cases classes but it is giving me null pointer exception. Could you please guide me how I can use this concept in hybrid framework? Thanks for your all vedios great job

    1. Hi Leena,

      Debug your script then you will come know about any missing parameter or value.

      1. Leena says:

        Thanks. Done with constructor super key in setup method created test case class.

  8. pit says:

    awsome. Is part 2 available ?

    1. Hi Arpit will upload soon.

  9. Pooja says:

    Hi Mukesh,
    In this video you have mentioned about part 2 of this video. I could not find that.
    Could you please share the video.

    1. Hi Pooja,

      Yes, I mentioned about part 2 in the video but it is not yet finalized. Whenever I publish this video(part 2) then you will get an email for that.

  10. James M. says:

    Hi Mukesh, your tutorial is awesome.
    But how can I implement something in the BaseClass that could make me choose whether Firefox, Chrome, IE or Safari browser I am using. How to implement it in the BaseClass?

    1. Hi James,

      There are multiple ways for doing this and one of them is passing parameters(in your case it is ‘browser’ name) to BaseClass using @Parameters annotation from testng.xml file

  11. Thanks it worked well. It would be great if we can know how to pass few parameters to base class.. I mean suppose if we want to pass the URL to the base class.

    Also its interesting to see that without creating any object, Base class functions are being called automatically when we run our script

    1. Yes Gaurav this is we also use in our framework.

  12. K praneeth kumar says:

    Hey Mukesh……nice work…

    can you tell me how to use the webdriver instance created in baseclass, in multiple tests in different classes, without declaring it as static??

    1. Hi Praneeth,

      Just inherit that class.

  13. Steve says:

    Nice example… thankyou

  14. Sowmya says:

    Mukesh…Your explanation is awesome. Your blog is helping me a lot in learning.

    1. Thanks Sowmya I am glad you liked it. Keep visiting.

  15. Jithin says:

    Hi mukesh, thanks for this tutorial
    I have a question. Is it possible to add utility functions along with BaseClass? ..

    1. Yes you can but based on functionality you can create packages as well.

  16. Cool says:

    Hi Mukesh,

    Shouldn’t this be @BeforeMethod and @AfterMethod annotations that you use in base class?

    1. In video I covered the same.

  17. pavani says:

    Hi Mukesh,

    Your lecture is awesome. I have small doubt. You gave chrome driver and u didnt give any path of chrome.
    Is is it possible>

    1. Yes Pavani because I am using MAC system and we can do that. Kindly follow below article for Chrome and Firefox https://vistasadprojects.com/mukeshotwani-blogs-v2/firefox-browser-on-mac-using-selenium-webdriver/

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.