0
votes

@Test(priority=1) public void AddUsers1() throws Exception {

  String invalidusername=admin.getData(8, 1);
    String validusername=admin.getData(9, 1);
    String firstname=admin.getData(10, 1);
    String lastname=admin.getData(11, 1);
    String invalidpwd=admin.getData(12, 1);
    String validpwd=admin.getData(13, 1);
    String invalidemail=admin.getData(14, 1);
    String validemail=admin.getData(15, 1);
    String phone=admin.getData(16, 1);



//driver.findElement(By.cssSelector("#expiretxt > button.btn.btn-default")).click();

Thread.sleep(1000);;
driver.findElement(By.linkText("ADMIN")).click();

driver.findElement(By.linkText("Users")).click();

WebDriverWait wait = new WebDriverWait(driver, 50);// 1 minute 

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@href='http://test.com]"))).click();

WebDriverWait wait1 = new WebDriverWait(driver, 40);// 1 minute 
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username"))).click();


//Submitting without giving user name
driver.findElement(By.id("submit-1")).click();


String expectedTitle = "Username is empty.";
String actualTitle = "";

actualTitle = driver.switchTo().alert().getText();

if (actualTitle.contentEquals(expectedTitle)){
    System.out.println("1.Test Passed!-By submitting without user name alert displayed as [User name is empty]"); -----> 

This result should write in existing excel sheet

1
Apache POI should give you what you need poi.apache.orgJsmith2800

1 Answers

0
votes

Import Apache POI jar and create this class. Instantiate by passing parameters at the method where you want to read/right

package pom;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class ExcelOperations 
{

    //public int lastRow;
    public String readExcel(String xlPath, String xlSheet, int rowNum, int cellNum) throws EncryptedDocumentException, InvalidFormatException, IOException
    {

        FileInputStream fis = new FileInputStream(xlPath);
        Workbook wb = WorkbookFactory.create(fis);
        Sheet sh = wb.getSheet(xlSheet);  
        Row row = sh.getRow(rowNum);
        String cellVal = row.getCell(cellNum).getStringCellValue();
        //lastRow = sh.getLastRowNum();
        return cellVal;
    }

    public int lastRowNum(String xlPath, String xlSheet) throws EncryptedDocumentException, InvalidFormatException, IOException
    {
        FileInputStream fis = new FileInputStream(xlPath);
        Workbook wb = WorkbookFactory.create(fis);
        Sheet sh = wb.getSheet(xlSheet);  
        //Row row = sh.getRow(rowNum);
        //String cellVal = row.getCell(cellNum).getStringCellValue();
        int lastRow = sh.getLastRowNum();
        return lastRow;
    }


    public void writeExcel(String xlPath, String xlSheet, int rowNum, int cellNum, String inputString) throws EncryptedDocumentException, InvalidFormatException, IOException 
    {
        FileInputStream fis = new FileInputStream(xlPath);
        Workbook wb = WorkbookFactory.create(fis);
        Sheet sh = wb.getSheet(xlSheet);  
        Row row = sh.getRow(rowNum);
        Cell cell=row.createCell(cellNum);
        cell.setCellValue(inputString);
        FileOutputStream fos=new FileOutputStream(xlPath);
        wb.write(fos);
        fos.close();
    }

}