Skip to main content
added 4 characters in body
Source Link
Vivek Sadh
  • 4.3k
  • 3
  • 35
  • 55

For using one browser to run all test cases use singleton design pattern i.e make class with private constructor and define class instance variable with a private access specifier.Create a method in that class and check that class is null or not and if it is null than create a new instance of class and return that instance to calling method.for example i am posting my code.

class OpenBrowserHelp { private WebDriver driver; private static OpenBrowserHelp browserHelp; private OpenBrowserHelp() { this.driver = new FirefoxDriver() driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); driver.manage().window().maximize(); } public static OpenBrowserHelp getOpenBrowserHelp() { if (null == browserHelp) { browserHelp = new OpenBrowserHelp(); } return browserHelp; } WebDriver getDriver() { return driver } void setDriver(WebDriver driver) { this.driver = driver } public void printSingleton() { System.out.println("Inside print Singleton"); } 

Now, where ever you need to create browser instance than use

WebDriver driver = OpenBrowserHelp.getOpenBrowserHelp().getDriver();

 WebDriver driver = OpenBrowserHelp.getOpenBrowserHelp().getDriver(); 

For using one browser to run all test cases use singleton design pattern i.e make class with private constructor and define class instance variable with a private access specifier.Create a method in that class and check that class is null or not and if it is null than create a new instance of class and return that instance to calling method.for example i am posting my code.

class OpenBrowserHelp { private WebDriver driver; private static OpenBrowserHelp browserHelp; private OpenBrowserHelp() { this.driver = new FirefoxDriver() driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); driver.manage().window().maximize(); } public static OpenBrowserHelp getOpenBrowserHelp() { if (null == browserHelp) { browserHelp = new OpenBrowserHelp(); } return browserHelp; } WebDriver getDriver() { return driver } void setDriver(WebDriver driver) { this.driver = driver } public void printSingleton() { System.out.println("Inside print Singleton"); } 

Now, where ever you need to create browser instance than use

WebDriver driver = OpenBrowserHelp.getOpenBrowserHelp().getDriver();

For using one browser to run all test cases use singleton design pattern i.e make class with private constructor and define class instance variable with a private access specifier.Create a method in that class and check that class is null or not and if it is null than create a new instance of class and return that instance to calling method.for example i am posting my code.

class OpenBrowserHelp { private WebDriver driver; private static OpenBrowserHelp browserHelp; private OpenBrowserHelp() { this.driver = new FirefoxDriver() driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); driver.manage().window().maximize(); } public static OpenBrowserHelp getOpenBrowserHelp() { if (null == browserHelp) { browserHelp = new OpenBrowserHelp(); } return browserHelp; } WebDriver getDriver() { return driver } void setDriver(WebDriver driver) { this.driver = driver } public void printSingleton() { System.out.println("Inside print Singleton"); } 

Now, where ever you need to create browser instance than use

 WebDriver driver = OpenBrowserHelp.getOpenBrowserHelp().getDriver(); 
Source Link

For using one browser to run all test cases use singleton design pattern i.e make class with private constructor and define class instance variable with a private access specifier.Create a method in that class and check that class is null or not and if it is null than create a new instance of class and return that instance to calling method.for example i am posting my code.

class OpenBrowserHelp { private WebDriver driver; private static OpenBrowserHelp browserHelp; private OpenBrowserHelp() { this.driver = new FirefoxDriver() driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); driver.manage().window().maximize(); } public static OpenBrowserHelp getOpenBrowserHelp() { if (null == browserHelp) { browserHelp = new OpenBrowserHelp(); } return browserHelp; } WebDriver getDriver() { return driver } void setDriver(WebDriver driver) { this.driver = driver } public void printSingleton() { System.out.println("Inside print Singleton"); } 

Now, where ever you need to create browser instance than use

WebDriver driver = OpenBrowserHelp.getOpenBrowserHelp().getDriver();