Skip to content

Commit

Permalink
Allowed for a shared browser between scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
mcasperson committed Sep 13, 2019
1 parent 4e664e0 commit edb4432
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/main/java/com/octopus/decoratorbase/AutomatedBrowserBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.octopus.AutomatedBrowser;
import com.octopus.AutomatedBrowserFactory;
import com.octopus.exceptions.BrowserException;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import org.openqa.selenium.WebDriver;
Expand All @@ -14,6 +15,7 @@ public class AutomatedBrowserBase implements AutomatedBrowser {
static private final AutomatedBrowserFactory AUTOMATED_BROWSER_FACTORY = new AutomatedBrowserFactory();
private Map<String, String> aliases = new HashMap<>();
private AutomatedBrowser automatedBrowser;
private static AutomatedBrowser sharedAutomatedBrowser;

public AutomatedBrowserBase() {

Expand All @@ -24,6 +26,9 @@ public AutomatedBrowserBase(final AutomatedBrowser automatedBrowser) {
}

public AutomatedBrowser getAutomatedBrowser() {
if (sharedAutomatedBrowser != null)
return sharedAutomatedBrowser;

return automatedBrowser;
}

Expand All @@ -32,10 +37,19 @@ public void setAliases(Map<String, String> aliases) {
this.aliases.putAll(aliases);
}

@Given("^I open the browser \"([^\"]*)\"$")
public void openBrowser(String browser) {
automatedBrowser = AUTOMATED_BROWSER_FACTORY.getAutomatedBrowser(browser);
automatedBrowser.init();
@Given("^I open the( shared ) browser \"([^\"]*)\"$")
public void openBrowser(String shared, String browser) {
if (shared != null) {
sharedAutomatedBrowser = AUTOMATED_BROWSER_FACTORY.getAutomatedBrowser(browser);
sharedAutomatedBrowser.init();
} else {
if (sharedAutomatedBrowser != null) {
throw new BrowserException("Can not open a browser with an existing shared browser.");
}

automatedBrowser = AUTOMATED_BROWSER_FACTORY.getAutomatedBrowser(browser);
automatedBrowser.init();
}
}

@Given("^I close the browser$")
Expand All @@ -44,6 +58,11 @@ public void closeBrowser() {
automatedBrowser.destroy();
automatedBrowser = null;
}

if (sharedAutomatedBrowser != null) {
sharedAutomatedBrowser.destroy();
sharedAutomatedBrowser = null;
}
}

@And("^I set the default explicit wait time to \"(\\d+)\" seconds?$")
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/octopus/exceptions/BrowserException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.octopus.exceptions;

public class BrowserException extends RuntimeException {

public BrowserException() {

}

public BrowserException(final String message) {
super(message);
}

public BrowserException(final Throwable cause) {
super(cause);
}

public BrowserException(final String message, final Throwable cause) {
super(message, cause);
}
}

0 comments on commit edb4432

Please sign in to comment.