Thiago Andrade
Thiago Andrade

Reputation: 61

How I check selected value from combobox

I need to confirm if selected value from combobox is correct and I have this code, but it just print the selected value. I want to compare with a text typed and if the selected value is the correct, the test should pass

I'm using Selenium with Java.

Select select = new Select(driver.findElement(By.id ("koopon-produto-select-produto- 
especifico-calculos-icms")));
WebElement option = select.getFirstSelectedOption();
String defaultItem = option.getText();
System.out.println(defaultItem);

Upvotes: 1

Views: 334

Answers (2)

Lia
Lia

Reputation: 526

import org.junit.Test;
import org.junit.Ignore;
import static org.junit.Assert.assertEquals;

public class TestJunit1 {
       
   @Test
   public void testSelectedCombobox() { 
   Select select  = new Select(driver.findElement(By.id("koopon-produto- 
   select-produto-especifico-calculos-icms")));
   String text = select.getFirstSelectedOption().getText();
    
   assertEquals("expectedText", text);     
   }
}

Upvotes: 2

YaDav MaNish
YaDav MaNish

Reputation: 1352

Can you check the below code, if it help 

Select select  = new Select(driver.findElement(By.id("koopon-produto- 
select-produto-especifico-calculos-icms")));
String text = select.getFirstSelectedOption().getText();
    
    String result = (text.equals(("test")))?"Pass":"Fail";
    System.out.println(result);

Upvotes: 1

Related Questions