This is my complete code
package mytestpackage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
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;
//import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
//import com.google.common.base.Verify;
//import com.google.common.base.Function;
import org.testng.asserts.SoftAssert;
public class Addcourse {
WebDriver driver;
WebDriverWait wait;
XSSFWorkbook workbook;
XSSFSheet sheet;
XSSFCell cell;
String actualTitle;
String expectedTitle;
@BeforeTest
public void TestSetup() {
// Set the path of the Firefox driver.
System.setProperty("webdriver.chrome.driver", "");
driver = new FirefoxDriver();
//driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("");
// driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
wait = new WebDriverWait(driver, 25);
}
private XSSFCell getCellValue(int row, int column) {
XSSFCell cell = sheet.getRow(row).getCell(column, Row.CREATE_NULL_AS_BLANK);
return cell;
}
public void ExcelRead() throws IOException {
File src = new File("");
FileInputStream finput = new FileInputStream(src);
workbook = new XSSFWorkbook(finput);
}
@Test(priority = 1)
public void Signin() throws IOException, InterruptedException {
SoftAssert softAssert = new SoftAssert();
ExcelRead();
sheet = workbook.getSheetAt(0);
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
try {
cell = getCellValue(i, 1);
String cellValue = getCellValueAsString(cell);
wait.until(ExpectedConditions.elementToBeClickable(By.id("LG")));
driver.findElement(By.id("LG")).clear();
driver.findElement(By.id("LG")).sendKeys(cellValue);
cell = getCellValue(i, 2);
String cellValue1 = getCellValueAsString(cell);
driver.findElement(By.id("PWD")).clear();
driver.findElement(By.id("PWD")).sendKeys(cellValue1);
driver.findElement(By.id("btnSubmit")).click();
String actualTitle = driver.getTitle();
String expectedTitle = "IntouchOnDemand: Menu";
softAssert.assertEquals(actualTitle, expectedTitle);
File src = new File("D:\\BALA\\TestData3.xlsx");
FileOutputStream foutput = new FileOutputStream(src);
if (driver.getTitle().equalsIgnoreCase(expectedTitle)) {
String message = "Passed ";
sheet.getRow(i).createCell(3).setCellValue(message);
workbook.write(foutput);
} else {
String message1 = "Failed";
sheet.getRow(i).createCell(3).setCellValue(message1);
workbook.write(foutput);
}
} catch (Exception e) {
}
}
softAssert.assertAll();
}
@Test(priority = 2)
public void AddCourse_Valid_Data() throws IOException, InterruptedException {
driver.findElement(By.linkText("Training")).click();
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("» Add")));
driver.findElement(By.linkText("» Add")).click();
SoftAssert softAssert = new SoftAssert();
ExcelRead();
sheet = workbook.getSheetAt(1);
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
try {
// driver.findElement(By.linkText("Add a Course")).click();
cell = getCellValue(i, 1);
String cellValue = getCellValueAsString(cell);
wait.until(ExpectedConditions.elementToBeClickable(By.id("txtTitle")));
driver.findElement(By.id("txtTitle")).sendKeys(cellValue);
cell = getCellValue(i, 2);
String cellValue9 = getCellValueAsString(cell);
driver.findElement(By.id("txtStartDate")).sendKeys(cellValue9);
// driver.findElement(By.linkText("2")).click();
// driver.findElement(By.id("txtEndDate")).click();
// driver.findElement(By.linkText("9")).click();
// wait.until(ExpectedConditions.elementToBeClickable(By.linkText("action")));
driver.findElement(By.name("action")).click();
} catch (Exception e) {
}
}
softAssert.assertAll();
}
Read more:
https://softwaretestingboard.com/qna/2478/read-date-value-from-excel-for-the-date-picker-selenium-java#ixzz4xztdBpSg
public static String getCellValueAsString(Cell cell) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
return cell.getRichStringCellValue().getString();
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
// return cell.getStringCellValue();
return cell.getDateCellValue() + "";
// return cell.getDateCellValue().toString();
} else {
return (int) cell.getNumericCellValue() + "";
// return cell.getNumericCellValue();
}
case Cell.CELL_TYPE_BOOLEAN:
return ((Boolean) cell.getBooleanCellValue()).toString();
case Cell.CELL_TYPE_FORMULA:
return cell.getCellFormula().toString();
}
return null;
}
@AfterTest
public void tearDown() throws Exception {
driver.close();
}
}