Overview –

In Selenium test automation framework it is required to use or perform an action on current driver instance of browser(Chrome, IE, firefox etc). In this blog post, we will understand and learn how to create a driver object and use the same object in another class.

It is therefore required to use the same driver object if the driver usage requirement in another class else it will throw an exception.

Please follow this post on, How to pass it or how to get that same object in another class(java- considering scripting in java).

How to do it-using constructor and inheritance

First, create a class which has a constructor, which is initializing selenium driver

*In below code carefull update chromedriver path of your local machine.
package TestPck;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Webdriverdefinition {
public static WebDriver driver = null;
public Webdriverdefinition()
{
if(driver == null)
{
          //Mention path of chrome driver of your system
	 String exePath = "./Resources/chromedriver.exe";
		System.setProperty("webdriver.chrome.driver", exePath);
       driver = new ChromeDriver();
System.out.println("**Chrome driver initiated**");
}
else
{
System.out.println("**Chrome driver already instantiated**");
}
}
public static WebDriver getdriver()
{
	if (driver == null){
		return driver;
		}else{
		return driver;
		}
}
}

Class 2- Create another class with name- WebInteraction

In this class Class1(Webdriverdefinition) has been extended, using extend has automatically constructor which defining object. In @Test method Login getdriver is being called which is getting driver from class –

In class 2 chrome driver is being launched and orangeHRM application is launched, there are 2 test –

1- Test 1(with priority 1),-is launching OrangeHRM application and login is happening.

Method Name-login_OrangeHRM()

2- Test 2(with priority 2)-getTitle is being used to get title of home/dashboard page.

Method Name– loginHRM_GetTitle

package TestPck;
import org.openqa.selenium.By;
import org.testng.annotations.Test;
public class WebInteraction extends Webdriverdefinition
{
	@Test(priority=1)
	public void login_OrangeHRM()
	{
		
		getdriver().get("https://opensource-demo.orangehrmlive.com/");
		getdriver().findElement(By.cssSelector("[name=txtUsername]")).sendKeys("Admin");
		getdriver().findElement(By.cssSelector("[name=txtPassword]")).sendKeys("admin123");
		getdriver().findElement(By.cssSelector("[name='Submit']")).click();
	
	}
	
	@Test(priority=2)
	public void loginHRM_GetTitle()
	{
		String Title;
		Title = getdriver().getTitle();
		System.out.print("Title--"+Title);
		
		
	}
}

Summary-

There are different ways of handling driver object passing to another class, this is one of the efficient ways of doing it.

Points to be considered while implementing it-

1- Always extend weddriver class which has a constructor (initializing webdriver ) wherever you want to call a driver.

2- Use of static driver object is needed, refer class “Webdriverdefinition” so that same driver object is shared.

Please share if you find any better way to handle it

Keep Automating 🙂