title: How to pass single instance of WebDriver between TestNg classes
body: <p>I couldnt find a difinitive answer to this anywhere on the net.</p>
  
  <p>I have multiple TestNg classes to run tests, BrowserFunctions, Login, Search, Filter (testing Amazon uk for practice). I also have a BrowserLauncher class that returns the appropriate webdriver based on browser name, and a testng.xml file.</p>
  
  <p>BrowserFunctions.java</p>
  
  <pre><code>public class BrowserFunctions {
  
  BrowserLauncher bl = new BrowserLauncher();
  WebDriver driver;
  StringBuilder sb = new StringBuilder();
  
  @BeforeSuite
  public void initialioseBrowser() {
      driver = bl.launchBrowser("Firefox");
  }
  
  @Parameters({ "URL" })
  @BeforeSuite
  public void invokeURL(String URL) {
      driver.get(URL);
  }
  
  @AfterSuite
  public void closeBrowser() {
      driver.close();
  }
  </code></pre>
  
  <p>Login.java</p>
  
  <pre><code>public class Login {
  
  BrowserLauncher bl = new BrowserLauncher();
  WebDriver driver;
  StringBuilder sb = new StringBuilder();
  
  @Parameters({ "email", "password" })
  @Test
  public void logInTest(String email, String passowrd) {
      Assert.assertTrue(CommonFunctions.checkVisibility(driver, PageElements.amzUKsignInCTA), "Sign in CTA visible");
      CommonFunctions.clickButton(driver, PageElements.amzUKsignInCTA);
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      Assert.assertTrue(CommonFunctions.checkVisibility(driver, PageElements.amzUKEmailField), "Email field visible");
      CommonFunctions.inputToField(driver, PageElements.amzUKEmailField, email);
      Assert.assertTrue(CommonFunctions.checkVisibility(driver, PageElements.amzUKPasswordField),
              "Password field visible");
      CommonFunctions.inputToField(driver, PageElements.amzUKPasswordField, passowrd);
      Assert.assertTrue(CommonFunctions.checkVisibility(driver, PageElements.amzUKSignInButton),
              "Sign in button visible");
      CommonFunctions.clickButton(driver, PageElements.amzUKSignInButton);
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      Assert.assertEquals(driver.getCurrentUrl(), "https://www.amazon.co.uk/?ref_=nav_ya_signin&amp;");
  }
  </code></pre>
  
  <p>Search.java</p>
  
  <pre><code>public class Search {
  
  BrowserLauncher bl = new BrowserLauncher();
  WebDriver driver;
  StringBuilder sb = new StringBuilder();
  
  @Parameters({ "searchTerm" })
  @Test
  public void searchTest(String searchTerm) {
      Assert.assertTrue(CommonFunctions.checkVisibility(driver, PageElements.amzUKSearchField),
              "Search field visible");
      CommonFunctions.inputToField(driver, PageElements.amzUKSearchField, searchTerm);
      Assert.assertTrue(CommonFunctions.checkVisibility(driver, PageElements.amzUKSearchButton),
              "Search button visible");
      CommonFunctions.clickButton(driver, PageElements.amzUKSearchButton);
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  
      Assert.assertTrue(CommonFunctions.checkVisibility(driver, PageElements.amzUKResultContainer),
              "Results container visible");
      if (driver.findElements(PageElements.amzUKResultContainer).size() &gt; 0) {
          List&lt;WebElement&gt; resultContainerList = driver.findElements(PageElements.amzUKResultContainer);
          for (WebElement w : resultContainerList) {
              if (w.findElements(PageElements.amzUKResultTitle).size() &gt; 0) {
                  if (w.findElement(PageElements.amzUKResultTitle).getText().contains(searchTerm)) {
                  } else {
                      sb.append(w.findElement(PageElements.amzUKResultTitle).getText() + " does not contain"
                              + searchTerm + "\n");
                  }
              }
              Assert.assertTrue(CommonFunctions.checkVisibility(driver, PageElements.amzUKResultImage),
                      "Result image visible");
              if (w.findElements(PageElements.amzUKResultImage).size() &gt; 0) {
              } else {
                  sb.append(w.findElement(PageElements.amzUKResultTitle).getText() 
                          + " does not contain an image\n");
              }
              Assert.assertTrue(CommonFunctions.checkVisibility(driver, PageElements.amzUKResultPrice),
                      "Result price visible");
              if (w.findElements(PageElements.amzUKResultPrice).size() &gt; 0) {
                  if (w.findElement(PageElements.amzUKResultPrice).getText().contains("£")) {
                  } else {
                      sb.append(w.findElement(PageElements.amzUKResultTitle).getText() 
                              + " does not contain a price\n");
                  }
              }
          }
      }
      System.out.println("searchTest(" + searchTerm + ") Failures:\n" + sb.toString());
  }
  </code></pre>
  
  <p>Filter.java
  public class Filter {</p>
  
  <pre><code>BrowserLauncher bl = new BrowserLauncher();
  WebDriver driver;
  StringBuilder sb = new StringBuilder();
  
  @Test
  public void filterTest() {
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      List&lt;String&gt; filterOptionsList = new ArrayList&lt;String&gt;();
  
      sb.append(CommonFunctions.clickButton(driver, PageElements.amzUKFilterSeeMore));
  
      Assert.assertTrue(CommonFunctions.checkVisibility(driver, PageElements.amzUKFilterOptions),
              "Filter options visible");
      for (int i = 0; i &lt; driver.findElements(PageElements.amzUKFilterOptions).size(); i++) {
          filterOptionsList.add(driver.findElements(PageElements.amzUKFilterOptions).get(i).getText());
      }
  
      for (String s : filterOptionsList) {
          Assert.assertTrue(CommonFunctions.checkVisibility(driver, By.partialLinkText(s)), "" 
                  + s + " link visible");
          if (driver.findElements(By.partialLinkText(s)).size() &gt; 0) {
              driver.findElement(By.partialLinkText(s)).click();
          }else {
              sb.append("Link " + s + " not visible");
              continue;
          }
          driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
          Assert.assertTrue(CommonFunctions.checkVisibility(driver, PageElements.amzUKFilterAnyCategory),
                  "\"Any Category\" button visible");
          CommonFunctions.clickButton(driver, PageElements.amzUKFilterAnyCategory);
          driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
          sb.append(CommonFunctions.clickButton(driver, PageElements.amzUKFilterSeeMore));
          driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      }
      System.out.println("filterTest Failures:\n" + sb.toString());
  }
  </code></pre>
  
  <p>BrowserLauncher.java</p>
  
  <pre><code>public WebDriver launchBrowser(String BrowserName) {
      if(BrowserName.equalsIgnoreCase("Firefox")) {
          driver = new FirefoxDriver();
          driver.manage().window().maximize();
      } else if(BrowserName.equalsIgnoreCase("IE")) {
          DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
          capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
          capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
          capabilities.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, "");
          System.setProperty("webdriver.ie.driver", "C:\\Eclipse EE x86 Workspace\\ResidentAdvisorLabels\\IEDriverServer.exe");
          driver = new InternetExplorerDriver(capabilities);
          driver.get("javascript:document.getElementById('overridelink').click();");
      } else if(BrowserName.equalsIgnoreCase("Chrome")) {
          ChromeOptions op = new ChromeOptions();
          op.addArguments("--user-data-dir=C:\\Users\\Bernard\\Desktop\\Selenium Data");
          op.addArguments("--start-maximized");
          DesiredCapabilities capabilities = new DesiredCapabilities();
          capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
          System.setProperty("webdriver.chrome.driver", "C:\\Eclipse EE x86 Workspace\\ResidentAdvisorLabels\\chromedriver.exe");
          driver = new ChromeDriver(op);
          driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
      } else if(BrowserName.equalsIgnoreCase("PhantomJS")) {
          DesiredCapabilities capabilities = new DesiredCapabilities();
          capabilities.setCapability("takesScreenshot", false);
          String d = "\\";
          capabilities.setCapability("phantomjs.binary.path", "C:"+d+"Eclipse EE x86 Workspace"+d+"phantomjs-2.0.0-windows"+d+"bin"+d+"phantomjs.exe");
          capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
          capabilities.setCapability("load-images", false);
          driver = new PhantomJSDriver(capabilities);
          driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
      }
  
      return driver;
  }
  </code></pre>
  
  <p>testng.xml</p>
  
  <pre><code>&lt;suite name="Suite1" verbose="1"&gt;
  &lt;parameter name="searchTerm" value="Selenium Webdriver" /&gt;
  &lt;parameter name="URL" value="Http://www.amazon.co.uk" /&gt;
  &lt;parameter name="email" value="scrubbed" /&gt;
  &lt;parameter name="password" value="scrubbed" /&gt;
  &lt;test name="AmazonUKTesting"&gt;
      &lt;classes&gt;
          &lt;class name="tests.amazonUKTests.BrowserFunctions" /&gt;
          &lt;class name="tests.amazonUKTests.Login" /&gt;
          &lt;class name="tests.amazonUKTests.Search" /&gt;
          &lt;class name="tests.amazonUKTests.Filter" /&gt;
      &lt;/classes&gt;
  &lt;/test&gt;
  </code></pre>
  
  <p></p>
  
  <p>My problem is that i cannot seem to be able to pass the WebDriver that is returned to the BrowserFunctions.java class on to the other classes in my test suite without having to create a new instance of WebDriver in each java file. I am looking to open the browser, open the URL, then run the 3 tests one after another in the same browser window, and have the browser close when done.</p>