How to pass selenium webdriver object to another class

Test Automation
3

In Selenium test script it is required to use or perform an action on current driver instance of browser(Chrome, IE, firefox etc).

it is therefore required to use the same driver object if the driver usage requirement in another class. 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


package Seleniumclasses;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Webdriverdefinition {
public static WebDriver driver = null;

public Webdriverdefinition()
{
if(driver == null)
{
String chromepath;
chromepath = “./chromedriver”;
System.setProperty(“webdriver.chrome.driver”, chromepath+”.exe” );
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- where 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 google search has been opened, Test has been entered in text box and button has been clicked.

1(Webdriverdefinition class)

package Seleniumclasses;
import org.openqa.selenium.By;
import org.testng.annotations.Test;

public class WebInteraction extends Webdriverdefinition
{

@Test
public void login()
{
//getdriver().get(“https://www.google.com/”);
getdriver().findElement(By.id(“lst-ib”)).sendKeys("Test Automation”);
getdriver().findElement(By.cssSelector(“[name='btnK']”)).click();
}

}

It is recommended to use same driver instance inside your test scripts, and to pass the driver instance across different class/methods there are couple of other ways to declare and pass driver object which i will publish in next post. Keep reading for the same.

Its Prashant Chaudhary, I have 10+ years of experience in software test automation, performance & process automation with various commercial and open source tools. This blog shares my experience on the topic of Automation. The content published on my blog is solely mine, it does not express the views of any of my employers (current or previous).
3 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Selenium Basic
1
Step by step selenium – step 3, what is findelement method

Abstract- So far, we have learned how to launch a web browser, entering values in a text box and clicking button. In this post, we will learn about findelement method. Test automation is all about get and set, which means get from web and set into web. Talking more specifically- …

Test Automation
1
What is ITestListner interface and its Use in Selenium

There are several interfaces available in TestNG that allow you to modify TestNG’s behavior or to give test scripts additional strength. These interfaces are broadly called “TestNG Listeners”. Here are a few listeners which are commonly used: 1- IAnnotationTransformer 2- IAnnotationTransformer2 3- IHookable 4- IInvokedMethodListener 5- IMethodInterceptor 6- IReporter 7- …

Test Automation
How to Capture Screenshot in Selenium Webdriver-3 different ways

Abstract– Capturing screenshot is an essential feature of a test automation framework, They help in defect analyses once after test execution is over. In this blog post we will see how to capture screenshot, there are different ways to capture screenshots. Which includes- 1- Capture full Page 2- Capturng full …

Shares