package integration;

import com.codeborne.selenide.WebDriverRunner;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

import static com.codeborne.selenide.Condition.visible;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.using;
import static com.codeborne.selenide.WebDriverRunner.closeWebDriver;
import static com.codeborne.selenide.WebDriverRunner.isChrome;
import static com.codeborne.selenide.WebDriverRunner.isFirefox;
import static com.codeborne.selenide.WebDriverRunner.setWebDriver;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat;

class CustomWebdriverTest extends IntegrationTest {
  private WebDriver browser1;
  private WebDriver browser2;

  @BeforeAll
  static void setUpWebdrivers() {
    assumeThat(isChrome() || isFirefox()).isTrue();

    closeWebDriver();
    if (isFirefox()) WebDriverManager.firefoxdriver().setup();
    if (isChrome()) WebDriverManager.chromedriver().setup();
  }

  @BeforeEach
  void setUpTwoBrowsers() {
    useProxy(false);

    browser1 = isFirefox() ?
      new FirefoxDriver(addSslErrorIgnoreCapabilities(addHeadless(new FirefoxOptions()))) :
      new ChromeDriver(addSslErrorIgnoreCapabilities(addHeadless(new ChromeOptions())));
    browser2 = isFirefox() ?
      new FirefoxDriver(addSslErrorIgnoreCapabilities(addHeadless(new FirefoxOptions()))) :
      new ChromeDriver(addSslErrorIgnoreCapabilities(addHeadless(new ChromeOptions())));
  }

  @Test
  void userCanSwitchBetweenWebdrivers_using_setWebDriver() {
    setWebDriver(browser1);
    openFile("page_with_selects_without_jquery.html");
    $("h1").shouldBe(visible);

    setWebDriver(browser2);
    openFile("page_with_selects_without_jquery.html");
    $("h2").shouldBe(visible);

    setWebDriver(browser1);
    openFile("page_with_selects_without_jquery.html");
    $("h1").shouldBe(visible);
  }

  @Test
  void userCanSwitchBetweenWebdrivers_usingIn() {
    using(browser1, () -> {
      openFile("page_with_selects_without_jquery.html");
      $("h1").shouldBe(visible);
      assertThat(WebDriverRunner.getWebDriver()).isSameAs(browser1);
    });

    assertThat(WebDriverRunner.hasWebDriverStarted()).isFalse();

    using(browser2, () -> {
      openFile("page_with_selects_without_jquery.html");
      $("h2").shouldBe(visible);
      assertThat(WebDriverRunner.getWebDriver()).isSameAs(browser2);
    });

    assertThat(WebDriverRunner.hasWebDriverStarted()).isFalse();

    using(browser1, () -> {
      openFile("page_with_selects_without_jquery.html");
      $("h1").shouldBe(visible);
      assertThat(WebDriverRunner.getWebDriver()).isSameAs(browser1);
    });

    assertThat(WebDriverRunner.hasWebDriverStarted()).isFalse();
  }

  @AfterEach
  void tearDown() {
    if (browser1 != null) browser1.quit();
    if (browser2 != null) browser2.quit();
  }
}

@@ -505,16 +505,69 @@
 tions;%0A%0A
+import static com.codeborne.selenide.Condition.text;%0A
 import s
@@ -2534,34 +2534,72 @@
 houldBe(visible)
+.shouldHave(text(%22Page with selects%22))
 ;%0A
-
       assertThat
@@ -2834,32 +2834,66 @@
 houldBe(visible)
+.shouldHave(text(%22Dropdown list%22))
 ;%0A      assertTh
@@ -3060,58 +3060,244 @@
     
-openFile(%22page_
+$(%22h1%22).shouldBe(visible).shouldHave(text(%22Page 
 with
-_
+ 
 selects
-_without_jqu
+%22));%0A      assertThat(WebDriverRunner.getWebDriver()).isSameAs(browser1);%0A    %7D);%0A%0A    assertThat(WebDriverRunn
 er
-y
 .h
-tml%22);
+asWebDriverStarted()).isFalse();%0A%0A    using(browser2, () -%3E %7B
 %0A   
@@ -3295,33 +3295,33 @@
  -%3E %7B%0A      $(%22h
-1
+2
 %22).shouldBe(visi
@@ -3320,24 +3320,58 @@
 dBe(visible)
+.shouldHave(text(%22Dropdown list%22))
 ;%0A      asse
@@ -3417,33 +3417,33 @@
 isSameAs(browser
-1
+2
 );%0A    %7D);%0A%0A    

improve test for WebDriverRunner.using(): don't open browser again, but reuse the previous browser