package integration;

import com.codeborne.selenide.ex.ElementNotFound;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import javax.annotation.Nonnull;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static com.codeborne.selenide.Condition.visible;
import static java.time.Duration.ofMillis;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

final class ReportForChainedElementsTest extends ITest {
  private List<File> previousScreenshots;

  @BeforeEach
  void openTestPage() {
    setTimeout(1);
    openFile("page_with_absence.html");
    previousScreenshots = screenshots();
  }

  @Test
  void shouldShowWhichElementInChainWasNotFound() {
    assertThatThrownBy(() -> {
      $("body")
        .$(".h1")
        .$(".h2")
        .$(".h3")
        .$(".h4")
        .$(".h5")
        .$(".h6")
        .shouldBe(visible, ofMillis(400));
    })
      .isInstanceOf(ElementNotFound.class)
      .hasMessageStartingWith("Element not found {body/.h1/.h2/.h3/.h4}");

    assertTookScreenshots();
  }

  @Test
  void shouldShowAliasInElementNotFoundError() {
    assertThatThrownBy(() -> {
      $("body").as("-the-body-")
        .$(".h1").as("-the-h1-")
        .$(".h2").as("-the-h2-")
        .$(".h3").as("-the-h3-")
        .$(".h4").as("-the-h4-")
        .$(".h5").as("-the-h5-")
        .$(".h6").as("-the-h6-")
        .shouldBe(visible, ofMillis(400));
    })
      .isInstanceOf(ElementNotFound.class)
      .hasMessageStartingWith("Element not found {-the-h4-}");

    assertTookScreenshots();
  }

  private void assertTookScreenshots() {
    List<File> screenshots = new ArrayList<>(screenshots());
    screenshots.removeAll(previousScreenshots);
    assertThat(screenshots).as("*.png + *.html").hasSize(2);
  }

  private List<File> screenshots() {
    File dir = screenshotsFolder();
    File[] files = dir.listFiles();
    return files == null ? emptyList() : asList(files);
  }

  @Nonnull
  private File screenshotsFolder() {
    return new File(driver().config().reportsFolder());
  }
}

@@ -15,16 +15,108 @@
 ation;%0A%0A
+import com.codeborne.selenide.SelenideConfig;%0Aimport com.codeborne.selenide.SelenideDriver;%0A
 import c
@@ -699,17 +699,31 @@
 extends 
-I
+BaseIntegration
 Test %7B%0A 
@@ -767,55 +767,348 @@
 ts;%0A
-%0A  @BeforeEach%0A  void openTestPage() %7B%0A    setT
+  private static final String reportsFolder = %22build/reports/tests/%22 + System.currentTimeMillis();%0A  private SelenideDriver driver;%0A%0A  @BeforeEach%0A  void openTestPage() %7B%0A    driver = new SelenideDriver(new SelenideConfig()%0A      .browser(browser).headless(headless)%0A      .baseUrl(getBaseUrl())%0A      .reportsFolder(reportsFolder)%0A      .t
 imeo
@@ -1116,24 +1116,35 @@
 t(1)
+%0A    )
 ;%0A
+%0A
     
+driver.
 open
-File
 (%22
+/
 page
@@ -1161,17 +1161,36 @@
 nce.html
-%22
+?browser=%22 + browser
 );%0A    p
@@ -1319,32 +1319,39 @@
 y(() -%3E %7B%0A      
+driver.
 $(%22body%22)%0A      
@@ -1749,16 +1749,23 @@
 %7B%0A      
+driver.
 $(%22body%22
@@ -2632,18 +2632,16 @@
 e(driver
-()
 .config(

fix flaky test ReportForChainedElementsTest