首页 > 解决方案 > 我如何从组合框中检查选定的值

问题描述

我需要确认从组合框中选择的值是否正确并且我有这个代码,但它只是打印选择的值。我想与输入的文本进行比较,如果选择的值正确,则测试应该通过

我在 Java 中使用 Selenium。

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

标签: javaseleniumselenium-webdriver

解决方案


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

推荐阅读