22
33import com .octopus .AutomatedBrowser ;
44import com .octopus .AutomatedBrowserFactory ;
5+ import com .octopus .exceptions .BrowserException ;
56import cucumber .api .java .en .And ;
67import cucumber .api .java .en .Given ;
78import org .openqa .selenium .WebDriver ;
@@ -14,6 +15,7 @@ public class AutomatedBrowserBase implements AutomatedBrowser {
1415 static private final AutomatedBrowserFactory AUTOMATED_BROWSER_FACTORY = new AutomatedBrowserFactory ();
1516 private Map <String , String > aliases = new HashMap <>();
1617 private AutomatedBrowser automatedBrowser ;
18+ private static AutomatedBrowser sharedAutomatedBrowser ;
1719
1820 public AutomatedBrowserBase () {
1921
@@ -24,6 +26,9 @@ public AutomatedBrowserBase(final AutomatedBrowser automatedBrowser) {
2426 }
2527
2628 public AutomatedBrowser getAutomatedBrowser () {
29+ if (sharedAutomatedBrowser != null )
30+ return sharedAutomatedBrowser ;
31+
2732 return automatedBrowser ;
2833 }
2934
@@ -32,10 +37,19 @@ public void setAliases(Map<String, String> aliases) {
3237 this .aliases .putAll (aliases );
3338 }
3439
35- @ Given ("^I open the browser \" ([^\" ]*)\" $" )
36- public void openBrowser (String browser ) {
37- automatedBrowser = AUTOMATED_BROWSER_FACTORY .getAutomatedBrowser (browser );
38- automatedBrowser .init ();
40+ @ Given ("^I open the( shared ) browser \" ([^\" ]*)\" $" )
41+ public void openBrowser (String shared , String browser ) {
42+ if (shared != null ) {
43+ sharedAutomatedBrowser = AUTOMATED_BROWSER_FACTORY .getAutomatedBrowser (browser );
44+ sharedAutomatedBrowser .init ();
45+ } else {
46+ if (sharedAutomatedBrowser != null ) {
47+ throw new BrowserException ("Can not open a browser with an existing shared browser." );
48+ }
49+
50+ automatedBrowser = AUTOMATED_BROWSER_FACTORY .getAutomatedBrowser (browser );
51+ automatedBrowser .init ();
52+ }
3953 }
4054
4155 @ Given ("^I close the browser$" )
@@ -44,6 +58,11 @@ public void closeBrowser() {
4458 automatedBrowser .destroy ();
4559 automatedBrowser = null ;
4660 }
61+
62+ if (sharedAutomatedBrowser != null ) {
63+ sharedAutomatedBrowser .destroy ();
64+ sharedAutomatedBrowser = null ;
65+ }
4766 }
4867
4968 @ And ("^I set the default explicit wait time to \" (\\ d+)\" seconds?$" )
0 commit comments