package browserview;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.NoSuchElementException;

// acts as a middleman for ChromeDriver when not in testing mode,
// and as a stub when testing

public class ChromeDriverEx {

    private boolean isTestChromeDriver;
    private ChromeDriver driver;

    ChromeDriverEx(ChromeOptions options, boolean isTestChromeDriver) {
        this.isTestChromeDriver = isTestChromeDriver;
        initialise(options);
    }

    private void initialise(ChromeOptions options) {
        if (!isTestChromeDriver) driver = new ChromeDriver(options);
    }

    public WebDriver.Options manage() {
        return !isTestChromeDriver ? driver.manage() : null;
    }

    public void quit() {
        if (!isTestChromeDriver) driver.quit();
    }

    public void get(String url) throws WebDriverException {
        if (isTestChromeDriver) {
            testGet();
        } else {
            driver.get(url);
        }
    }

    public void testGet() throws WebDriverException {
        double chance = Math.random();
        if (chance < 0.25) {
            throw new WebDriverException("no such window");
        } else if (chance < 0.5) {
            throw new WebDriverException("no such element");
        } else if (chance < 0.75) {
            throw new WebDriverException("unexpected alert open");
        }
    }

    public String getCurrentUrl() {
        return !isTestChromeDriver ? driver.getCurrentUrl() :
                "https://github.com/HubTurbo/HubTurbo/issues/1";
    }

    public WebElement findElementById(String id) throws NoSuchElementException {
        if (!isTestChromeDriver) return driver.findElementById(id);
        throw new NoSuchElementException();
    }

    public WebElement findElementByTagName(String tag) throws NoSuchElementException {
        if (!isTestChromeDriver) return driver.findElementByTagName(tag);
        throw new NoSuchElementException();
    }

    public WebDriver.TargetLocator switchTo() throws WebDriverException {
        if (!isTestChromeDriver) {
            return driver.switchTo();
        } else {
            // ~25% chance to throw an exception which is used to test resetBrowser
            if (Math.random() < 0.25) {
                throw new WebDriverException();
            }
        }
        return null;
    }

    public String getWindowHandle() {
        return !isTestChromeDriver ? driver.getWindowHandle() : "";
    }

    public WebElement findElement(By by) throws NoSuchElementException {
        if (!isTestChromeDriver) return driver.findElement(by);
        throw new NoSuchElementException();
    }

    public Object executeScript(String script) {
        return !isTestChromeDriver ? driver.executeScript(script) : "";
    }
}

@@ -15,16 +15,100 @@
 rview;%0A%0A
+import org.apache.logging.log4j.LogManager;%0Aimport org.apache.logging.log4j.Logger;%0A
 import o
@@ -522,16 +522,112 @@
 erEx %7B%0A%0A
+    private static final Logger logger = LogManager.getLogger(ChromeDriverEx.class.getName());%0A%0A
     priv
@@ -1257,32 +1257,86 @@
 ChromeDriver) %7B%0A
+            logger.info(%22Test loading page: %22 + url);%0A
             test
@@ -1371,16 +1371,20 @@
         
+if (
 driver.g
@@ -1389,14 +1389,242 @@
 .get
-(url);
+CurrentUrl().equals(url)) %7B%0A                logger.info(%22Already on page: %22 + url + %22 will not load it again. %22);%0A            %7D else %7B%0A                logger.info(%22Loading page: %22 + url);%0A                driver.get(url);%0A            %7D
 %0A   

put logging and same url checking in ChromeDriverEx