Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
5.7k views
by Contributing Tester (78 points)
retagged by
In excel i have value like 12/10/2017. How to pass this value in date picker in selenium using java language.
by Master (1.2k points)
have you tried any code? can you post your code, please? This will help us understand current libraries you are using. Also, post any error stack trace.
by Contributing Tester (78 points)
Actually i dont know how to pass the date from excel to open the date picker sir.
@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();
    }

1 Answer

0 like 0 dislike
by Master (1.2k points)

Your code does not really say what kind of library you use, as you have created the wrapper for each functionality.

Here is what I would do,

First, read the date from Excel file using Apache POI

DataFormatter dataFormatter = new DataFormatter();
String cellStringValue = "";

if (HSSFDateUtil.isCellDateFormatted(row.getCell(0))) {

cellStringValue = dataFormatter.formatCellValue(row.getCell(0).getDateCellValue());
}

Now using Selenium's send keys command, you can pass on those values to your element.

driver.findElement(By.id("txtStartDate")).sendKeys(cellStringValue â€‹);

This is what your code is doing I believe.

Try that and let me know.

by Contributing Tester (78 points)
edited by
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();
    }
}


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...