Skip to content

Commit edb4432

Browse files
committed
Allowed for a shared browser between scenarios
1 parent 4e664e0 commit edb4432

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

src/main/java/com/octopus/decoratorbase/AutomatedBrowserBase.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.octopus.AutomatedBrowser;
44
import com.octopus.AutomatedBrowserFactory;
5+
import com.octopus.exceptions.BrowserException;
56
import cucumber.api.java.en.And;
67
import cucumber.api.java.en.Given;
78
import 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?$")
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.octopus.exceptions;
2+
3+
public class BrowserException extends RuntimeException {
4+
5+
public BrowserException() {
6+
7+
}
8+
9+
public BrowserException(final String message) {
10+
super(message);
11+
}
12+
13+
public BrowserException(final Throwable cause) {
14+
super(cause);
15+
}
16+
17+
public BrowserException(final String message, final Throwable cause) {
18+
super(message, cause);
19+
}
20+
}

0 commit comments

Comments
 (0)