package ui;

import com.codeborne.selenide.ElementsCollection;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;

import static com.codeborne.selenide.Condition.*;
import static com.codeborne.selenide.Selenide.*;
import static org.junit.Assert.assertTrue;

public class RedPenDemoTest {
  @Before
  public void setUp() throws Exception {
    open("http://localhost:8080/");
  }

  @Test
  public void welcomeIsDisplayed() throws Exception {
    $("h1").shouldHave(text("Welcome to RedPen!"));
  }

  @Test
  public void redpenEditorIsPrepopulated() throws Exception {
    String value = $("#redpen-editor").shouldBe(visible).val();
    assertTrue(value.startsWith("Some software tools work"));
  }

  @Test
  public void userCanChooseSampleTexts() throws Exception {
    $("#themes").click();
    $(By.linkText("JAPANESE TEXT")).click();

    String value = $("#redpen-editor").shouldBe(visible).val();
    assertTrue(value.startsWith("最近利用"));
  }

  @Test
  public void userCanClearTheText() throws Exception {
    $("[title='Clear text']").click();
    $("#redpen-editor").shouldBe(empty);
  }

  @Test
  public void textIsValidatedAsItEntered() throws Exception {
    $("#redpen-editor").val("Hello Wodrl");
    $("#redpen-errors").shouldHave(text("RedPen found 1 error"));

    ElementsCollection errors = $$(".redpen-error-list .redpen-error-message").shouldHaveSize(1);
    errors.get(0).shouldHave(text("Found possibly misspelled word \"Wodrl\"."));
    errors.get(0).find(".redpen-error-validator").shouldHave(text("Spelling"));
  }
}

@@ -56,16 +56,63 @@
 ection;%0A
+import com.codeborne.selenide.SelenideElement;%0A
 import o
@@ -1610,11 +1610,1172 @@
 %22));%0A  %7D
+%0A%0A  @Test%0A  public void validatorsCanBeDisabled() throws Exception %7B%0A    $(%22input%5Btype=checkbox%5D%5Bvalue=Spelling%5D%22).click();%0A    $(%22#redpen-editor%22).val(%22Hello Wodrl%22);%0A    $(%22#redpen-errors%22).shouldHave(text(%22RedPen found 0 errors%22));%0A  %7D%0A%0A  @Test%0A  public void validatorPropertiesCanBeChanged() throws Exception %7B%0A    SelenideElement validatorProperties = $(%22.redpen-validator-properties%5Bname=SentenceLength%5D%22);%0A    validatorProperties.click();%0A%0A    $(%22.popover-title%22).should(appear).shouldHave(text(%22SentenceLength properties%22));%0A    $(%22.popover-content input%5Btype=text%5D%22).shouldHave(value(%22max_len=200%22)).val(%22max_len=10%22);%0A    $(%22.popover-content button%5Btype=submit%5D%22).click();%0A%0A    validatorProperties.shouldHave(text(%22max_len=10%22));%0A%0A    $(%22#redpen-editor%22).val(%22This is a very long sentence of over ten words.%22);%0A    $(%22#redpen-errors%22).shouldHave(text(%22RedPen found 2 errors%22));%0A%0A    $$(%22.redpen-error-message%22).get(0).shouldHave(text(%22The length of the sentence (47) exceeds the maximum of 10.%22), text(%22SentenceLength%22));%0A    $$(%22.redpen-error-message%22).get(1).shouldHave(text(%22%5C%22very%5C%22 is considered a weak expression.%22), text(%22WeakExpression%22));%0A  %7D
 %0A%7D%0A

add more validation UI tests