Passing the single instance of the driver object to all other classes (Testng framework)

18,890

I found a solution myself...When i read about the testng in detail i found that testng xml file calls the default constructor of all classes specified in the xml file.so even if we pass the object to another class we cannot perform the action through the object so null pointer exception occurs....i found two solutions first one is to use a pagefactory and second one is to use a common driver class for your Test suite...so that we can use the same driver instance in all classes

Common driver class

public class Driver {

    public static WebDriver driver=null;



    public static WebDriver startdriver(String browser){


        if(browser.equalsIgnoreCase("Chrome")){

        System.setProperty("webdriver.chrome.driver", "/home/vicky/Documents/Jars/chromedriver");

        driver=new ChromeDriver();

        }else if(browser.equals("Firefox")){

        driver=new FirefoxDriver();

        }
        return driver;

        }

    }
Share:
18,890
Vicky
Author by

Vicky

Oracle Certified Java Programmer Working as an Software Developer in RiskSense

Updated on June 28, 2022

Comments

  • Vicky
    Vicky about 2 years

    I have a driver object initialized in class sample.I want to pass the driver object to other classes also but i get a null pointer exception. My code is

    sample class

        public class sample {
    
        WebDriver driver ;
    
    
        @Test(priority=1)
    
        public void openbrowser(){
    
    
            System.setProperty("webdriver.chrome.driver",
                    "/home/ss4u/Desktop/Vignesh/jars/chromedriver");
    
            driver = new ChromeDriver();
    
            driver.get("http://www.google.com");
    
            System.out.println(driver instanceof WebDriver);
    
    
        }
       @Test(priority=2)
       public void maximize(){
    
          driver.manage().window().maximize();
    
       }
       @Test(priority=3)
       public void transfer_instance(){
    
           sampleone obj=new sampleone(driver);
    
    
       }
    
    }
    

    sampleclassone

    public class sampleone {
    
        WebDriver driver;
    
        public sampleone(WebDriver driver){
    
            this.driver=driver;
    
            System.out.println(driver instanceof WebDriver);
    
            System.out.println(this.driver instanceof WebDriver);
    
            System.out.println("constructor2");
    
    
        }
    
      public sampleone(){
    
            System.out.println("Default constructor called");
    
    
        }
    
    
        @Test(priority=1)
    
         public void gettitle(){
    
              System.out.println(this.driver instanceof WebDriver);
    
              System.out.println(driver instanceof WebDriver);
    
              String title=this.driver.getTitle();
    
              System.out.println(this.driver instanceof WebDriver);
    
              System.out.println(title);
    
              Assert.assertEquals(title, "Google");
    
            }
    
        @Test(priority=2)
    
        public void navigate(){
    
            this.driver.get("https:in.yahoo.com");
    
        }
    
    }
    

    Testng xml file

    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    
    <suite name="TestNG" verbose="1" >
    
        <test name="sample test">
        <classes>
          <class name="testsample.sample" />
        </classes>
        </test>
    
        <test name="sample testone">
        <classes>
          <class name="testsample.sampleone" />
        </classes>
        </test>
    
    </suite>
    

    This issue occurs as iam calling the class not using the object created but using the testng.xml file is there any possible way to create a new java instance(common for all classes) or use the existing instance in all classes