I am new to cucumber testing as well as selenium testing.Please help me to run all cucumber test cases in single browser.As for now i am creating new WebDriver object in each cucumber step_def for feature file.
2 Answers
The solution is, Using / passing the same Web Driver object across your step_def. From your Question i assume, you have multiple Step Def files, If the stories are small and related put all of them in a single step_def file and have a single Web driver object. If it is not the case, invoke every step_def with a predefined Driver object that is globally declared in the configuration loader.
2 Comments
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();