[フレーム]

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

by 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.

Reader Interactions

Comments

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

    • Mukesh Otwani says

      Hi Tejas,

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

  2. 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…

  3. 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

  4. 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.

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

    • Mukesh Otwani says

      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

  6. Gaurav Khurana says

    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

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

  8. Jithin says

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

  9. 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>

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.

AltStyle によって変換されたページ (->オリジナル) /