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();
  }
}

package com.codeborne.selenide.conditions;

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

import java.util.List;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class TextTest implements WithAssertions {
  private Driver driver = mock(Driver.class);

  @Test
  void apply_for_textInput() {
    assertThat(new Text("Hello World").apply(driver, elementWithText("Hello World"))).isTrue();
    assertThat(new Text("Hello World").apply(driver, elementWithText("Hello"))).isFalse();
  }

  @Test
  void apply_matchTextPartially() {
    assertThat(new Text("Hello").apply(driver, elementWithText("Hello World"))).isTrue();
    assertThat(new Text("World").apply(driver, elementWithText("Hello World"))).isTrue();
  }

  @Test
  void apply_for_select() {
    assertThat(new Text("Hello World").apply(driver, select("Hello", "World"))).isFalse();
    assertThat(new Text("Hello World").apply(driver, select("Hello", " World"))).isTrue();
  }

  private WebElement elementWithText(String text) {
    WebElement webElement = mock(WebElement.class);
    when(webElement.getText()).thenReturn(text);
    return webElement;
  }

  private WebElement select(String... optionTexts) {
    WebElement select = elementWithText("Hello World");
    when(select.getTagName()).thenReturn("select");

    List<WebElement> options = Stream.of(optionTexts)
      .map(this::elementWithText)
      .peek(option -> when(option.isSelected()).thenReturn(true))
      .collect(toList());

    when(select.findElements(By.tagName("option"))).thenReturn(options);
    return select;
  }
}

make TextTest human-readable