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
Nitu A Staff answered 5 years ago

public static void main(String[] args)

{

String ActualTotal;
String Total ;

try{
WebDriver driver;
System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\HP\\Downloads\\selenium folder\\chromedriver.exe\\”);
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get(“https://parabank.parasoft.com”);
driver.findElement(By.xpath(“//input[@name=’username’]”)).sendKeys(“john”);
driver.findElement(By.xpath(“//input[@name=’password’]”)).sendKeys
(“demo”);
driver.findElement(By.xpath(“//input[@value=’Log In’ and @type=’submit’]”)).click();
List<WebElement> Data =driver.findElements(By.xpath(“//table[@id=’accountTable’]//td[2][@class=’ng-binding’]”));
System.out.println(“Total No of rows are : ” + Data.size());
double Addition=0;
double temp1=0;
double ExactAmount =0;
for(int i=0;i<Data.size();i++)
{
String temp =Data.get(i).getText().replace(“$”, “”);

temp1=Double.parseDouble(temp);
Addition=Addition+temp1;
DecimalFormat F = new DecimalFormat(“##.00”);
ActualTotal = F.format(Addition);
ExactAmount =Double.parseDouble(ActualTotal);
}
System.out.println(“Total No of balance is : ” + ExactAmount);

WebElement $Total = driver.findElement(By.xpath(“//table[@id=’accountTable’]//b[@class=’ng-binding’]”));
Total =$Total.getText().replace(“$”, “”);
double temp_Total = Double.parseDouble(Total);
System.out.println(“Total : ” + temp_Total);
if( ExactAmount==temp_Total){
System.out.println(“Addition of balance is same as total amount”);
}else{
System.out.println(“Addition of balance is not same as total amount”);
}
}
catch(Exception exp){
System.out.println(exp.getMessage());

}

}}

Sonali Adik Staff answered 5 years ago

public class Parabank {
public static void main(String[] args) throws InterruptedException {

System.setProperty(“webdriver.chrome.driver”,”C:\\sonali\\chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
//driver.manage().timeouts().pageLoadTimeout(6500, TimeUnit.SECONDS);
//driver.manage().timeouts().implicitlyWait(4000, TimeUnit.SECONDS);
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);

}

}

Sneha Funde Staff answered 5 years ago
Start your code here 
Handling WebTable :

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
Start your code here 
Handling WebTable :

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
Start your code here 
Handling WebTable :

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));
}
}