package ca.corefacility.bioinformatics.irida.web.controller.test.listeners;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.RunListener;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.junit.Assert.fail;

/**
 * Global settings for UI integration tests.
 * 
 */
public class IntegrationUITestListener extends RunListener {
	private static final Logger logger = LoggerFactory.getLogger(IntegrationUITestListener.class);
	public static final int DRIVER_TIMEOUT_IN_SECONDS = 3;

	private static WebDriver driver;

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void testRunStarted(Description description) throws Exception {
		logger.debug("Running ChromeDriver for UI tests.");
		startWebDriver();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void testRunFinished(Result result) throws Exception {
		logger.debug("Closing ChromeDriver for UI tests.");
		stopWebDriver();
	}

	/**
	 * Get a reference to the {@link WebDriver} used in the tests.
	 * 
	 * @return the instance of {@link WebDriver} used in the tests.
	 */
	public static WebDriver driver() {
		return driver;
	}

	/**
	 * Start the web driver.
	 */
	public static void startWebDriver() {
		DesiredCapabilities capabilities = DesiredCapabilities.chrome();

		/*
		 * Run chrome in no sandbox mode. Only use this option for running tests
		 * in docker containers.
		 */
		String noSandbox = System.getProperty("irida.it.nosandbox");
		if (noSandbox != null && noSandbox.equals("true")) {
			logger.warn("Running Chrome in no sandbox mode");
			capabilities.setCapability("no-sandbox",true);
		}

		// Run chrome in headless mode
		String headless = System.getProperty("irida.it.headless");
		if (headless != null && headless.equals("true")) {
			logger.info("Running Chome in headless mode");
			capabilities.setCapability("headless", true);
		} else {
			logger.info("Running Chome in no headless (normal) mode");
		}

		capabilities.setCapability("window-size=1920,1080", true);

		// Run selenium tests through external selenium server
		String seleniumUrl = System.getProperty("webdriver.selenium_url");
		if (seleniumUrl != null) {
			try {
				driver = new RemoteWebDriver(new URL(seleniumUrl), capabilities);
			} catch (MalformedURLException e) {
				fail();
			}
		} else {
			driver = new ChromeDriver(capabilities);
		}

		driver.manage().timeouts().implicitlyWait(DRIVER_TIMEOUT_IN_SECONDS, TimeUnit.SECONDS);
	}

	public static void stopWebDriver() {
		driver.quit();
	}
}
@@ -366,32 +366,81 @@
 e.ChromeDriver;%0A
+import org.openqa.selenium.chrome.ChromeOptions;%0A
 import org.openq
@@ -1575,16 +1575,63 @@
 ver() %7B%0A
+%09%09ChromeOptions options = new ChromeOptions();%0A
 %09%09Desire
@@ -1689,16 +1689,16 @@
 rome();%0A
-
 %0A%09%09/*%0A%09%09
@@ -1981,36 +1981,32 @@
 %0A%09%09%09
-capabilities.setCapability(%22
+options.addArguments(%22--
 no-s
@@ -2016,13 +2016,8 @@
 box%22
-,true
 );%0A%09
@@ -2224,34 +2224,28 @@
 %0A%09%09%09
-capabilities.setCapability
+options.addArguments
 (%22he
@@ -2251,22 +2251,16 @@
 eadless%22
-, true
 );%0A%09%09%7D e
@@ -2338,64 +2338,120 @@
 %0A%0A%09%09
-capabilities.setCapability(%22window-size=1920,1080%22, true
+options.addArguments(%22--window-size=1920,1080%22);%0A%0A%09%09capabilities.setCapability(ChromeOptions.CAPABILITY, options
 );%0A%0A

Fix chromedriver to use chromeoptions