Java 将值与Selenium和JXL进行比较

Java 将值与Selenium和JXL进行比较,java,selenium,selenium-webdriver,jxl,Java,Selenium,Selenium Webdriver,Jxl,以下代码读取电子表格单元格值​​使用JXL插件,然后将这些值与​​并从组合框中选择匹配值 我的代码可以工作,但它区分大小写,值必须相同。 我想改进这段代码,以更快地搜索组合框,并选择最接近的值,而不会完全相同。目前,它在所有值中运行缓慢 String valDesejado = tipopromocao; String valorComboBox = ""; Select verificaOpt = new Select(driver.findElement(By.name("tipoDePro

以下代码读取电子表格单元格值​​使用JXL插件,然后将这些值与​​并从组合框中选择匹配值

我的代码可以工作,但它区分大小写,值必须相同。 我想改进这段代码,以更快地搜索组合框,并选择最接近的值,而不会完全相同。目前,它在所有值中运行缓慢

String valDesejado = tipopromocao;
String valorComboBox = "";
Select verificaOpt = new Select(driver.findElement(By.name("tipoDePromocaoPromocao")));

int tamanhoBox = verificaOpt.getOptions().size();
int variavelVerificadora1 = 0;
System.out.println("Tamanho: " + tamanhoBox);

for (int i = 0; i < tamanhoBox; i++)
{
    verificaOpt.selectByIndex(i);
    valorComboBox = verificaOpt.getFirstSelectedOption().getText().toString();

    if (valDesejado.equalsIgnoreCase(valorComboBox))
    {
        i = tamanhoBox;
        variavelVerificadora1 = 1;
    }

}
if (variavelVerificadora1 == 0)
{
    System.out.println("ALERTA: The Option + valDesejado + "  no comboBox \"tipoDePromocaoPromocao\" not found.");
}
String valDesejado=tipopromocao;
字符串valorComboBox=“”;
Select-verificaOpt=new-Select(driver.findElement(By.name(“tipodepromocao”));
int-tamanhoBox=verificaOpt.getOptions().size();
int variavelVerificadora1=0;
System.out.println(“Tamanho:+tamanhoBox”);
对于(int i=0;i
我在代码中添加了一些注释,解释了我在做什么,并对一些事情进行了更正

  • 不要使用
    int
    并将其设置为0/1,而是使用
    boolean
    并将其设置为true/false
  • 此循环应该更快,因为我在循环时没有选择每个选项。您可以检查每个选项的文本而不选择它…然后找到匹配项后,选择匹配项
  • 使用
    break
    退出循环,而不是将计数器设置为最大值
  • 试试这段代码

    String valDesejado = tipopromocao;
    boolean variavelVerificadora1 = false; // use boolean instead of int set to 0/1
    Select verificaOpt = new Select(driver.findElement(By.name("tipoDePromocaoPromocao")));
    System.out.println("Tamanho: " + verificaOpt.getOptions().size());
    // as this loops, the variable 'option' contains the current loops' OPTION element
    // you don't need to select the option to get its text so this loop should be much faster
    // it selects the OPTION once the correct one is found
    for (WebElement option : verificaOpt.getOptions())
    {
        if (valDesejado.equalsIgnoreCase(option.getText()))
        {
            verificaOpt.selectByVisibleText(option.getText()); // select the OPTION match
            variavelVerificadora1 = true; // set the boolean to true to indicate we found a match
            break; // exits the for loop
        }
    }
    
    if (!variavelVerificadora1) // this is the equivalent of variavelVerificadora1 == false, it's basically saying if not true
    {
        System.out.println("ALERTA: The Option" + valDesejado + " no comboBox \"tipoDePromocaoPromocao\" not found.");
    }
    

    快速评论…您缺少此处的结束语
    “警报a:选项+…
    。它应该是“警报A:选项”+…