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.

Train Asked on December 12, 2014 in Selenium WebDriver.

Please close the question Nitin.

on December 15, 2014.
Add Comment
  • 4 Answer(s)

    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

    This can be used in TestNG too, right?

    Train Answered on December 12, 2014.


    on December 12, 2014.
    Add Comment

    Returned the driver object from method of Class 1
    and called that in Class 2 as
    “@Test(description=”chat tab”)
    public void conversation() throws IOException, InterruptedException
    WebDriver driver = Class_1.atStart();


    But there is an error and the methods in class 2 fails.
    The error message is
    “org.openqa.selenium.SessionNotCreatedException: A new session could not be created. (Original error: Requested a new session but one was in progress) (WARNING: The server did not provide any stacktrace information)”

    I understand that driver is trying to create a new session when one is already up.
    Do not know what can be done betterly to continue the same session.

    Can you please help?

    Thanks in Advance!

    Train Answered on December 16, 2014.

    Adding the point to above data, following is textng.xml

    <?xml version=”1.0″ encoding=”UTF-8″?>
    <!DOCTYPE suite SYSTEM “”>
    <suite name=”Suite1″ parallel=”none”>
    <test name=”test”>
    <class name=”Class_1.Master_avaamo”/>
    <class name=”Class_2.Signup”/>

    on December 16, 2014.
    Add Comment

    Resolved 🙂

    WebDriver driver=Class_1.driver;

    Train Answered on December 17, 2014.
    Add Comment
  • Your Answer

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