package com.codeborne.selenide.conditions;

import com.codeborne.selenide.Driver;
import org.assertj.core.api.WithAssertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import static java.util.Arrays.asList;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class TextTest implements WithAssertions {
  private final String defaultText = "Hello World";
  private Text text;
  private Driver driver = mock(Driver.class);
  private WebElement mWebElement = mock(WebElement.class);

  @BeforeEach
  void setup() {
    text = new Text(defaultText);
  }

  @Test
  void testApplyForNonSelectText() {
    when(mWebElement.getText()).thenReturn(defaultText);
    assertThat(text.apply(driver, mWebElement)).isTrue();

    when(mWebElement.getText()).thenReturn("Hello");
    assertThat(text.apply(driver, mWebElement)).isFalse();
  }

  @Test
  void testApplyForSelectTagName() {
    WebElement element1 = mock(WebElement.class);
    String element1Text = "Hello";
    WebElement element2 = mock(WebElement.class);
    String element2Text = "World";

    when(mWebElement.findElements(By.tagName("option"))).thenReturn(asList(element1, element2));

    when(element1.isSelected()).thenReturn(true);
    when(element1.getText()).thenReturn(element1Text);

    when(element2.isSelected()).thenReturn(true);
    when(element2.getText()).thenReturn(element2Text);

    when(mWebElement.getTagName()).thenReturn("select");
    assertThat(text.apply(driver, mWebElement)).isFalse();

    when(element2.getText()).thenReturn(" " + element2Text);
    assertThat(text.apply(driver, mWebElement)).isTrue();
  }
}

@@ -123,49 +123,8 @@
 ns;%0A
-import org.junit.jupiter.api.BeforeEach;%0A
 impo
@@ -236,34 +236,101 @@
 ort 
-static java.util.Arrays.as
+java.util.List;%0Aimport java.util.stream.Stream;%0A%0Aimport static java.util.stream.Collectors.to
 List
@@ -459,81 +459,8 @@
 s %7B%0A
-  private final String defaultText = %22Hello World%22;%0A  private Text text;%0A
   pr
@@ -505,237 +505,371 @@
 s);%0A
+%0A
   
-private WebElement mWebElement = mock(WebElement.class);%0A%0A  @BeforeEach%0A  void setup() %7B%0A    text = new Text(defaultText);%0A  %7D%0A%0A  @Test%0A  void testApplyForNonSelectText() %7B%0A    when(mWebE
+@Test%0A  void apply_for_textInput() %7B%0A    assertThat(new Text(%22Hello World%22).apply(driver, elementWithText(%22Hello World%22))).isTrue();%0A    assertThat(new Text(%22Hello World%22).apply(driver, elementWithText(%22Hello%22))).isFalse();%0A  %7D%0A%0A  @Test%0A  void apply_matchTextPartially() %7B%0A    assertThat(new Text(%22Hello%22).apply(driver, e
 lement
-.get
+With
 Text(
-)).thenReturn(defaultText
+%22Hello World%22))).isTrue(
 );%0A 
@@ -874,36 +874,49 @@
 %0A    assertThat(
-text
+new Text(%22World%22)
 .apply(driver, m
@@ -910,35 +910,54 @@
 ply(driver, 
-mWebE
+e
 lement
+WithText(%22Hello World%22)
 )).isTrue();
@@ -961,109 +961,128 @@
 ();%0A
-%0A
   
-  when(mWebElement.getText()).thenReturn(%22Hello%22);%0A    assertThat(text.apply(driver, mWebElement
+%7D%0A%0A  @Test%0A  void apply_for_select() %7B%0A    assertThat(new Text(%22Hello World%22).apply(driver, select(%22Hello%22, %22World%22
 ))
+)
 .isF
@@ -1095,140 +1095,153 @@
 ;%0A  
-%7D%0A%0A  @Test%0A  void testApplyForSelectTagName() %7B%0A    WebElement element1 = mock(WebElement.class);%0A    String element1Text = %22Hello%22;
+  assertThat(new Text(%22Hello World%22).apply(driver, select(%22Hello%22, %22 World%22))).isTrue();%0A  %7D%0A%0A  private WebElement elementWithText(String text) %7B
 %0A   
@@ -1252,24 +1252,26 @@
 Element 
-e
+webE
 lement
-2
  = mock(
@@ -1293,44 +1293,8 @@
 s);%0A
-    String element2Text = %22World%22;%0A%0A
     
@@ -1290,34 +1290,33 @@
 lass);%0A    when(
-mW
+w
 ebElement.findEl
@@ -1313,191 +1313,174 @@
 ent.
-findElements(By.tagName(%22option%22))).thenReturn(asList(element1, e
+getText()).thenReturn(text);%0A    return webE
 lement
-2))
 ;%0A
-%0A
   
-  when(element1.isSelected()).thenReturn(true);%0A    when(element1.getText()).thenReturn(element1Text
+%7D%0A%0A  private WebElement select(String... optionTexts) %7B%0A    WebElement select = elementWithText(%22Hello World%22
 );%0A
-%0A
     
@@ -1488,27 +1488,25 @@
 hen(
+s
 ele
-ment2.isSelected
+ct.getTagName
 ()).
@@ -1520,184 +1520,197 @@
 urn(
-true
+%22select%22
 );%0A
+%0A
     
-when(element2.getText()).thenReturn(
+List%3CWebElement%3E options = Stream.of(optionTexts)%0A      .map(this::
 element
-2
+With
 Text)
-;%0A%0A    when(mWebElement.getTagName()).thenReturn(%22select%22);%0A    assertThat(text.apply(driver, mWebElement)).isFalse(
+%0A      .peek(option -%3E when(option.isSelected()).thenReturn(true))%0A      .collect(toList()
 );%0A%0A
@@ -1722,116 +1722,89 @@
 hen(
+s
 ele
-ment2.getText()).thenReturn(%22 %22 + element2Text);%0A    assertThat(text.apply(driver, mWebElement)).isTrue()
+ct.findElements(By.tagName(%22option%22))).thenReturn(options);%0A    return select
 ;%0A  

make TextTest human-readable