title: How to use the same browser window in multiple classes using TestNG Selenium webdriver in java?
body: <p>I am trying to automate a webpage which has a login and post login has many menu item. I would like to automate it in such a way that it logs into the webpage only once and then use the different menu items. Each new menu item automation is created in a different class.</p>
  
  <pre><code>package pack1;
  
  public class Init {
  
      public WebDriver driver;
  
      ChromeOptions options;
  
      @BeforeSuite
      public void beforeSuite() throws AWTException, InterruptedException, IOException {
          //Setting Chrome Driver and disabling the save password option
          System.setProperty(“webdriver.chrome.driver”,”C:\\Users\\user\\Desktop\\Demo\\chromedriver.exe”);
          options = new ChromeOptions();
          Map&lt;String, Object&gt; prefs = new HashMap&lt;String, Object&gt;();
          prefs.put(“credentials_enable_service”, false);
          prefs.put(“profile.password_manager_enabled”, false);
          options.setExperimentalOption(“prefs”, prefs);
          driver=new ChromeDriver(options);
  
          //Opening the URL
          driver.get(“myURL”);
          driver.manage().window().maximize();
  
          //Login to the portal
          driver.findElement(By.xpath(“.//*[@id=’content-wrapper’]/div/div/div/div/div/div/div/form/div/div[1]/input”)).sendKeys(username);
          driver.findElement(By.xpath(“.//*[@id=’content-wrapper’]/div/div/div/div/div/div/div/form/div/div[2]/input”)).sendKeys(password);
          driver.findElement(By.xpath(“.//*[@id=’content-wrapper’]/div/div/div/div/div/div/div/form/div/div[3]/button”)).click();
  
      }
  
      @AfterSuite
      public void afterSuite() {
          //Closing the driver
          // driver.close();
      }
  }
  </code></pre>
  
  <p>Class A</p>
  
  <pre><code>    package pack1;
  
  public class ClassA extends Init{
  
      @Test (priority=0, enabled = true)
      public void Setup() throws InterruptedException{
          //Traversing the menu to reach contract grower setup
          Thread.sleep(5000);
          driver.findElement(By.linkText(“Menu1”)).click();
          driver.findElement(By.linkText(“SubMenu1”)).click();
  
      }
  
  }
  </code></pre>
  
  <p>Class B</p>
  
  <pre><code>    package pack1;
  
  public class ClassBextends Init{
  
      @Test (priority=0, enabled = true)
      public void Setup() throws InterruptedException{
          //Traversing the menu to reach contract grower setup
          Thread.sleep(5000);
          driver.findElement(By.linkText(“Menu2”)).click();
          driver.findElement(By.linkText(“SubMenu2”)).click();
      }
  
  }
  </code></pre>
  
  <p>testing.xml</p>
  
  <pre><code> &lt;?xml version=”1.0″ encoding=”UTF-8″?&gt;
  &lt;!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd”&gt;
  &lt;suite name=”Suite”&gt;
      &lt;test name=”Test”&gt;
          &lt;classes&gt;
              &lt;class name=”pack1.ClassA”/&gt;
              &lt;class name=”pack1.ClassB”/&gt;
              &lt;class name=”pack1.Init”/&gt;
          &lt;/classes&gt;
      &lt;/test&gt; &lt;!– Test –&gt;
  &lt;/suite&gt; &lt;!– Suite –&gt;
  </code></pre>