Groovy lists and maps -


i writing helper methods selenium tests. 1 of them :

    private static list<datarow> parsetable(webelement table) {     list<webelement> tableheaders = table.findelements(by.tagname("th"))     list<datarow> datarow = table.findelements(by.xpath(".//tbody/tr")).collect {         map<string, string> columns = [:]         it.findelements(by.tagname("td")).eachwithindex { item, ->             columns[tableheaders.get(i).text] = item.text         }         new datarow(it, columns)     }     return datarow } 

and dont part:

 it.findelements(by.tagname("td")).eachwithindex { item, ->         columns[tableheaders.get(i).text] = item.text     } 

is there better way make map 2 lists?

you should able do:

def columns = [tableheaders,it.findelements(by.tagname("td"))].transpose().collectentries() 

by way of explanation:

given:

def = [ 'a', 'b', 'c' ] def b = [ 1, 2, 3 ] 

then

def c = [ a, b ].transpose() assert c == [ [ 'a', 1 ], [ 'b', 2 ], [ 'c', 3 ] ] 

and:

def d = c.collectentries() assert d instanceof map assert d == [ a:1, b:2, c:3 ] 

Comments