automated-tests - 如何使用数据驱动方法从下拉列表中选择 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 中的值,也不会显示任何错误。有人可以让我知道需要做什么吗?
解决方案
您可以使用以下方法在下拉列表中选择值。
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) { }
}
}
}
**注意:您可以从下拉列表中选择最多两个值,因此请根据您的要求更新代码。您看到过时的元素异常,因为下拉列表在每个选择上都得到更新。
推荐阅读
- php - 如何在不更新或创建新行的情况下向同一用户添加更多数据?
- c - 为什么只使用在这个 C 程序的参数列表中声明的指针进行引用传递?
- postgresql - 基于“动态”模式名称定义外键
- c++ - 关于在模板中使用构造函数初始化程序的 C++ 问题
- python - 将 CELL 中最常见的元素附加到 pandas 列
- angularjs - 使用 AngularJS 扩展应用时解决 HTTP 获取失败
- javascript - 在three.js中使用正交相机进行不精确的光线投射
- javascript - 角度嵌套表单组验证
- javascript - 如何在我的计算器功能中实现承诺?
- selenium - 任何人都可以建议我使用任何工具来安排我的 UI 测试,以便它在给定的时间间隔自动开始测试