Module 17 Exercise: solution: functionality of a calculator | Selenium Forum
M
Posted on 22/03/2016
The code below works with the following cases:
1. WebDriverWait sometimes pass and most of the times says no such element. but Thread.sleep overcomes it.
2. In the excel sheet the multiplication, subtraction and division symbol had to be changed as per the web element text. and then it works.
3.We have to close the excel sheet for the code to write the results to excel sheet.

code is below with the modified excel sheet attached.


package module_17_Exercise;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.poi.hdgf.streams.Stream;
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;

import module171.Xls_Reader;

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

M
Replied on 23/03/2016

thank you.