首页 > 解决方案 > 如何使用数据驱动方法从下拉列表中选择 2 个或更多值 - 现在可以使用

问题描述

这是我的代码:

Excel 实用程序:

public static Object [][] getTestData(String sheetName) 
{
    try 
    {
        FileInputStream ip = new FileInputStream(TESTDATA_SHEET_PATH);

        try 
        {
            book = WorkbookFactory.create(ip);
        } 
        catch (InvalidFormatException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        sheet = book.getSheet(sheetName);
            
        Object data[][] = new Object[sheet.getLastRowNum()][sheet.getRow(0).getLastCellNum()];

        for (int i = 0; i < sheet.getLastRowNum(); i++)
        {
            for (int k = 0; k < sheet.getRow(0).getLastCellNum(); k++)
            {
                // if(data[i][k]!=null)
                data[i][k] = sheet.getRow(i+1).getCell(k).toString();
            }
        }

        return data;
    }
    catch (FileNotFoundException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.println("Not able to fetch the values from the Excel");
    }

    return null;
}
    

我需要从下拉列表中选择多个值的页面方法:

public void pipeline(String fieldvalue,String savepipe)
{
    elementutils.waitforElementPresent(DealsLink);
    elementutils.doclick(DealsLink);
    elementutils.waitforclickingElement(pipeline);
    elementutils.doclick(pipeline);
    elementutils.waitforElementPresent(Selectfieldsdropdownclick);
    elementutils.doclick(Selectfieldsdropdownclick);
    elementutils.selectvaluefromdropdown(selectfieldsvalueselection, fieldvalue);
}

测试页方法:

@DataProvider
public Object[][] dealpipeline()
{
        Object data[][] = ExcelUtil.getTestData(AppConstants.Deal_Pipeline_Sheet_Name);
        return data;
}
    
@Test(priority=10,dataProvider="dealpipeline")
public void getdealspipelineinfo(String selectfields,String savepipelineas)
{
    dealspage.pipeline(selectfields, savepipelineas);
}

页面:有一个下拉列表,我们可以从下拉列表中一次选择两个值

![在此处输入图像描述][1]

屏幕截图显示了下拉列表

  [1]: https://i.stack.imgur.com/VuP03.png

带有值选择字段 Stage、Commission 的 Excel 文件。

当我运行此测试时,它不会选择 Excel 中的值,也不会显示任何错误。有人可以让我知道需要做什么吗?

标签: automated-teststestngtestng-dataprovidertestng-eclipsetestng-annotation-test

解决方案


您可以使用以下方法在下拉列表中选择值。

public void pipeline(String fieldvalue,String savepipe){
    String[] str = fieldvalue.split(",");
    for(int i=0; i<str.length; i++) {
        //performed the required operations as per requirement by accessing the value using str[i]
    }
}

编辑:

public void multiselectdropdown(By locator,String value) { 
    String[] valueTemp = value.split(",");
    for(int i=0;i<valueTemp.length;i++) { 
        List<WebElement> dropdownoptions = driver.findElements(locator); 
        for(int j=0; j<dropdownoptions.size(); j++) {
            String text = dropdownoptions.get(j).getText();
            try { 
                if(!text.isEmpty()) { 
                    if(text.equalsIgnoreCase(valueTemp[i])) { 
                        dropdownoptions.get(j).click(); 
                        break; 
                    } 
                } 
            }catch (Exception e) { }
        }
    }
}

**注意:您可以从下拉列表中选择最多两个值,因此请根据您的要求更新代码。您看到过时的元素异常,因为下拉列表在每个选择上都得​​到更新。


推荐阅读