package io.tromba.testdriver.core.logging;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.WebDriverEventListener;

/**
 * Wrapper to log WebDriver events.
 */
public class EventLoggingWebDriver implements WebDriverEventListener {

    private TestdriverLogger logger;

    public EventLoggingWebDriver(TestdriverLogger logger) {
        this.logger = logger;
    }

    public void beforeNavigateTo(String s, WebDriver webDriver) {
        logger.log(LogLevel.INFO, "Starting navigation to: " + s);
    }

    public void afterNavigateTo(String s, WebDriver webDriver) {
        logger.log(LogLevel.INFO, "Navigated to: " + s);
    }

    public void beforeNavigateBack(WebDriver webDriver) {
        logger.log(LogLevel.INFO, "About to navigate back");
    }

    public void afterNavigateBack(WebDriver webDriver) {
        logger.log(LogLevel.INFO, "Navigated back");
    }

    public void beforeNavigateForward(WebDriver webDriver) {
        logger.log(LogLevel.INFO, "About to navigate forward");
    }

    public void afterNavigateForward(WebDriver webDriver) {
        logger.log(LogLevel.INFO, "Navigated forward");
    }

    public void beforeFindBy(By by, WebElement webElement, WebDriver webDriver) {
        logger.log(LogLevel.INFO, "About to FindBy: " + by.toString());
    }

    public void afterFindBy(By by, WebElement webElement, WebDriver webDriver) {
        logger.log(LogLevel.INFO, "After to FindBy: " + by.toString());
    }

    public void beforeClickOn(WebElement webElement, WebDriver webDriver) {
        logger.log(LogLevel.INFO, "About to click on ");
    }

    public void afterClickOn(WebElement webElement, WebDriver webDriver) {
        logger.log(LogLevel.INFO, "Clicked on ");
    }

    public void beforeChangeValueOf(WebElement webElement, WebDriver webDriver) {
        logger.log(LogLevel.INFO, "About to change value of ");
    }

    public void afterChangeValueOf(WebElement webElement, WebDriver webDriver) {
        logger.log(LogLevel.INFO, "Changed value of ");
    }

    public void beforeScript(String s, WebDriver webDriver) {
        logger.log(LogLevel.INFO, "About to execute script " + s);
    }

    public void afterScript(String s, WebDriver webDriver) {
        logger.log(LogLevel.INFO, "Executed script " + s);
    }

    public void onException(Throwable throwable, WebDriver webDriver) {
        logger.log(LogLevel.INFO, "Caught exception: " + throwable.getMessage());
    }
}

@@ -1677,24 +1677,74 @@
 o click on %22
+ + webElement == null ? %22%22 : webElement.toString()
 );%0A    %7D%0A%0A  
@@ -1863,16 +1863,66 @@
 ked on %22
+ + webElement == null ? %22%22 : webElement.toString()
 );%0A    %7D
@@ -2058,32 +2058,82 @@
 hange value of %22
+ + webElement == null ? %22%22 : webElement.toString()
 );%0A    %7D%0A%0A    pu
@@ -2260,16 +2260,67 @@
 lue of %22
+  + webElement == null ? %22%22 : webElement.toString()
 );%0A    %7D

update logging