Can't type into google calculator textbox | Selenium Forum
M
Posted on 13/08/2016
Hi,
I am trying to do exercises given at the end of module 17..

I am stuck at one place while doing calculator exercise, I cant type into the calculator textbox... whenever I try, it types into the google searchbox instead of typing into calculator..

Please help me out how can I get it done ? I m trying this:
driver.findElement(By.xpath("//*[@id='cwtltblr']/div[2]/span")).sendKeys(num1);

Thanks,
Dhaval

M
Replied on 13/08/2016

try this

[code:2szynvip]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:2szynvip]


M
Replied on 14/08/2016

Thanks for the Reply. I have tried the similar thing, but I think I have done it a little complex. Here is my code.

[code:2t9rpbcz]
import java.util.ArrayList;
import java.util.List;
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.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import referenceClasses.OperateExcel;


public class Calculator {

static WebDriver driver = null;
WebElement simple_Calc = null;
List<WebElement> Calc_Operators = null;
ArrayList<String> Calc_Index = null;
int[] Operator = new int[4];
int iterator = 2;

@BeforeTest
public void OpenCalculator(){
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

driver.get("http://www.google.com");
driver.findElement(By.xpath("//input[@title='Search']")).sendKeys("Calculator" + Keys.ENTER);

simple_Calc = driver.findElement(By.xpath("//*[@id='cwmcwd']/div/div/div[3]/div[2]/div[2]"));
Calc_Operators = simple_Calc.findElements(By.xpath("//span[@class='cwbts']"));

Calc_Index = new ArrayList<String>();

int j=0;
//Table<String, int> Calc_Index = new Table<String, int>();

for(int i = 0; i < Calc_Operators.size(); i++){
System.out.println(Calc_Operators.get(i).getText());
Calc_Index.add(i, Calc_Operators.get(i).getText().trim());
if (Calc_Operators.get(i).getText().equals("9") ||
Calc_Operators.get(i).getText().equals("6") ||
Calc_Operators.get(i).getText().equals("3") ||
Calc_Operators.get(i).getText().equals("=")
){
Operator[j] = i+1;
j++;
}


}


}

@AfterTest
public void CloseBrowser(){
driver.close();
}

@Test(dataProvider = "getData")
public void Google_Calculator(String num1, String num2, String Operation, String expRes) throws InterruptedException {

char c;
for (int i = 0; i < num1.length() ; i++){
c = num1.charAt(i);
if (Calc_Index.indexOf(String.valueOf(c)) != -1)
Calc_Operators.get(Calc_Index.indexOf(String.valueOf(c))).click();

}
//Thread.sleep(1000);

c = Operation.charAt(0);
int index = -1;

if (c == '/') index = Operator[0];
else if (c == '*') index = Operator[1];
else if (c == '-') index = Operator[2];
else if (c == '+') index = Operator[3];


if (index != -1)
Calc_Operators.get(index).click();
//Thread.sleep(1000);

for (int i = 0; i < num2.length() ; i++){
c = num2.charAt(i);
if (Calc_Index.indexOf(String.valueOf(c)) != -1)
Calc_Operators.get(Calc_Index.indexOf(String.valueOf(c))).click();

}
//Thread.sleep(1000);

if (Calc_Index.indexOf("=") != -1)
Calc_Operators.get(Calc_Index.indexOf("=")).click();
Thread.sleep(1000);

String ResultText = driver.findElement(By.xpath("//*[@id='cwtltblr']/div[2]/span")).getText();


double ActualResult = Double.parseDouble(ResultText);
double ExpectedResult = Double.parseDouble(expRes);

OperateExcel exl = new OperateExcel(System.getProperty("user.dir") + "//Calculator.xlsx");
exl.setCellData("Addition", iterator, "ActualResult", ResultText);
if (ActualResult == ExpectedResult)
exl.setCellData("Addition", iterator, "Status", "Pass");
else
exl.setCellData("Addition", iterator, "Status", "Failed");

iterator++;
}

@DataProvider
public Object[][] getData(){

OperateExcel exl = new OperateExcel(System.getProperty("user.dir") + "//Calculator.xlsx");
int r = exl.get_totalRows();

Object[][] myData = new Object[r-1][4];

for (int i = 2; i <= r ; i++){
for(int j = 1; j < 5; j++){
myData[i-2][j-1] = exl.getCellData(i, j);
System.out.println(myData[i-2][j-1]);
}
}
return myData;
}


}

[/code:2t9rpbcz]


M
Replied on 15/08/2016

are you still having trouble?


M
Replied on 16/08/2016

Now I can run the code successfully. Thanks for the help.