首页 > 解决方案 > 如何修复“列表类型不是通用的;它不能用参数参数化“黄瓜硒JAVA中的错误

问题描述

我尝试使用数据表并实现我的函数来从这个 Cucumber 数据表中获取值,我使用了 List< List< String >> 但它不起作用!

public void myfunction(DataTable dt) throws Throwable {

列表> 列表 = dt.asList(String.class);

driver.findElement(By.id("name")).sendKeys(list.get(0).get(0));
driver.findElement(By.id("age")).sendKeys(list.get(0).get(1));
driver.findElement(By.id("nphone")).sendKeys(list.get(1).get(0));
driver.findElement(By.id("address")).sendKeys(list.get(1).get(1));

}

标签: javaeclipsedrop-down-menuselenium-chromedrivercucumber-jvm

解决方案


使用Header我们可以以非常干净和精确的方式实现数据表,并且考虑到数据表如下所示 -

And fill up the first & last name form with the following data
    | First Name | Last Name |
    |    Tom     |    Adam   |
    |   Hyden    | Pointing  |

public void myfunction(DataTable table) throws Throwable {

List<Map<String, String>> list = table.asMaps(String.class,String.class); 

driver.findElement(By.id("name")).sendKeys(list.get(0).get("First Name"));
driver.findElement(By.id("age")).sendKeys(list.get(0).get("Last Name"));
driver.findElement(By.id("nphone")).sendKeys(list.get(1).get("First Name"));
driver.findElement(By.id("address")).sendKeys(list.get(1).get("Last Name"));

}

实施规则 -下面是 2 个片段,最有趣的片段是第一个片段,它表明该方法的参数是 DataTable 数据表。该片段建议您应该将 DataTable dataTable 参数替换为以下任何一项:

 - List<E>
 - List<List<E>>
 - List<Map<K,V>>
 - Map<K,V>
 - Map<K, List<V>>

它还告诉我们每种类型 E、K、V 必须是以下任何一种类型:

  • 细绳
  • 整数
  • 漂浮,
  • 双倍的
  • 字节
  • 短的
  • 大整数
  • 大十进制

推荐阅读