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.

Please follow below steps

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





Selwebddriver Class:

//Package Name
package com.gmail.account;
import java.io.File;
import org.openqa.selenium.WebDriver;
//Chrome Driver
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
//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
@BeforeClass
public void setup(){
//Iam using Chrome Browser
File ChromeDriver = new File("F:\\Java_Applications\\ZipFiles\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver",ChromeDriver.getAbsolutePath());
//Chrome Options to maximize full window
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
driver = new ChromeDriver(options);
baseUrl="http://www.gmail.com";
}
}

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

AppConfirmation Class

//We are using Selwebdriver instance with extends keyword
public class AppConfirmation extends Selwebddriver {
//Test NG Annotation where we are executing Test cases using Test annotation.
@Test
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
driver.get(baseUrl);
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.

Must Read:

Verify element is enable in Selenium
Read Data From properties file using Selenium
Launch Firefox Browser using GeckoDriver
Selenium-Testng examples to execute multiple classes
Selenium WebDriver Methods
Generate HTML Reports using Selenium

2 comments:

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

    Ex:
    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

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

    ReplyDelete