package integration;

import com.codeborne.selenide.Selenide;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;

import static com.codeborne.selenide.Condition.visible;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.WebDriverRunner.isChrome;
import static com.codeborne.selenide.WebDriverRunner.isFirefox;
import static com.codeborne.selenide.WebDriverRunner.isHtmlUnit;
import static com.codeborne.selenide.WebDriverRunner.isIE;
import static com.codeborne.selenide.WebDriverRunner.isPhantomjs;
import static com.codeborne.selenide.WebDriverRunner.source;

class BasicAuthTest extends IntegrationTest {
  @Test
  void canPassBasicAuthInFirefox() {
    Assumptions.assumeTrue(isFirefox());
    Selenide.open("http://httpbin.org/basic-auth/user/passwd",
      "",
      "user",
      "passwd");
    $(By.tagName("pre")).waitUntil(visible, 10000);
    assertThat(source())
      .contains("\"authenticated\": true,");
  }

  @Test
  void canPassBasicAuthInHtmlUnit() {
    Assumptions.assumeTrue(isHtmlUnit());
    Selenide.open("http://httpbin.org/basic-auth/user/passwd",
      "",
      "user",
      "passwd");
    assertThat(source())
      .contains("\"authenticated\":true,");
  }

  @Test
  @Disabled
  void canPassBasicAuthInPhantomJS() {
    Assumptions.assumeTrue(isPhantomjs());
    Selenide.open("http://httpbin.org/basic-auth/user/passwd",
      "",
      "user",
      "passwd");
    $(By.tagName("pre")).waitUntil(visible, 10000);
    assertThat(source())
      .contains("\"authenticated\": true,");
  }

  @Test
  @Disabled
  void canPassBasicAuthInChrome() {
    Assumptions.assumeTrue(isChrome());
    Selenide.open("http://httpbin.org/basic-auth/user/passwd",
      "",
      "user",
      "passwd");
    $(By.tagName("pre")).waitUntil(visible, 10000);
    assertThat(source())
      .contains("\"authenticated\": true,");
  }

  @Test
  @Disabled
  void canPassBasicAuthInIe() {
    Assumptions.assumeTrue(isIE());
    Selenide.open("http://httpbin.org/basic-auth/user/passwd",
      "",
      "user",
      "passwd");
    assertThat(source())
      .contains("WebDriver");
  }
}

@@ -203,16 +203,49 @@
 um.By;%0A%0A
+import java.util.regex.Pattern;%0A%0A
 import s
@@ -292,16 +292,16 @@
 isible;%0A
-
 import s
@@ -718,16 +718,122 @@
 .source;
+%0Aimport static java.util.regex.Pattern.DOTALL;%0Aimport static org.junit.jupiter.api.Assumptions.assumeTrue;
 %0A%0Aclass 
@@ -917,36 +917,24 @@
 fox() %7B%0A    
-Assumptions.
 assumeTrue(i
@@ -1120,61 +1120,27 @@
 That
-(source())%0A      .contains(%22%5C%22authenticated%5C%22: true,%22
+BasicAuthSucceeded(
 );%0A 
@@ -1189,36 +1189,24 @@
 nit() %7B%0A    
-Assumptions.
 assumeTrue(i
@@ -1341,60 +1341,27 @@
 That
-(source())%0A      .contains(%22%5C%22authenticated%5C%22:true,%22
+BasicAuthSucceeded(
 );%0A 
@@ -1423,36 +1423,24 @@
 mJS() %7B%0A    
-Assumptions.
 assumeTrue(i
@@ -1628,61 +1628,27 @@
 That
-(source())%0A      .contains(%22%5C%22authenticated%5C%22: true,%22
+BasicAuthSucceeded(
 );%0A 
@@ -1707,36 +1707,24 @@
 ome() %7B%0A    
-Assumptions.
 assumeTrue(i
@@ -1909,61 +1909,27 @@
 That
-(source())%0A      .contains(%22%5C%22authenticated%5C%22: true,%22
+BasicAuthSucceeded(
 );%0A 
@@ -1988,28 +1988,16 @@
 ) %7B%0A    
-Assumptions.
 assumeTr
@@ -2137,16 +2137,16 @@
 urce())%0A
-
       .c
@@ -2170,11 +2170,187 @@
 r%22);%0A  %7D
+%0A%0A  private void assertThatBasicAuthSucceeded() %7B%0A    Pattern regex = Pattern.compile(%22%5C%5C%7B.*%5C%22authenticated%5C%22:%5C%5Cs*true.*%22, DOTALL);%0A    assertThat(source()).matches(regex);%0A  %7D
 %0A%7D%0A

make test independent on json formatting