• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Selenium Automation framework / How to Create Base Class in Selenium for Better script

How to Create Base Class in Selenium for Better script

September 29, 2016 by Mukesh Otwani 39 Comments

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.

Filed Under: Selenium Automation framework Tagged With: Base Class in Selenium for Framework, Hybrid Framework Guide, Step to create Selenium Framework

Reader Interactions

Comments

  1. arun says

    September 5, 2021 at 12:04 AM

    it is failing driver = chrome not running

    Reply
    • Mukesh Otwani says

      September 5, 2021 at 9:35 AM

      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

      Reply
      • Kalaiselvi says

        March 7, 2022 at 11:04 AM

        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

        Reply
        • Mukesh Otwani says

          March 9, 2022 at 9:46 AM

          Hi Kalaiselvi, please refer this YT series https://www.youtube.com/watch?v=vFXL4nMWvXI&list=PL6flErFppaj0WwNOMFeXPVlNCDuJyPYFi

          Reply
          • Kalaiselvi says

            March 10, 2022 at 5:56 AM

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

          • Mukesh Otwani says

            March 10, 2022 at 1:05 PM

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

  2. Mayank Singla says

    April 30, 2020 at 5:06 PM

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

    Reply
    • Mukesh Otwani says

      April 30, 2020 at 8:36 PM

      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

      Reply
  3. ali says

    July 12, 2019 at 2:13 AM

    perfect, thx.

    Reply
  4. raja says

    June 17, 2019 at 2:28 PM

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

    Reply
    • Mukesh Otwani says

      June 17, 2019 at 3:50 PM

      Hi Raja,

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

      Reply
  5. tejas says

    May 31, 2019 at 5:04 PM

    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?

    Reply
    • Mukesh Otwani says

      May 31, 2019 at 9:51 PM

      Hi Tejas,

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

      Reply
  6. Vishnu says

    March 26, 2019 at 4:54 PM

    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…

    Reply
    • Mukesh Otwani says

      March 27, 2019 at 10:05 AM

      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.

      Reply
  7. Leena says

    February 11, 2017 at 3:57 PM

    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

    Reply
    • Mukesh Otwani says

      February 12, 2017 at 4:06 PM

      Hi Leena,

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

      Reply
      • Leena says

        February 14, 2017 at 12:10 AM

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

        Reply
        • Mukesh Otwani says

          February 15, 2017 at 12:05 AM

          Great…:)

          Reply
  8. pit says

    February 1, 2017 at 1:57 PM

    awsome. Is part 2 available ?

    Reply
    • Mukesh Otwani says

      February 1, 2017 at 3:04 PM

      Hi Arpit will upload soon.

      Reply
  9. Pooja says

    January 18, 2017 at 10:45 PM

    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.

    Reply
    • Mukesh Otwani says

      January 19, 2017 at 11:31 AM

      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.

      Reply
  10. James M. says

    January 14, 2017 at 6:55 PM

    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?

    Reply
    • Mukesh Otwani says

      January 14, 2017 at 8:20 PM

      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

      Reply
  11. Gaurav Khurana says

    January 3, 2017 at 8:33 AM

    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

    Reply
    • Mukesh Otwani says

      January 11, 2017 at 3:55 PM

      Yes Gaurav this is we also use in our framework.

      Reply
  12. K praneeth kumar says

    December 25, 2016 at 12:52 AM

    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??

    Reply
    • Mukesh Otwani says

      December 28, 2016 at 11:46 AM

      Hi Praneeth,

      Just inherit that class.

      Reply
  13. Steve says

    December 14, 2016 at 2:47 AM

    Nice example… thankyou

    Reply
    • Mukesh Otwani says

      December 17, 2016 at 12:43 PM

      Welcome Steve.

      Reply
  14. Sowmya says

    November 8, 2016 at 5:53 PM

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

    Reply
    • Mukesh Otwani says

      January 11, 2017 at 3:09 PM

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

      Reply
  15. Jithin says

    November 5, 2016 at 5:10 PM

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

    Reply
    • Mukesh Otwani says

      November 5, 2016 at 10:34 PM

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

      Reply
  16. Cool says

    October 14, 2016 at 5:55 AM

    Hi Mukesh,

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

    Reply
    • Mukesh Otwani says

      October 14, 2016 at 6:21 PM

      In video I covered the same.

      Reply
  17. pavani says

    October 6, 2016 at 3:40 AM

    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>

    Reply
    • Mukesh Otwani says

      October 7, 2016 at 7:34 PM

      Yes Pavani because I am using MAC system and we can do that. Kindly follow below article for Chrome and Firefox http://learn-automation.com/firefox-browser-on-mac-using-selenium-webdriver/

      Reply

Leave a Reply Cancel 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.

Primary Sidebar

Free Selenium Videos

Video Player
https://www.youtube.com/watch?v=w_iPCT1ETO4
00:00
00:00
31:21
Use Up/Down Arrow keys to increase or decrease volume.

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?