RE: Can we use objects of one class in another class only with the help of TestNG?

Can we use objects of one class in another class only with the help of TestNG?

e.g.: I have an object of a driver and sheet in class 1 and I want to use them in class2. How we can do it having TestNG implemented?

Or you may suggest a solution that can be implemented.

thitenitin Train Asked on December 12, 2014 in Selenium WebDriver.

Please close the question Nitin.

on December 15, 2014.
Add Comment
4 Answers

Hi Nitin,

To avoid the complexity here make use of staic keyword in java:

Base Class

package testCases;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Base {
    public static WebDriver driver;
    private static HSSFSheet ExcelWSheet;
    private static HSSFWorkbook ExcelWBook;
    public static WebDriver startBrowser(){
        return driver = new FirefoxDriver();
    public static void setExcelFile(String Path,String SheetName) throws Exception {
        try {
            FileInputStream ExcelFile = new FileInputStream(Path);
            ExcelWBook = new HSSFWorkbook(ExcelFile);
            ExcelWSheet = ExcelWBook.getSheet(SheetName);
        } catch (Exception e){
            throw (e);

Test case

package testCases;
import org.openqa.selenium.WebDriver;
public class Test {
    public static void main(String[] args) {
        WebDriver driver = Base.startBrowser();

PLease close the question.

Lakshay Sharma

Professor Answered on December 12, 2014.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.