/*
 * (C) Copyright 2017 Boni Garcia (http://bonigarcia.github.io/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package io.github.bonigarcia;

import static java.lang.invoke.MethodHandles.lookup;
import static org.slf4j.LoggerFactory.getLogger;

import java.util.Scanner;

import org.openqa.selenium.WebDriver;
//import org.junit.jupiter.api.TestTemplate;
//import org.junit.jupiter.api.extension.ExtensionContext;
import org.slf4j.Logger;

import io.github.bonigarcia.config.Config;
import io.github.bonigarcia.handler.DockerDriverHandler;

/**
 * Main class for interactive mode.
 *
 * @author Boni Garcia (boni.gg@gmail.com)
 * @since 1.0.0
 */
public class SeleniumJupiter {

    static final Logger log = getLogger(lookup().lookupClass());

    protected static Config config;

    public static synchronized Config config() {
        if (config == null) {
            config = new Config();
        }
        return config;
    }

    public static void main(String[] args) {
        String validBrowsers = "chrome|firefox|opera";
        if (args.length <= 0) {
            log.error("Usage: SeleniumJupiter browserName <version>",
                    validBrowsers);
            log.error("\t...where");
            log.error("\tbrowserName = {}", validBrowsers);
            log.error("\tversion = optional version (latest if empty)");
        } else {
            String browser = args[0];
            String version = "";
            String versionMessage = "(latest)";
            if (args.length > 1) {
                version = args[1];
                versionMessage = version;
            }
            log.info("Using SeleniumJupiter to execute {} {} in Docker",
                    browser, versionMessage);
            try {
                config().setVnc(true);
                DockerDriverHandler dockerDriverHandler = new DockerDriverHandler();

                BrowserType browserType = BrowserType
                        .valueOf(browser.toUpperCase());
                browserType.init();

                WebDriver webdriver = dockerDriverHandler.resolve(browserType,
                        version);

                log.info("Press ENTER to exit");
                Scanner scanner = new Scanner(System.in);
                scanner.nextLine();
                scanner.close();

                webdriver.quit();
                dockerDriverHandler.cleanup();
                dockerDriverHandler.close();

            } catch (Exception e) {
                log.error("Exception trying to execute {} {} in Docker",
                        browser, versionMessage, e);
            }
        }
    }

}

@@ -655,16 +655,60 @@
 arcia;%0A%0A
+import static java.lang.Runtime.getRuntime;%0A
 import s
@@ -2278,24 +2278,25 @@
 onMessage);%0A
+%0A
             
@@ -2684,24 +2684,270 @@
  version);%0A%0A
+                getRuntime().addShutdownHook(new Thread() %7B%0A                    @Override%0A                    public void run() %7B%0A                        cleanContainers(dockerDriverHandler, webdriver);%0A                    %7D%0A                %7D);%0A%0A
             
@@ -3131,115 +3131,54 @@
     
-webdriver.quit();%0A                dockerDriverHandler.cleanup();%0A                dockerDriverHandler.close(
+cleanContainers(dockerDriverHandler, webdriver
 );%0A%0A
@@ -3335,24 +3335,24 @@
 essage, e);%0A
-
             
@@ -3370,10 +3370,343 @@
 %0A    %7D%0A%0A
+    private static void cleanContainers(DockerDriverHandler dockerDriverHandler,%0A            WebDriver webdriver) %7B%0A        if (webdriver != null) %7B%0A            webdriver.quit();%0A        %7D%0A        if (dockerDriverHandler != null) %7B%0A            dockerDriverHandler.cleanup();%0A            dockerDriverHandler.close();%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Add shutdown hook to clean containers if needed in interactive mode