Module 17 Exercise 3 -- test functionality of a calculator | Selenium Forum
M
Posted on 18/07/2016
Hi,
For testing the functionality of a calculator using Excel data, I wrote below code but it gives me [b:1hbyvmgk]"Null Pointer Exception"[/b:1hbyvmgk]. So please suggest me where i am wrong or any other way to write the code please suggest.

[b:1hbyvmgk]Exercise--[/b:1hbyvmgk]

Follow these steps and test functionality of a calculator:
a) Go to www.google.com and search for 'calculator'. In the results you will see a calculator coming in the browser itself.
b) Download this xls from here
c) Read the Num1, Num2, Operation and ExpectedResult columns using java code
d) Perform the operation on the calculator as described in xls file
e) Write the value in ExpectedResult column as result of actual operation on the application
f) Write the value in Result coulmn as Pass/Fail depending on output.

[b:1hbyvmgk]Excel Sheet and attached.[/b:1hbyvmgk]


[b:1hbyvmgk]Code --[/b:1hbyvmgk]

[code:1hbyvmgk]
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.xalan.xsltc.compiler.sym;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.w3c.dom.ls.LSInput;

public class Calculator_Excel {
static WebDriver driver=null;
static Actions act = null;
static String actualReslut;
static WebDriverWait wait;
public static void main(String[] args) throws InterruptedException {

driver= new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.google.co.in");
driver.findElement(By.xpath("//*[@id='sb_ifc0']")).sendKeys("calculator");
driver.findElement(By.xpath("//*[@id='sblsbb']/button")).click();

Xls_Reader xl = new Xls_Reader("D:\\Calculator2.xlsx");
int rowcount = xl.getRowCount("Addition");
int Colcount = xl.getColumnCount("Addition");

List<String> Number1 = new ArrayList<String>();
List<String> Number2 = new ArrayList<String>();
List<String> operation_Perform = new ArrayList<String>();

for(int i =2;i<=rowcount;i++){
String Num1 = xl.getCellData("Addition", "Num1", i);
String Num2 = xl.getCellData("Addition", "Num2", i);
String Operation= xl.getCellData("Addition", "Operation", i);
String expected_Result = xl.getCellData("Addition", "ExpectedResult", i);

if(Operation.equals("+")){

String fin = Num1+"+"+Num2;
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='cwtltblr']"))); // ISSUE COMES ON THIS LINE ON EACH STATEMENT
Thread.sleep(3000);
driver.findElement(By.xpath("//div[@id='cwtltblr']")).sendKeys(fin);
act= new Actions(driver);
act.sendKeys(Keys.ENTER).build().perform();
actualReslut= driver.findElement(By.xpath("//div[@id='cwtltblr']/div/span[@id='cwos']")).getText();
xl.setCellData("Addition", "ActualResult", i,actualReslut);


}else if(Operation.equals("-")){
String fin = Num1+"-"+Num2;
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='cwtltblr']")));
Thread.sleep(3000);
driver.findElement(By.xpath("//div[@id='cwtltblr']")).sendKeys(fin);
act.sendKeys(Keys.ENTER).build().perform();
actualReslut= driver.findElement(By.xpath("//div[@id='cwtltblr']/div/span[@id='cwos']")).getText();
xl.setCellData("Addition", "ActualResult", i, actualReslut);

}else if(Operation.equals("*")){
String fin = Num1+"*"+Num2;
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='cwtltblr']")));
Thread.sleep(3000);
driver.findElement(By.xpath("//div[@id='cwtltblr']")).sendKeys(fin);
act.sendKeys(Keys.ENTER).build().perform();
actualReslut= driver.findElement(By.xpath("//div[@id='cwtltblr']/div/span[@id='cwos']")).getText();
xl.setCellData("Addition", "ActualResult", i, actualReslut);

}else if(Operation.equals("/")){
String fin = Num1+"/"+Num2;
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='cwtltblr']")));
Thread.sleep(3000);
driver.findElement(By.xpath("//div[@id='cwtltblr']")).sendKeys(fin);
act.sendKeys(Keys.ENTER).build().perform();
actualReslut= driver.findElement(By.xpath("//div[@id='cwtltblr']/div/span[@id='cwos']")).getText();
xl.setCellData("Addition", "ActualResult", i, actualReslut);

}
}
}
}[/code:1hbyvmgk]

M
Replied on 18/07/2016

use this

[code:bm6s3jdc]package com.soapuitutorial.propertie;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;


public class Calci {
WebDriver driver;

@BeforeTest
public void log() {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://google.com");
WebElement ele = driver.findElement(By.name("q"));
ele.sendKeys("calculator");
ele.sendKeys(Keys.ENTER);
}

@Test(dataProvider = "getData")
public void calculator(String n1, String n2, String op, String exp,
String act, String stat) throws InterruptedException {
// System.out.println(n1+"--"+n2+"--"+op+"--"+exp+"--"+act+"--"+stat);

Thread.sleep(3000);
for (int i = 0; i < n1.length(); i++) {
System.out.println(n1.charAt(i));
driver.findElement(
By.xpath("//span[@class='cwbts'][contains(text(), '"
+ n1.charAt(i) + "')]")).click();
Thread.sleep(500);
}
for (int i = 0; i < op.length(); i++) {
System.out.println(op.charAt(i));
driver.findElement(
By.xpath("//span[@class='cwbts'][contains(text(), '"
+ op.charAt(i) + "')]")).click();
Thread.sleep(500);
}
for (int i = 0; i < n2.length(); i++) {
System.out.println(n2.charAt(i));
driver.findElement(
By.xpath("//span[@class='cwbts'][contains(text(), '"
+ n2.charAt(i) + "')]")).click();
Thread.sleep(500);
}
driver.findElement(
By.xpath("//span[@class='cwbts'][contains(text(), '=')]"))
.click();
Thread.sleep(500);
String actual = driver.findElement(By.xpath("//span[@id='cwos']"))
.getText();
String state = "";
if (actual.contentEquals(exp)) {
state = "pass";
} else {
state = "fail";
}

Xls_Reader xls = new Xls_Reader(
"C:\\Users\\Vishwa\\Desktop\\Calculator.xlsx");
int row = xls.getCellRowNum("Addition", "ActualResult", "");
xls.setCellData("Addition", "ActualResult", row, actual);
xls.setCellData("Addition", "Status", row, state);

Assert.assertEquals(actual, exp);

driver.findElement(
By.xpath("//span[@class='cwbts'][contains(text(), 'AC')]"))
.click();

}

@DataProvider
public Object[][] getData() {
Xls_Reader xls = new Xls_Reader(
"C:\\Users\\Vishwa\\Desktop\\Calculator.xlsx");
int rows = xls.getRowCount("Addition");
int cols = xls.getColumnCount("Addition");
System.out.println(rows + "---" + cols);
Object[][] data = new Object[rows - 1][cols];
for (int rnum = 2; rnum <= rows; rnum++) {
for (int cnum = 0; cnum < cols; cnum++) {
data[rnum - 2][cnum] = xls.getCellData("Addition", cnum, rnum);

}
}

return data;
}
}[/code:bm6s3jdc]