package cucumber.examples.java.websockets;

import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.Scenario;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;

/**
 * Example of a WebDriver implementation that has an underlying instance that is used for all scenarios and only closed
 * when the JVM exits. This makes scenarios a lot faster since opening and closing a browser for each scenario is pretty slow.
 * To prevent browser state from leaking between scenarios, cookies are deleted before every scenario.
 * <p/>
 * As a bonus, screenshots are embedded into the report for each scenario. (This only works
 * if you're also using the HTML formatter).
 * <p/>
 * This class can be shared across step definitions via dependency injection.
 */
public class SharedDriver extends EventFiringWebDriver {
    private static final WebDriver REAL_DRIVER = new ChromeDriver();
    private static final Thread CLOSE_THREAD = new Thread() {
        @Override
        public void run() {
            REAL_DRIVER.close();
        }
    };

    static {
        Runtime.getRuntime().addShutdownHook(CLOSE_THREAD);
    }

    public SharedDriver() {
        super(REAL_DRIVER);
    }

    @Override
    public void close() {
        if(Thread.currentThread() != CLOSE_THREAD) {
            throw new UnsupportedOperationException("You shouldn't close this WebDriver. It's shared and will close when the JVM exits.");
        }
        super.close();
    }

    @Before
    public void deleteAllCookies() {
        manage().deleteAllCookies();
    }

    @After
    public void embedScreenshot(Scenario scenario) {
        try {
            byte[] screenshot = getScreenshotAs(OutputType.BYTES);
            scenario.embed(screenshot, "image/png");
        } catch (WebDriverException somePlatformsDontSupportScreenshots) {
            System.err.println(somePlatformsDontSupportScreenshots.getMessage());
        }
    }
}

@@ -373,16 +373,23 @@
 r;%0A%0A/**%0A
+ * %3Cp%3E%0A
  * Examp
@@ -434,68 +434,154 @@
 has 
-an underlying instance that is used for all scenarios and on
+delegates all methods to a static instance (REAL_DRIVER) that is only %0A * created once for the duration of the JVM. The REAL_DRIVER is automatical
 ly c
@@ -585,19 +585,16 @@
 y closed
-%0A *
  when th
@@ -617,16 +617,20 @@
 s makes 
+%0A * 
 scenario
@@ -786,16 +786,30 @@
 ies are 
+automatically 
 deleted 
@@ -831,26 +831,170 @@
 enario.%0A * %3C
-p
 /
+p%3E%0A * %3Cp%3E%0A * A new instance of SharedDriver is created for each Scenario and passed to yor Stepdef classes via Dependency Injection%0A * %3C/p%3E%0A * %3Cp
 %3E%0A * As a bo
@@ -1128,53 +1128,115 @@
  * %3C
-p
 /
+p
 %3E%0A * 
-This class can be s
+%3Cp%3E%0A * A new instance of the S
 hared
- across s
+Driver is created for each Scenario and then passed to the S
 tep 
-d
+D
 efin
@@ -1244,35 +1244,173 @@
 tion
-s via dependency injection.
+ classes'%0A * constructor. They all receive a reference to the same instance. However, the REAL_DRIVER is the same instance throughout%0A * the life of the JVM.%0A * %3C/p%3E
 %0A */

Update javadoc