Thursday, 26 November 2015

Read test data from xl sheet for gmail login

package interview;

import java.io.Console;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.concurrent.TimeUnit;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
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.firefox.FirefoxDriver;
import org.testng.Assert;

import com.google.common.base.Verify;

public class Read_data_from_xl_POI
{
     static WebDriver d;
public static void main(String[] args) throws Exception {
   
    d=new FirefoxDriver();
    d.get("http://gmail.com");
   
    File src=new File("D:\\selenium_NEW_PRACTICE\\test_data\\read_excel_data_test.xls");
    FileInputStream fis=new FileInputStream(src);
    HSSFWorkbook wb= new HSSFWorkbook(fis);
    HSSFSheet sheet1=wb.getSheetAt(0);



//imp note: For xl file use : HSSF
//for .xlsx file use: XSSF
   
    int totalRowCount=sheet1.getLastRowNum();
    System.out.println("total rows is:" + totalRowCount);
   
    String firstRowName=sheet1.getRow(0).getCell(0).getStringCellValue();
    System.out.println("first row first column name is: " + firstRowName);
   
    String secondColFirstName=sheet1.getRow(0).getCell(1).getStringCellValue();
    System.out.println("first row first column name is: " + secondColFirstName);
   
    //for print all the rows usng for loop
   
    for(int i=0;i<totalRowCount;i++)
    {
        String userName=sheet1.getRow(i).getCell(0).getStringCellValue();
        System.out.println("first row all names:" + userName);
       
        String passwordName=sheet1.getRow(i).getCell(1).getStringCellValue();
        System.out.println("first row all names:" + passwordName);
       

/*d=new FirefoxDriver();
d.get("http://gmail.com");*/
d.manage().window().maximize();
d.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
d.findElement(By.xpath("//*[@id='Email']")).sendKeys(userName);
d.findElement(By.xpath("//*[@id='next']")).click();

d.findElement(By.xpath("//*[@id='Passwd']")).sendKeys(passwordName);
d.findElement(By.xpath("//*[@id='signIn']")).click();
String actErrorMsg=d.findElement(By.xpath("//*[@id='errormsg_0_Passwd']")).getText();
String expErrorMsg="The email and password you entered don't match.";
System.out.println("actual message is: " +actErrorMsg);

//Assert.assertEquals("The email and password you entered don't match.", actErrorMsg);

if(expErrorMsg.contains(actErrorMsg))
        {
    System.out.println("sucess");
        }else
            System.out.println("not sucess");
d.navigate().back();

    }
}
}

No comments:

Post a Comment