package integration;

import com.codeborne.selenide.Configuration;
import com.codeborne.selenide.Selenide;
import com.codeborne.selenide.WebDriverRunner;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.TimeoutException;

import static com.codeborne.selenide.Condition.visible;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.Wait;
import static com.codeborne.selenide.WebDriverRunner.source;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;

public class BasicAuthTest extends IntegrationTest {

  private String browserOriginalValue;

  @Before
  public void setUp() {
    browserOriginalValue = Configuration.browser;
  }

  @After
  public void tearDown() {
    WebDriverRunner.closeWebDriver();
    Configuration.browser = browserOriginalValue;
  }

  @Test
  public void canPassBasicAuthInFirefox() {
    Configuration.browser = "firefox";
    Selenide.open("http://httpbin.org/basic-auth/user/passwd",
        "",
        "user",
        "passwd");
    $(By.tagName("pre")).waitUntil(visible, 10000);
    assertThat(source(), containsString("\"authenticated\": true,"));
  }

  @Test
  public void canPassBasicAuthInHtmlUnit() {
    Configuration.browser = "htmlunit";
    Selenide.open("http://httpbin.org/basic-auth/user/passwd",
        "",
        "user",
        "passwd");
    assertThat(source(), containsString("\"authenticated\": true,"));
  }

  @Test
  @Ignore
  public void canPassBasicAuthInPhantom() {
    Configuration.browser = "phantomjs";
    Selenide.open("http://httpbin.org/basic-auth/user/passwd",
        "",
        "user",
        "passwd");
    $(By.tagName("pre")).waitUntil(visible, 10000);
    assertThat(source(), containsString("\"authenticated\": true,"));
  }

  @Test
  @Ignore
  public void canPassBasicAuthInChrome() {
    Configuration.browser = "chrome";
    Selenide.open("http://httpbin.org/basic-auth/user/passwd",
        "",
        "user",
        "passwd");
    $(By.tagName("pre")).waitUntil(visible, 10000);
    assertThat(source(), containsString("\"authenticated\": true,"));
  }

  @Test
  @Ignore
  public void canPassBasicAuthInIe() {
    Configuration.browser = "ie";
    Selenide.open("http://httpbin.org/basic-auth/user/passwd",
        "",
        "user",
        "passwd");
    assertThat(source(), containsString("WebDriver"));
  }

  public boolean isAlertPresent() {
    try {
      Wait().until(alertIsPresent());
      return true;
    }
    catch (TimeoutException | NoAlertPresentException ex) {
      return false;
    }
  }
}

@@ -49,333 +49,95 @@
 ide.
-Configuration;%0Aimport com.codeborne.selenide.Selenide;%0Aimport com.codeborne.selenide.WebDriverRunner;%0Aimport org.junit.After;%0Aimport org.junit.Before;%0Aimport org.junit.Ignore;%0Aimport org.junit.Test;%0Aimport org.openqa.selenium.By;%0Aimport org.openqa.selenium.NoAlertPresentException;%0Aimport org.openqa.selenium.TimeoutException
+Selenide;%0Aimport org.junit.Ignore;%0Aimport org.junit.Test;%0Aimport org.openqa.selenium.By
 ;%0A%0Ai
@@ -244,60 +244,8 @@
 .$;%0A
-import static com.codeborne.selenide.Selenide.Wait;%0A
 impo
@@ -293,22 +293,17 @@
 rRunner.
-source
+*
 ;%0Aimport
@@ -417,381 +417,86 @@
 org.
-openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;%0A%0Apublic class BasicAuthTest extends IntegrationTest %7B%0A%0A  private String browserOriginalValue;%0A%0A  @Before%0A  public void setUp() %7B%0A    browserOriginalValue = Configuration.browser;%0A  %7D%0A%0A  @After%0A  public void tearDown() %7B%0A    WebDriverRunner.closeWebDriver();%0A    Configuration.browser = browserOriginalValue;%0A  %7D
+junit.Assume.assumeTrue;%0A%0Apublic class BasicAuthTest extends IntegrationTest %7B
 %0A%0A  
@@ -553,41 +553,31 @@
     
-Configuration.browser = %22f
+assumeTrue(isF
 irefox
-%22
+())
 ;%0A  
@@ -872,42 +872,32 @@
     
-Configuration.browser = %22htmlunit%22
+assumeTrue(isHtmlUnit())
 ;%0A  
@@ -1136,16 +1136,18 @@
 nPhantom
+JS
 () %7B%0A   
@@ -1151,42 +1151,30 @@
     
-Configuration.browser = %22p
+assumeTrue(isP
 hantomjs
 %22;%0A 
@@ -1169,17 +1169,19 @@
 hantomjs
-%22
+())
 ;%0A    Se
@@ -1480,40 +1480,30 @@
     
-Configuration.browser = %22c
+assumeTrue(isC
 hrome
-%22
+())
 ;%0A  
@@ -1802,36 +1802,26 @@
     
-Configuration.browser = %22ie%22
+assumeTrue(isIE())
 ;%0A  
@@ -1990,207 +1990,7 @@
 %0A  %7D
-%0A%0A  public boolean isAlertPresent() %7B%0A    try %7B%0A      Wait().until(alertIsPresent());%0A      return true;%0A    %7D%0A    catch (TimeoutException %7C NoAlertPresentException ex) %7B%0A      return false;%0A    %7D%0A  %7D
 %0A%7D%0A

rewrite test to avoid running Firefox/Chrome tests in headless job "htmlunit"