package com.codeborne.selenide;

import org.junit.Test;
import org.openqa.selenium.WebDriver;

import static com.codeborne.selenide.WebDriverRunner.FIREFOX;
import static com.codeborne.selenide.WebDriverRunner.cleanupWebDriverExceptionMessage;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.mock;

public class WebDriverRunnerTest {

  static WebDriver driver = mock(WebDriver.class);

  @Test
  public void allowsToSpecifyCustomWebDriverConfiguration() {
    WebDriverRunner.closeWebDriver();
    Configuration.browser = "com.codeborne.selenide.WebDriverRunnerTest$CustomWebDriverProvider";

    try {
      assertSame(driver, WebDriverRunner.getWebDriver());
    } finally {
      WebDriverRunner.closeWebDriver();
      Configuration.browser = System.getProperty("browser", FIREFOX);
    }
  }

  @Test
  public void cleansWebDriverExceptionMessage() {
    String webdriverException = "org.openqa.selenium.NoSuchElementException: The element could not be found (WARNING: The server did not provide any stacktrace information)\n" +
        "Command duration or timeout: 21 milliseconds\n" +
        "For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html\n" +
        "Build info: version: '2.29.1', revision: 'dfb1306b85be4934d23c123122e06e602a15e446', time: '2013-01-22 12:58:05'\n" +
        "System info: os.name: 'Linux', os.arch: 'amd64', os.version: '3.5.0-23-generic', java.version: '1.7.0_10'\n" +
        "Session ID: 610138404f5c180a4f3153785e66c528\n" +
        "Driver info: org.openqa.selenium.chrome.ChromeDriver\n" +
        "Capabilities [{platform=LINUX, chrome.chromedriverVersion=26.0.1383.0, acceptSslCerts=false, javascriptEnabled=true, browserName=chrome, rotatable=false, locationContextEnabled=false, version=24.0.1312.56, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, webStorageEnabled=true, nativeEvents=true, applicationCacheEnabled=false, takesScreenshot=true}]";
    String expectedException = "NoSuchElementException: The element could not be found";
    assertEquals(expectedException, cleanupWebDriverExceptionMessage(webdriverException));
  }

  public static class CustomWebDriverProvider implements WebDriverProvider {
    @Override
    public WebDriver createDriver() {
      return driver;
    }
  }
}

@@ -87,16 +87,116 @@
 Driver;%0A
+import org.openqa.selenium.chrome.ChromeDriver;%0A%0Aimport static com.codeborne.selenide.Selenide.open;
 %0Aimport 
@@ -337,16 +337,62 @@
 essage;%0A
+import static java.lang.Thread.currentThread;%0A
 import s
@@ -471,16 +471,70 @@
 rtSame;%0A
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;%0A
 import s
@@ -650,16 +650,36 @@
 er.class
+, RETURNS_DEEP_STUBS
 );%0A%0A  @T
@@ -1086,16 +1086,407 @@
  %7D%0A  %7D%0A%0A
+  @Test%0A  public void allowsToSpecifyCustomWebDriverProgrammatically() %7B%0A    WebDriverRunner.setWebDriver(new ChromeDriver());%0A    open(currentThread().getContextClassLoader().getResource(%22page_with_selects_without_jquery.html%22));%0A    try %7B%0A      assertEquals(ChromeDriver.class, WebDriverRunner.getWebDriver().getClass());%0A    %7D finally %7B%0A      WebDriverRunner.closeWebDriver();%0A    %7D%0A  %7D%0A%0A
   @Test%0A

Test for WebDriverRunner.setWebDriver