package integration;

import com.codeborne.selenide.Configuration;
import com.codeborne.selenide.ex.ElementNotFound;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;

import static com.codeborne.selenide.Selectors.byName;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.getSelectedRadio;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

final class RadioTest extends IntegrationTest {
  @BeforeEach
  void openTestPage() {
    openFile("page_with_selects_without_jquery.html");
    assertThat(getSelectedRadio(By.name("me")))
      .isNull();
  }

  @Test
  void userCanSelectRadioButtonByValue() {
    $(By.name("me")).selectRadio("cat");
    assertThat(getSelectedRadio(By.name("me")).val())
      .isEqualTo("cat");
  }

  @Test
  void userCanSelectRadioButtonByValueOldWay() {
    $(By.name("me")).selectRadio("cat");
    assertThat(getSelectedRadio(By.name("me")).getAttribute("value"))
      .isEqualTo("cat");
  }

  @Test
  void userCanSelectRadioButtonUsingSetValue() {
    Configuration.versatileSetValue = true;
    $(byName("me")).setValue("margarita");
    assertThat(getSelectedRadio(By.name("me")).val())
      .isEqualTo("margarita");
  }

  @Test
  void selenideElement_selectRadio() {
    $(By.name("me")).selectRadio("margarita");
    assertThat(getSelectedRadio(By.name("me")).val())
      .isEqualTo("margarita");
  }

  @Test
  void selenideElement_selectRadio_elementNotFound() {
    assertThatThrownBy(() -> $(By.id("unknownId")).selectRadio("margarita"))
      .isInstanceOf(ElementNotFound.class)
      .hasMessageStartingWith(String.format("Element not found {By.id: unknownId}%nExpected: value 'margarita'"));
  }

  @Test
  void selenideElement_selectRadio_valueNotFound() {
    assertThatThrownBy(() -> $(By.name("me")).selectRadio("unknown-value"))
      .isInstanceOf(ElementNotFound.class)
      .hasMessageStartingWith(String.format("Element not found {By.name: me}%nExpected: value 'unknown-value'"));
  }
}

@@ -218,16 +218,189 @@
 um.By;%0A%0A
+import static com.codeborne.selenide.Condition.attribute;%0Aimport static com.codeborne.selenide.Condition.checked;%0Aimport static com.codeborne.selenide.Condition.exactValue;%0A
 import s
@@ -446,16 +446,16 @@
 byName;%0A
-
 import s
@@ -987,35 +987,24 @@
 %22cat%22);%0A    
-assertThat(
 getSelectedR
@@ -1027,38 +1027,37 @@
 %22)).
-val())%0A      .isEqualTo
+shouldHave(exactValue
 (%22cat%22)
+)
 ;%0A  
@@ -1401,35 +1401,24 @@
 rita%22);%0A    
-assertThat(
 getSelectedR
@@ -1437,39 +1437,37 @@
 (%22me%22)).
-val())%0A      .isEqualTo
+shouldHave(exactValue
 (%22margar
@@ -1463,32 +1463,33 @@
 lue(%22margarita%22)
+)
 ;%0A  %7D%0A%0A  @Test%0A 
@@ -1573,35 +1573,24 @@
 rita%22);%0A    
-assertThat(
 getSelectedR
@@ -1613,45 +1613,347 @@
 %22)).
-val(
+shouldHave(exactValue(%22margarita%22
 ))
+;
 %0A  
-    .isEqualTo(%22margarita%22);
+%7D%0A%0A  @Test%0A  void assertCheckedRadioButton() %7B%0A    $(%22%5Bname=me%5D%5Bvalue=margarita%5D%22).shouldNotBe(checked);%0A%0A    $(byName(%22me%22)).selectRadio(%22margarita%22);%0A%0A    $(%22%5Bname=me%5D%5Bvalue=margarita%5D%22).shouldBe(checked);%0A    $(%22%5Bname=me%5D%5Bvalue=margarita%5D%22).shouldHave(attribute(%22checked%22)); // just an alternative
 %0A  %7D

improve examples of checking radio buttons