Home > Programming, QA, Test and Automation Stuffs > Using hashtable for include many values in an array – Webdriver (Java)

Using hashtable for include many values in an array – Webdriver (Java)

Hello again,

I had this new thing that I have learned and use almost every days.

You can use this when you need return many values of some table.This code will return a list of things with a column and theirs line. So, here go the code:

Hashtable valores = new Hashtable<String, String>();

int qtdLinesTblSimAcu = 0;

if(tableSimPresent)

{

String[] cTabelSim = new String[10];

cTabelSim[1] = “ID”;

cTabelSim[2] = “Buyer.Firm”;

cTabelSim[3] = “Buyer.Account”;

cTabelSim[4] = “Buyer.Qty”;

cTabelSim[5] = “Seller.Firm”;

cTabelSim[6] = “Seller.Account”;

cTabelSim[7] = “Seller.Qty”;

cTabelSim[8] = “Price”;

cTabelSim[9] = “Traded.Qty”;

//LET CATCH ALL THE LINES AND PUT IN HASHTABLE

int qtdLinhasTblSim = (Integer) selenium.getXpathCount(//html/tbody/table/tr“)); // CHANGE FOR THE CORRECT XPATH ONLY UNTIL THE TR TAG.

qtdLinesTblSimAcu += qtdLinesTblSim;

if(qtdLinesTblSim > 0)

{

//NAVIGATE FOR LINES AND COLUMNS

for(int l = 2; l <= qtdLinhasTblSim; l++)

{

//NAVIGATE FOR COLUMNS

for(int c = 1 ;c < 10;c++) //CHANGE FOR THE NUMBER OF COLUMNS

{

String val  = selenium.getText(String.format(“xpath=//html/tbody/tr[%s]/td[%s]“),l,c)); /*CHANGE FOR THE XPATH CORRECT, IN THE %s YOU WILL SUBSTITUTE RESPECTIVELY FOR VALUE OF L AND C, LIKE LINE AND COLUMN. */

values.put(cTabelSim[c] + “:” + ((l + qtdLinesTblSimAcu) -1),val);

}

}

}

/* THIS IS THE END OF THE RETURN, IF YOU HAVE CATCH VALUES OF ANOTHER TABLE, PUT THE FOR FOR CATCH THE VALUES OF THIS TABLE, AND THE CODE BELOW IN THE END OF THIS LAST FOR   */

String[][] returnE = new String[values.size()][2];

Enumeration e = values.keys();

int index = -1;

while(e.hasMoreElements())

{

index++;

String k = (String) e.nextElement();

returnE[index][0] = k;

returnE[index][1] = values.get(k).toString();

}

return returnE ;

}

The return will be something like this:

columnA:1 valueA1

columnA:2 valueA2

columnB:1 valueB1

columnC:1 valueC1

columnC:2 valueC2

columnC:3 valueC3

Bye bye, guys !!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: