如何使用数据驱动方法从下拉列表中选择2个或多个值-现在正在工作



这是我的代码:

Excel util:

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

Excel文件的值选择字段舞台,委员会。

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

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

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

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

最新更新