For each loop below not adding all 6 values from the list. Instead It just repeats the first value 6times. Also Json file created with the same inputs.
Note: I also referred to this link but not getting the expected output. Javascript each loop over JSON only getting first element?
Steps followed are, 1. Find the tr elements and read data one by one 2. While reading store these values also using Map 3. Add the map values into Jason Array.
Below is my code,
public static void GetPercentageValue(String locatorType, String locatorValue) throws FileNotFoundException
{
locatorType="xpath";
locatorValue="//div[@class='content shadow']/div/child::table[1]/tbody/tr";
List<WebElement> tableRows=findTheElements(locatorType,locatorValue);
for(WebElement tablerow : tableRows)
{
Map map = new LinkedHashMap(tableRows.size());
row=row+1;
String rowKeys=tablerow.findElement(By.xpath("//div[@class='content shadow']/div/child::table[1]/tbody/tr['"+row+"']/td[1]")).getText();
String price=tablerow.findElement(By.xpath("//div[@class='content shadow']/div/child::table[1]/tbody/tr['"+row+"']//input")).getAttribute("value");
System.out.println("RowDesc & Price: "+rowKeys+" "+price);
map.put(rowKeys, price);
jArr.add(map);
}
jaObj.put("Values", jArr);
PrintWriter pWriter = new PrintWriter(mbsDataPath1);
pWriter.write(jaObj.toJSONString());
pWriter.flush();
pWriter.close();
}
Expected Output should be:
RowDesc & Price: <80.000 0
RowDesc & Price: 80.000-150.000 2
RowDesc & Price: 150.000-300.000 10
RowDesc & Price: 300.000-500.000 15
RowDesc & Price: >500.000 18
Actual Output of the above code is:
RowDesc & Price: <80.000 0
RowDesc & Price: <80.000 0
RowDesc & Price: <80.000 0
RowDesc & Price: <80.000 0
RowDesc & Price: <80.000 0