Reputation: 15
I tried to use the data table and implements my function to fetch the values from this Cucumber data table, I used List< List< String >> but it doesn't work !
public void myfunction(DataTable dt) throws Throwable {
List> list = 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));}
Upvotes: 0
Views: 3553
Reputation: 21
Check your imports, please. I have downloaded java.awt.list by mistake. It worked when I imported java.util.list.
Like :
import java.util.list;
Upvotes: 2
Reputation: 1996
Using Header we can implement Data Table in much clean & precise way and considering Data Table looks like below one -
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"));
}
Implementation Rules - Below are 2 snippet and the most interesting snippet is the first one, the one that suggest that the argument to the method is a DataTable dataTable. The snippet suggests that you should replace the DataTable dataTable argument with any of:
- List<E>
- List<List<E>>
- List<Map<K,V>>
- Map<K,V>
- Map<K, List<V>>
It also tells us that each type, E, K, V must be of any of these types:
Upvotes: 2