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:




    
        
            
            
            
        
     
 

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 !

Leave a Reply

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