How to pass WebDriver instance to other class in Selenium

In this post we are going to learn How to pass the Web Driver instance to other Class in Selenium web driver?.In this post i am using Selenium Web Driver,Java language and Test NG,for more details on Selenium TutorialsTestNG please read my tutorials,so that you can get more knowledge.

 Pass WebDriver instance to other class in Selenium

1.Create class
2.Create other class here I am using as below.

Selwebddriver Class:

import org.openqa.selenium.WebDriver;
//Chrome Driver
//Test NG Annotation
import org.testng.annotations.BeforeClass;

//Class Name
public class Selwebddriver {
//Declare Webdriver variables
WebDriver driver;
String baseUrl;

//Initializing Selenium Webdriver Before Class
public void setup() {
//Iam using Chrome Browser
File ChromeDriver = new File("F:\Java_Applications\ZipFiles\chromedriver.exe");
System.setProperty("", ChromeDriver.getAbsolutePath());
//Chrome Options to maximize full window
ChromeOptions options = new ChromeOptions();
driver = new ChromeDriver(options);
baseUrl = "";

Now we have to use Selwebddriver Class instances in second class as below.

ALSO READ  Selenium WebDriver Page Object Model Framework Introduction

AppConfirmation Class

public class AppConfirmation extends Selwebddriver {

public void Confirmation() throws AWTException {
reports = new ExtentReports("E:\Automation_Testing\GmailAuto\TestResults\Testresults.html",true);
ExtentTest test = reports.startTest("Verify GmailLogin page");
//Open Specified application URL with below code
test.log(LogStatus.PASS, "Gmail Login page is opened");

Now run AppConfirmation Class as Run as Test NG with right click on AppConfirmation Class,it will automatically calls Selwebdriver instances and open the browser.In case you like my post and idea about How to pass the Web Driver instance to other Class in Selenium ,please provide your valuable comments, suggestions , feedback. Thank you for reading.


What have you Learned?

1.Java extends keyword.
2.Use one class objects in another class ex:driver.
3.Create two classes and use driver instance to a number of classes.
4.TestNG tests annotations in different classes.

ALSO READ  Appium Introduction

Must Read:

Send Extent Reports in Email with Screenshots
How to write Test Cases PASS or FAIL in Excel using Selenium
How to capture screenshot for failed test cases in Selenium
Selenium Webdriver Using Python – Complete Tutorial Guide
Get HTML source of webelements using python

Rajesh K

2 thoughts on “How to pass WebDriver instance to other class in Selenium

  1. I am using chrome drive , but i am always getting "Cannot instantiate class" which is caused by "java.lang.reflect.InvocationTargetException" and "java.lang.NullPointerException". The null pointer seems to be occurring due to driver not initialised although i have done that in @BeforTest method. The code stricture is same as you have mentioned but the problem seems to be that Base class methods are not being executed. Have you faced such issue, can you please help me in that ?

  2. Hi, This is very nice information.
    How can we call driver class in other classes also?

    1. Driver class
    2. Login
    3. Add user
    Here i want to use driver class in both "login" and "Add user" classes. So i can run all the classes in "testng.xml" file
    Could you please explain this scenario

Comments are closed.