Receiving javaioFileNotFoundException | Selenium Forum
P
P K Shankar Posted on 15/02/2019

package Ecommerce_Assignment_Day_5;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;


public class Ecommerce_Assignment_Day_5 {

public static void main(String[] args) throws Exception {

System.setProperty("webdriver.chrome.driver","C:\\Users\\pkshank\\Downloads\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String baseUrl = "http://live.guru99.com/index.php/";
String message1 = "Thank you for registering with Main Website Store.";
String message3 = "Your Wishlist has been shared.";
driver.get(baseUrl);
driver.manage().window().maximize();
driver.findElement(By.linkText("MY ACCOUNT")).click();
driver.findElement(By.cssSelector("a[title='Create an Account']")).click();
File src = new File("C:\\Users\\pkshank\\Desktop\\Selenium_Excel_Data\\Ecommerce_Assignment_Day_5.xlsx");
FileInputStream fis = new FileInputStream(src);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet1 = wb.getSheet("Registration_Data");
int rowCount = sheet1.getLastRowNum();

for(int i=1;i<rowCount+1;i++)
{

String fName=sheet1.getRow(i).getCell(0).getStringCellValue();
String mName=sheet1.getRow(i).getCell(1).getStringCellValue();
String lName=sheet1.getRow(i).getCell(2).getStringCellValue();
String eAddress=sheet1.getRow(i).getCell(3).getStringCellValue();
String pwd=sheet1.getRow(i).getCell(4).getStringCellValue();
String cPwd=sheet1.getRow(i).getCell(5).getStringCellValue();
driver.findElement(By.id("firstname")).sendKeys(fName);
driver.findElement(By.id("middlename")).sendKeys(mName);
driver.findElement(By.id("lastname")).sendKeys(lName);
driver.findElement(By.id("email_address")).sendKeys(eAddress);
driver.findElement(By.id("password")).sendKeys(pwd);
driver.findElement(By.id("confirmation")).sendKeys(cPwd);
driver.findElement(By.cssSelector("button[title='Register']")).click();
String message2 = driver.findElement(By.xpath("//li[@class='success-msg']")).getText();
fis.close();
Row row1 = sheet1.createRow(i);
Cell cell1 = row1.createCell(6);


if(message1.contentEquals(message2))
{
cell1.setCellValue("REGISTRATION SUCCESSFULL");
FileOutputStream fos = new FileOutputStream("C:\\Users\\pkshank\\Desktop\\Selenium_Excel_Data\\Ecommerce_Assignment_Day_5.xlsx");
wb.write(fos);
fos.close();
}

else
{

cell1.setCellValue("REGISTRATION NOT SUCCESSFULL");
FileOutputStream fos = new FileOutputStream("C:\\Users\\pkshank\\Desktop\\Selenium_Excel_Data\\Ecommerce_Assignment_Day_5.xlsx");
wb.write(fos);
fos.close();
}

XSSFSheet sheet2 = wb.getSheet("Share_Wishlist_Data");
Row row2 = sheet2.createRow(i);
Cell cell2 = row1.createCell(2);

driver.findElement(By.linkText("TV")).click();
driver.findElement(By.cssSelector("a[href*='product/4'][class='link-wishlist']")).click();
driver.findElement(By.cssSelector("button[title='Share Wishlist']")).click();
String eAddress1 = sheet2.getRow(i).getCell(0).getStringCellValue();
String message = sheet2.getRow(i).getCell(1).getStringCellValue();
driver.findElement(By.id("email_address")).sendKeys(eAddress1);
driver.findElement(By.id("message")).sendKeys(message);
driver.findElement(By.cssSelector("button[title='Share Wishlist']")).click();
String message4 = driver.findElement(By.xpath("//li[@class='success-msg']")).getText();

if(message3.contentEquals(message4))
{
cell2.setCellValue("WISHLIST SHARED SUCCESSFULLY");
FileOutputStream fos = new FileOutputStream("C:\\Users\\pkshank\\Desktop\\Selenium_Excel_Data\\Ecommerce_Assignment_Day_5.xlsx");
wb.write(fos);
fos.close();

}

else
{
cell2.setCellValue("WISHLIST NOT SHARED");
FileOutputStream fos = new FileOutputStream("C:\\Users\\pkshank\\Desktop\\Selenium_Excel_Data\\Ecommerce_Assignment_Day_5.xlsx");
wb.write(fos);
fos.close();
}

driver.findElement(By.cssSelector("a[href*='customer/account']")).click();
driver.findElement(By.linkText("Log Out")).click();;
driver.findElement(By.linkText("MY ACCOUNT")).click();
driver.findElement(By.cssSelector("a[title='Create an Account']")).click();
}


wb.close();
driver.close();
}


}

 


A
Ashish Thakur Replied on 15/02/2019

Did you try debugging the code?

This error comes up when the code is not able to find the file needed.


P
P K Shankar Replied on 18/02/2019

I was able to resolve this issue. Until now I was running the script while the excel sheet was open. When I closed the sheet and ran the script, it ran. But now I am facing this issue: java.lang.NullPointerException.

I am facing this issue at this line of code: sheet2.getRow(i).getCell(2).setCellValue("WISHLIST SHARED SUCCESSFULLY");

This line is just writing that message at row = i and column =2 when an 'if' critreria is satisfied. The column in which I intend to write this mesage is initially empty.

I have another line of code in which I write to excel. That line of code is:

sheet1.getRow(i).getCell(6).setCellValue("REGISTRATION SUCCESSFULL");

This line of code is successfully written to the excel. There is no difference between the 2 lines of code so why is it that I am able to write one message on to the excel and not the other.

 


A
Ashish Thakur Replied on 20/02/2019

Please share your project and its dependencies over here.