How to pass the web driver instance from one testcase class to another testcase class in TestNG?



  • InitializeDriver.java:

    public class InitializeDriver{
    
    WebDriver driver;
    
    public WebDriver getDriver() throws Exception{
            driver = new FirefoxDriver();
            System.out.println("Initialize Browser: Browser is initalized");
            return driver;
    }
    
    @BeforeSuite
    public static void openBrowser() {
        driver.get("blah.blah.blah"));
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        System.out.println("openBrowser: Browser is opened");
    }
    
    @AfterSuite
    public static void closeBrowser() {
        driver.quit();
        System.out.println("closeBrowser: Browser is closed");
        }
     }
    

    I have two testcase files.
    1) testcase1.java
    2) testcase2.java

    testing.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="TestSuite">
        <test thread-count="5" name="Test">
            <classes>
                <class name="InitializeDriver" />
                <class name="Testcase1" />
                <class name="Testcase2" />
            </classes>
        </test> <!-- Test -->
    </suite> <!-- Suite -->
    

    Testcase1.java:

      public class Testcase1 {
        WebDriver driver = InitializeDriver.getDriver();
        //some operations
        .....
        .....
        Testcase2 tc2=new Testcase2(driver);
    }
    

    Testcase2.java

    public class Testcase2 {
        //some operations
        .....
        .....
    }
    

    The Output is:

    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    Running TestSuite
    1540192536684   mozrunner::runner   INFO    Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "-foreground" "-no-remote" "-profile" "C:\\Users\\sample\\AppData\\Local\\Temp\\rust_mozprofile.VgaP2iQZZRM1"
    1540192539830   Marionette  INFO    Listening on port 54439
    1540192540037   Marionette  WARN    TLS certificate errors will be ignored for this session
    1540192540101   Marionette  DEBUG   [6442450945] Frame script loaded
    1540192540127   Marionette  DEBUG   [6442450945] Frame script registered
    Oct 22, 2018 12:45:40 PM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: W3C
    
    Initialize Browser: Browser is initalized
    
    1540192540526   Marionette  DEBUG   [6442450945] Received DOM event beforeunload for about:blank
    1540192541179   Marionette  DEBUG   [6442450945] Received DOM event pagehide for about:blank
    1540192543731   Marionette  DEBUG   [6442450945] Received DOM event DOMContentLoaded for blah.blah.blah
    1540192544033   Marionette  DEBUG   [6442450945] Received DOM event pageshow for blah.blah.blah
    
    openBrowser: Browser is opened
    Inside Test Case 1
    
    1540192561608   Marionette  INFO    Stopped listening on port 54439
    [Parent 18364, Gecko_IOThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346
    [Child 3432, Chrome_ChildThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346
    [Child 3432, Chrome_ChildThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346
    [Parent 18364, Gecko_IOThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346
    [Parent 18364, Gecko_IOThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346
    [GPU 4372, Chrome_ChildThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346
    
    ###!!! [Child][MessageChannel::SendAndWait] Error: Channel error: cannot send/recv
    
    closeBrowser: Browser is closed
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 29.642 sec
    
    Results :
    
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 33.065 s
    [INFO] Finished at: 2018-10-22T12:46:04+05:30
    [INFO] Final Memory: 10M/243M
    [INFO] ------------------------------------------------------------------------
    

    It tests only the Testcase1.java and not the testcase2.java.
    Please help me !



  • Here are few notes:

    1. You cannot use this line WebDriver driver = InitializeDriver.getDriver(); because your getDriver() method is not static (according to your example). To call non-static methods you need to instantiate objects first.

    2. I would not recommend to use the same WebDriver instance in different tests because that would make your tests dependent on each other. Since all the changed you will introduce to WebDriver object in one test will have the place in another tests which might impact the logic.

    3. If you use the same driver for all your tests you should carefully use quit() and close() methods.

    4. If you still need to use the same instance you may set your WebDriver as a static field of some class, initialize it once, and then just take that field when you need WebDriver in all your tests.

    P.S. - If you need to force everyone to use the only single driver in all the tests and all the users of your framework, then you can implement Singleton pattern like shown below:

    public class SingletonWebDriver {
    
        private static WebDriver driver;
    
        private SingletonWebDriver(){};
    
        public static WebDriver getDriver(){
            if (driver == null){
                driver = new ChromeDriver(); // Here you can introduce some logic on what particular driver to instantiate.
            }
            return driver;
        }
    }
    

    So having this class implemented you can use import static SingletonWebDriver to your test classes and use getDriver() instead of driver in your code.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2