DWQA Ask QuestionCategory: QuestionsAssignment-Level_Medium: Xpath, Web table Handling-Fetch Amount from the account Summary Table, add them and compare with total
admin Staff asked 5 years ago

Problem Statement: Fetch Amount from 2nd Column from Account Summary Web table, add them and Compare addition total is equal to total displayed in the Application.

Steps to take:

  1. Create a project, Pck, class, main method
  2. Create Driver object using Chrome Driver
  3. Navigate to url: http://parabank.parasoft.com/
  4. Login with: username as “john” and password as “demo”
  5. Fetch all the values out of 2nd column of the Account Summary Web Table (preferably using xpath)
  6. Add all the amount
  7. Fetch the total from the application
  8. Compare  the total of addition with total displayed in the application.
15 Answers
Suraj Gaikwad Staff answered 5 years ago

Fetching column data, add them and Compare total.
 

driver.get(“https://parabank.parasoft.com”);

WebElement username = driver.findElement(By.xpath(“//input[@name=’username’]”));
username.sendKeys(“john”);

WebElement password = driver.findElement(By.xpath(“//input[@name=’password’]”));
password.sendKeys(“demo”);

WebElement login_button = driver.findElement(By.xpath(“//div[@class=’login’]//input[@value=’Log In’]”));
login_button.click();

WebElement account_overview_link = driver.findElement(By.xpath(“//a[text()=’Accounts Overview’]”));
account_overview_link.click();

List<WebElement> balance_column = driver.findElements(By.xpath(“//table[@id=’accountTable’]//td[2][@class=’ng-binding’]”));
double total=0;
double res=0;
for(WebElement bal:balance_column)
{

String temp=bal.getText().replace(“$”, “”);
System.out.println(temp);
res=Double.parseDouble(temp);
total = total+res;

}

WebElement total_on_webpage = driver.findElement(By.xpath(“//b[@class=’ng-binding’]”));
String value1=total_on_webpage.getText().replace(“$”, “”);
double expected_total = Double.parseDouble(value1);

DecimalFormat f = new DecimalFormat(“##.00”);
String temp_total = f.format(total);
String temp_expected_total = f.format(expected_total);

//comparing total is same or not
if(temp_expected_total.equals(temp_total))
{
System.out.println(“Total matched”);
}

System.out.println(“actual Total is : “+total);
System.out.println(“expected total is:” +expected_total);
System.out.println(“Expected total format changed: ” +temp_expected_total);
System.out.println(“Actual total format changed: ” +temp_total);

}
}

Pallavi Gadale Staff answered 5 years ago

code:
WebDriver driver=new FirefoxDriver();
driver.get(“https://parabank.parasoft.com/parabank/overview.htm&#8221;);
driver.manage().window().maximize();
driver.findElement(By.xpath(“//input[@name=’username’]”)).sendKeys(“john”);

driver.findElement(By.xpath(“//input[@type=’password’]”)).sendKeys(“demo”);
Thread.sleep(3000);
driver.findElement(By.xpath(“//input[@value=’Log In’]”)).click();

List<WebElement> data=driver.findElements(By.xpath(“//td[@ class=’ng-binding’][1]”));

String temp;
double result = 0;
double temp1;

for(int i=0;i<data.size();i++)
{
temp=data.get(i).getText();
temp=temp.replace(“$”, “”);
temp1=Double.parseDouble(temp);
result=result+temp1;
System.out.println(“total is =” +result);
}

DecimalFormat f=new DecimalFormat(“##.00”);
String temp_total=f.format(result);
String temp_expected_total=f.format(result);

//comparing total is same or not
if(temp_expected_total.equals(temp_total))
{
System.out.println(“total_match”);
}
System.out.println(“actual_total is:”+result);
System.out.println(“expected_total is:”+result);
System.out.println(“expected_total format changed:”+temp_expected_total);
System.out.println(“actual_total format changed:”+temp_total);
}
}
Output:
total is =-2302.0
total is =-2289.55
total is =-2189.55
total is =-2289.55
total is =-2189.55
total is =-2189.55
total is =-2089.55
total is =-989.5500000000002
total is =-889.5500000000002
total is =341.5499999999997
total is =1692.6699999999996
total_match
actual_total is:1692.6699999999996
expected_total is:1692.6699999999996
expected_total format changed:1692.67
actual_total format changed:1692.67

admin Staff replied 5 years ago

hi pallavi, why are u getting – sign in total is = ‘-2189.55’

Bindiya Patil Staff answered 5 years ago

dynamic webtable handling of account overview

Start your code here

Thread.sleep(5000);
List<WebElement> td1 =driver.findElements(By.xpath(“//table[@id=’accountTable’]//td[@class=’ng-binding’][1]”));
double b=0;
double total=0;

for(int i=0;i<td1.size();i++)

{
String s=td1.get(i).getText().replace(“$”, “”);

System.out.println(s);
b=Double.parseDouble(s);
total=total+b;
//System.out.println(total);
}

DecimalFormat f1=new DecimalFormat(“##.000”);
String temp_total=f1.format(total);
String Expected_total=f1.format(total);
//comparing total balance
if(Expected_total.equals(temp_total))
{
System.out.println(“matched total”);
}
System.out.println(“actual total:”+total);
System.out.println(“expected result:”+total);
System.out.println(“Expected total:”+Expected_total);
System.out.println(“actual total:”+temp_total);
Thread.sleep(5000);
//WebElement account_overview=driver.findElement(By.xpath(“//a[text()=’Accounts Overview’]”));
List<WebElement> td2 =driver.findElements(By.xpath(“//table[@id=’accountTable’]//td[@class=’ng-binding’][2]”));
String temp;

double c;
double total1=0;
for(int j=0;j<td2.size();j++)

{
temp=td2.get(j).getText().replace(“$”, “”);
System.out.println(temp);
//temp=temp.replaceAll(“$”, “Rs”);
c=Double.parseDouble(temp);
total1= total1+c;
}
System.out.println( total1);
//comparing available amount
DecimalFormat f2=new DecimalFormat(“##.000”);
String temp_total1=f2.format(total);
String Expected_total1=f2.format(total);
//comparing total value
if(Expected_total1.equals(temp_total1))
{
System.out.println(“matched total”);
}
System.out.println(“actual total:”+total1);
System.out.println(“expected result:”+total1);
System.out.println(“Expected total:”+Expected_total1);
System.out.println(“actual total:”+temp_total1);
}
}

Nishu padwe Staff answered 5 years ago

Fetch data,add and compre total
driver.get(“https://parabank.parasoft.com&#8221;);
WebElement username= driver.findElement(By.xpath(“//input[@name=’username’]”));
username.sendKeys(“john”);
WebElement password= driver.findElement(By.xpath(“//input[@name=’password’]”));
password.sendKeys(“demo”);
WebElement login= driver.findElement(By.xpath(“//input[@class=’button’]”));
login.click();
WebElement account_overview= driver.findElement(By.xpath(“//a[contains(@href,’overview’)]”));
account_overview.click();
Thread.sleep(5000);
List<WebElement> balance=
driver.findElements(By.xpath(“//table[@id=’accountTable’]//td[@class=’ng-binding’][1]”));
String temp;
double temp1=0;
double result=0;
for(int i=0;i<balance.size();i++)
{
temp=balance.get(i).getText();
temp=temp.replace(“$”, “”);
temp1=Double.parseDouble(temp);
result=result+temp1;
System.out.println(temp);
}
System.out.println(result);
DecimalFormat df=new DecimalFormat(“##.00”);
String temp_total=df.format(result);
String temp_expected_total=df.format(result);
//comparing total is same or not
if(temp_expected_total.equals(temp_total))
{
System.out.println(“Total Match”);
}
System.out.println(“actual_total “+result);
System.out.println(“expected_total “+result);
System.out.println(“expected_total format changed”+temp_expected_total);
System.out.println(“actual_total format changed”+temp_total);

Thread.sleep(5000);
List<WebElement> available_amount=
driver.findElements(By.xpath(“//table[@id=’accountTable’]//td[@class=’ng-binding’][2]”));
String s;
double b=0;
double total=0;
for(int i=0;i<available_amount.size();i++)
{
s=available_amount.get(i).getText();
s=s.replace(“$”, “”);
b=Double.parseDouble(s);
total=total+b;
System.out.println(s);
}
System.out.println(total);
DecimalFormat df1=new DecimalFormat(“##.00”);
String temp_total_1=df1.format(total);
String temp_expected_total_1=df1.format(total);
//comparing total is same or not
if(temp_expected_total_1.equals(temp_total_1))
{
System.out.println(“Total Match”);
}
System.out.println(“actual_total_1 “+total);
System.out.println(“expected_total_1 “+total);
System.out.println(“expected_total format changed: “+temp_expected_total_1);
System.out.println(“actual_total format changed:”+temp_total_1);

}
}

Nishu padwe Staff answered 5 years ago

Fetch data,add and compare total
driver.get(“https://parabank.parasoft.com&#8221;);
WebElement username= driver.findElement(By.xpath(“//input[@name=’username’]”));
username.sendKeys(“john”);
WebElement password= driver.findElement(By.xpath(“//input[@name=’password’]”));
password.sendKeys(“demo”);
WebElement login= driver.findElement(By.xpath(“//input[@class=’button’]”));
login.click();
WebElement account_overview= driver.findElement(By.xpath(“//a[contains(@href,’overview’)]”));
account_overview.click();
Thread.sleep(5000);
List<WebElement> balance=
driver.findElements(By.xpath(“//table[@id=’accountTable’]//td[@class=’ng-binding’][1]”));
String temp;
double temp1=0;
double result=0;
for(int i=0;i<balance.size();i++)
{
temp=balance.get(i).getText();
temp=temp.replace(“$”, “”);
temp1=Double.parseDouble(temp);
result=result+temp1;
System.out.println(temp);
}
System.out.println(result);
DecimalFormat df=new DecimalFormat(“##.00”);
String temp_total=df.format(result);
String temp_expected_total=df.format(result);
//comparing total is same or not
if(temp_expected_total.equals(temp_total))
{
System.out.println(“Total Match”);
}
System.out.println(“actual_total “+result);
System.out.println(“expected_total “+result);
System.out.println(“expected_total format changed”+temp_expected_total);
System.out.println(“actual_total format changed”+temp_total);

Thread.sleep(5000);
List<WebElement> available_amount=
driver.findElements(By.xpath(“//table[@id=’accountTable’]//td[@class=’ng-binding’][2]”));
String s;
double b=0;
double total=0;
for(int i=0;i<available_amount.size();i++)
{
s=available_amount.get(i).getText();
s=s.replace(“$”, “”);
b=Double.parseDouble(s);
total=total+b;
System.out.println(s);
}
System.out.println(total);
DecimalFormat df1=new DecimalFormat(“##.00”);
String temp_total_1=df1.format(total);
String temp_expected_total_1=df1.format(total);
//comparing total is same or not
if(temp_expected_total_1.equals(temp_total_1))
{
System.out.println(“Total Match”);
}
System.out.println(“actual_total_1 “+total);
System.out.println(“expected_total_1 “+total);
System.out.println(“expected_total format changed: “+temp_expected_total_1);
System.out.println(“actual_total format changed:”+temp_total_1);

}
}

Reshma M Staff answered 5 years ago

handling  of dynamic webtable
 
 
List<WebElement> Balance= driver.findElements
(By.xpath(“//table[@id=’accountTable’]//td[@class=’ng-binding’][1]”));
double Total_Balance=0;
int i=0;
int total_node=Balance.size();
double d=0;
for(i=0;i<total_node;i++)
{
Thread.sleep(2000);
String d11 =Balance.get(i).getText().replace(“$”, “”);
d =Double.parseDouble(d11);
Total_Balance= Total_Balance+d;
}

System.out.println( Total_Balance);

WebElement expected_total=driver.findElement(By.xpath(“//b[@class=’ng-binding’]”));
String e= expected_total.getText().replace(“$”, “”);
double d11 =Double.parseDouble(e);
DecimalFormat df = new DecimalFormat(“##.00”);
String temp_total_bal=df.format(Total_Balance);
String temp_d11=df.format(d11);

if(temp_total_bal.equals(temp_d11))
{
System.out.println(“total matched”);
}

List<WebElement> total= driver.findElements
(By.xpath(“//table[@id=’accountTable’]//td[@class=’ng-binding’][2]”));
double Total_AvailableAmount = 0;

for( int j=0;j<total.size();j++)
{
String t= total.get(j).getText().replace(“$”, “”);
double d1 =Double.parseDouble(t);

Total_AvailableAmount=Total_AvailableAmount + d1;

}
System.out.println(Total_AvailableAmount);
}

Chintan answered 5 years ago

Dymanic Webtable Handle
 
package parabank;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class login {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\chintan\\Downloads\\chromedriver_win32\\chromedriver.exe”);
WebDriver driver=new ChromeDriver();
driver.get(“http://parabank.parasoft.com/parabank/index.htm&#8221;);
driver.manage().window().maximize();
WebElement username=driver.findElement(By.xpath(“//input[@name=’username’]”));
username.sendKeys(“john”);
WebElement password=driver.findElement(By.xpath(“//input[@name=’password’]”));
password.sendKeys(“demo”);
WebElement login=driver.findElement(By.xpath(“//input[@type=’submit’]”));
login.click();

List<WebElement> balance_column = driver.findElements(By.xpath(“//table[@id=’accountTable’]//td[@class=’ng-binding’][1]”));
double total=0;
double res=0;
for(WebElement bal:balance_column)
{
String temp=bal.getText().replace(“$”, “”);
System.out.println(temp);
res=Double.parseDouble(temp);
total = total+res;
}

}
}
 

Sneha Funde Staff answered 5 years ago

Fetch Data >> Add >> Compare
List<WebElement> table=driver.findElements(By.xpath(“//table[@id=’accountTable’]//td[@class=’ng-binding’][1]”));
//String text=table.get(0).getText();
String text;
double Result=0;
double temp1=0;

for(int i=0;i<table.size();i++)
{
text=table.get(i).getText();
text=text.replace(“$”,””);
temp1=Double.parseDouble(text);
Result=Result+temp1;

System.out.println(temp1);
}
System.out.println(“Total = “+Result);

DecimalFormat f=new DecimalFormat(“##.00”);
System.out.println(f.format(Result));

WebElement total=driver.findElement(By.xpath(“//b[text()=’$1692.67′]”));
String text1=total.getText();
text1=text1.replace(“$”, “”);
if(text1.equals(f.format(Result)))
{
System.out.println(text1+” = “+f.format(Result));
}
}
}
 

 
Sneha Funde Staff answered 5 years ago

Fetch Data >> Add >> Compare
List<WebElement> table=driver.findElements(By.xpath(“//table[@id=’accountTable’]//td[@class=’ng-binding’][1]”));
//String text=table.get(0).getText();
String text;
double Result=0;
double temp1=0;

for(int i=0;i<table.size();i++)
{
text=table.get(i).getText();
text=text.replace(“$”,””);
temp1=Double.parseDouble(text);
Result=Result+temp1;

System.out.println(temp1);
}
System.out.println(“Total = “+Result);

DecimalFormat f=new DecimalFormat(“##.00”);
System.out.println(f.format(Result));

WebElement total=driver.findElement(By.xpath(“//b[text()=’$1692.67′]”));
String text1=total.getText();
text1=text1.replace(“$”, “”);
if(text1.equals(f.format(Result)))
{
System.out.println(text1+” = “+f.format(Result));
}
}
}
 

 
Pragati Shamkuwar Staff answered 5 years ago
driver.get("http://parabank.parasoft.com/parabank/index.htm");
driver.manage().window().maximize();
Thread.sleep(5000);
driver.findElement(By.xpath("//input[@name='username']")).sendKeys("john");
driver.findElement(By.xpath("//input[@type='password']")).sendKeys("demo");
driver.findElement(By.xpath("//input[@type='submit']")).click();
List<WebElement> list=driver.findElements(By.xpath("//td[@class='ng-binding'][1]"));
String temp;
String temp3;
double result2;
double temp1, result=0;

for(int i=0; i<list.size(); i++)
{
temp=list.get(i).getText().replace("$", "");
System.out.println(temp);
temp1=Double.parseDouble(temp);
result = result+temp1;
System.out.println(result);
}

WebElement total=driver.findElement(By.xpath("//b[@class='ng-binding']"));
temp3=total.getText().replace("$", "");
result2=Double.parseDouble(temp3);
DecimalFormat df1 = new DecimalFormat("#.##");

String total1 = df1.format(result);
String expected_total = df1.format(result2);
System.out.println(df1.format(result2));

if(expected_total.equals(total1))
{
System.out.println("match");
}
else {
System.out.println("not match");
}


}

}