package com.codeborne.selenide;

import com.codeborne.selenide.Selectors.ByText;
import com.codeborne.selenide.Selectors.WithText;
import org.junit.Test;
import org.openqa.selenium.By;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class SelectorsTest {
  @Test
  public void withTextUsesXPath() {
    By selector = Selectors.withText("john");
    assertTrue(selector instanceof By.ByXPath);
    assertEquals("with text: john", selector.toString());
    assertEquals(".//*/text()[contains(normalize-space(.), \"john\")]/parent::*", ((WithText) selector).getXPath());
  }

  @Test
  public void withTextEscapesQuotes() {
    By selector = Selectors.withText("Ludvig'van\"Beethoven");
    assertEquals("with text: Ludvig'van\"Beethoven", selector.toString());
    assertTrue(selector instanceof By.ByXPath);
    assertEquals(".//*/text()[contains(normalize-space(.), concat(\"Ludvig'van\", '\"', \"Beethoven\"))]/parent::*", 
        ((WithText) selector).getXPath());
  }

  @Test
  public void byTextUsesXPath() {
    By selector = Selectors.byText("john");
    assertEquals("by text: john", selector.toString());
    assertTrue(selector instanceof By.ByXPath);
    assertEquals(".//*/text()[normalize-space(.) = \"john\"]/parent::*", ((ByText) selector).getXPath());
  }

  @Test
  public void byTextEscapesQuotes() {
    By selector = Selectors.byText("Ludvig'van\"Beethoven");
    assertEquals("by text: Ludvig'van\"Beethoven", selector.toString());
    assertTrue(selector instanceof By.ByXPath);
    assertEquals(".//*/text()[normalize-space(.) = concat(\"Ludvig'van\", '\"', \"Beethoven\")]/parent::*", 
        ((ByText) selector).getXPath());
  }

  @Test
  public void byAttributeUsesXPath() {
    By selector = Selectors.byAttribute("value", "катя");
    assertEquals("By.xpath: .//*[@value=\"катя\"]", selector.toString());
  }

  @Test
  public void byAttributeEscapesQuotes() {
    By selector = Selectors.byAttribute("value", "Ludvig'van\"Beethoven");
    assertEquals("By.xpath: .//*[@value=concat(\"Ludvig'van\", '\"', \"Beethoven\")]", selector.toString());
  }

  @Test
  public void userCanFindElementByAnyAttribute() {
    By selector = Selectors.by("data-account-id", "666");
    assertEquals("By.xpath: .//*[@data-account-id=\"666\"]", selector.toString());
  }

  @Test
  public void byTitleUsesXPath() {
    By selector = Selectors.byTitle("PDF report");
    assertEquals("By.xpath: .//*[@title=\"PDF report\"]", selector.toString());
  }

  @Test
  public void byValueUsesXPath() {
    By selector = Selectors.byValue("водокачка");
    assertEquals("By.xpath: .//*[@value=\"водокачка\"]", selector.toString());
  }
}

@@ -1829,35 +1829,34 @@
 %22By.
-xpath: .//*%5B@
+cssSelector: %5B
 value=
-%5C%22
+'
 %D0%BA%D0%B0%D1%82%D1%8F
-%5C%22
+'
 %5D%22, 
@@ -2033,36 +2033,29 @@
 %22By.
-xpath: .//*%5B@value=concat(%5C%22
+cssSelector: %5Bvalue='
 Ludv
@@ -2056,34 +2056,24 @@
 ='Ludvig'van
-%5C%22, '%5C%22', 
 %5C%22Beethoven%5C
@@ -2071,19 +2071,17 @@
 eethoven
-%5C%22)
+'
 %5D%22, sele
@@ -2241,29 +2241,30 @@
 als(%22By.
-xpath: .//*%5B@
+cssSelector: %5B
 data-acc
@@ -2275,15 +2275,13 @@
 -id=
-%5C%22666%5C%22
+'666'
 %5D%22, 
@@ -2426,29 +2426,29 @@
 %22By.
-xpath: .//*%5B@
+cssSelector: %5B
 title=
-%5C%22
+'
 PDF 
@@ -2453,18 +2453,17 @@
 F report
-%5C%22
+'
 %5D%22, sele
@@ -2603,29 +2603,29 @@
 %22By.
-xpath: .//*%5B@
+cssSelector: %5B
 value=
-%5C%22
+'
 %D0%B2%D0%BE%D0%B4%D0%BE
@@ -2629,18 +2629,17 @@
 %D0%BE%D0%B4%D0%BE%D0%BA%D0%B0%D1%87%D0%BA%D0%B0
-%5C%22
+'
 %5D%22, sele

Fix unit tests