package integration;

import static com.codeborne.selenide.Condition.name;

import com.codeborne.selenide.Configuration;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;

import static com.codeborne.selenide.Condition.text;
import static com.codeborne.selenide.Selenide.*;
import static com.codeborne.selenide.WebDriverRunner.currentFrameUrl;
import static com.codeborne.selenide.WebDriverRunner.isChrome;
import static com.codeborne.selenide.WebDriverRunner.source;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeFalse;

public class FramesTest extends IntegrationTest {
  @Before
  public void openPage() {
    openFile("page_with_frames.html");
  }

  @Test
  public void canSwitchIntoInnerFrame() {
    assertEquals("Test::frames", title());

    switchTo().innerFrame("parentFrame");
    $("frame").shouldHave(name("childFrame_1"));
    assertThat(currentFrameUrl(), equalTo(Configuration.baseUrl + "/page_with_parent_frame.html"));

    switchTo().innerFrame("parentFrame", "childFrame_1");
    assertTrue(source().contains("Hello, WinRar!"));
    assertThat(currentFrameUrl(), equalTo(Configuration.baseUrl + "/hello_world.txt"));

    switchTo().innerFrame("parentFrame", "childFrame_2");
    $("frame").shouldHave(name("childFrame_2_1"));
    assertThat(currentFrameUrl(), equalTo(Configuration.baseUrl + "/page_with_child_frame.html"));

    switchTo().innerFrame("parentFrame", "childFrame_2", "childFrame_2_1");
    assertTrue(source().contains("This is last frame!"));
    assertThat(currentFrameUrl(), equalTo(Configuration.baseUrl + "/child_frame.txt"));

    switchTo().innerFrame("parentFrame");
    $("frame").shouldHave(name("childFrame_1"));
    assertThat(currentFrameUrl(), equalTo(Configuration.baseUrl + "/page_with_parent_frame.html"));
  }

  @Test
  public void switchToInnerFrame_withoutParameters_switchesToDefaultContent() {
    switchTo().innerFrame("parentFrame");
    $("frame").shouldHave(name("childFrame_1"));

    switchTo().innerFrame();
    $("frame").shouldHave(name("topFrame"));
  }

  @Test
  public void canSwitchBetweenFramesByTitle() {
    assertEquals("Test::frames", title());

    switchTo().frame("topFrame");
    assertTrue(source().contains("Hello, WinRar!"));

    switchTo().defaultContent();
    switchTo().frame("leftFrame");
    $("h1").shouldHave(text("Page with dynamic select"));

    switchTo().defaultContent();
    switchTo().frame("mainFrame");
    $("h1").shouldHave(text("Page with JQuery"));
  }

  @Test
  public void canSwitchBetweenFramesByIndex() {
    assumeFalse(isChrome());
    assertEquals("Test::frames", title());

    switchTo().frame(0);
    assertTrue(source().contains("Hello, WinRar!"));

    switchTo().defaultContent();
    switchTo().frame(1);
    $("h1").shouldHave(text("Page with dynamic select"));

    switchTo().defaultContent();
    switchTo().frame(2);
    $("h1").shouldHave(text("Page with JQuery"));
  }

  @AfterClass
  public static void tearDown() {
    close();
  }
}

@@ -19,62 +19,8 @@
 n;%0A%0A
-import static com.codeborne.selenide.Condition.name;%0A%0A
 impo
@@ -138,16 +138,69 @@
 .Test;%0A%0A
+import static com.codeborne.selenide.Condition.name;%0A
 import s
@@ -346,31 +346,17 @@
 rRunner.
-currentFrameUrl
+*
 ;%0Aimport
@@ -367,296 +367,131 @@
 tic 
-com.codeborne.selenide.WebDriverRunner.isChrome;%0Aimport static com.codeborne.selenide.WebDriverRunner.source;%0Aimport static org.hamcrest.CoreMatchers.equalTo;%0Aimport static org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.assertThat;%0Aimport static org.junit.Assert.assertTrue
+org.hamcrest.CoreMatchers.containsString;%0Aimport static org.hamcrest.CoreMatchers.equalTo;%0Aimport static org.junit.Assert.*
 ;%0Aim
@@ -1011,35 +1011,35 @@
 1%22);%0A    assertT
-rue
+hat
 (source().contai
@@ -1027,33 +1027,34 @@
 hat(source()
-.
+, 
 contains
 (%22Hello, Win
@@ -1033,32 +1033,38 @@
 urce(), contains
+String
 (%22Hello, WinRar!
@@ -1445,35 +1445,35 @@
 1%22);%0A    assertT
-rue
+hat
 (source().contai
@@ -1465,25 +1465,26 @@
 source()
-.
+, 
 contains
 (%22This i
@@ -1475,16 +1475,22 @@
 contains
+String
 (%22This i
@@ -2188,35 +2188,35 @@
 e%22);%0A    assertT
-rue
+hat
 (source().contai
@@ -2204,33 +2204,34 @@
 hat(source()
-.
+, 
 contains
 (%22Hello, Win
@@ -2210,32 +2210,38 @@
 urce(), contains
+String
 (%22Hello, WinRar!
@@ -2665,11 +2665,11 @@
 ertT
-rue
+hat
 (sou
@@ -2673,25 +2673,26 @@
 source()
-.
+, 
 contains
 (%22Hello,
@@ -2683,16 +2683,22 @@
 contains
+String
 (%22Hello,

use assertThat instead of assertThat (it gives more detailed error message)