Pages

Monday, June 18, 2018

Selenium WebDriver - Read and Write data from Excel for Gmail Login and Logout

Using Apache POI Jar file.
Apache POI Jar Files Used for this program :  ( Please download latest version )
poi 3.17.jar,
poi-examples-3.17.jar,
poi-excelant-3.17.jar,
poi-ooxml-3.17.jar,
poi-ooxml-schemas-3.17.jar,
poi-scratchpad-3.17.jar.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
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.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;


public class WriteExcel {
    WebDriver d;
    @Test
    public void testDataImport()throws Exception
    {
        d.get("https://www.gmail.com");
        d.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
        d.findElement(By.linkText("SIGN IN")).click();
        WebDriverWait wait = new WebDriverWait(d, 60);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.name("identifier")));
        d.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
        //Read data from excel
        FileInputStream fis=new FileInputStream("D:\\test.xlsx");
        XSSFWorkbook wb=new XSSFWorkbook(fis);
        XSSFSheet s=wb.getSheetAt(0);
       
        String data;
        for(int i=0;i<s.getLastRowNum()+1;i++)
        {
            String username = s.getRow(i).getCell(0).getStringCellValue();
            String password = s.getRow(i).getCell(1).getStringCellValue();
            d.findElement(By.name("identifier")).sendKeys(username);
            d.findElement(By.id("identifierNext")).click();
            d.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
            Thread.sleep(4000);
            Row dataRow = s.createRow(i);
            if(d.findElement(By.xpath("//div[contains(@class,'dEOOab RxsGPe')]")).isDisplayed()){
                System.out.println("Given User Name is wrong");
                screenshot(d, "Emailwrong");
                d.findElement(By.name("identifier")).clear();
                data="fail";
                if(data=="fail") {
                    dataRow.createCell(0).setCellValue(username);
                    dataRow.createCell(1).setCellValue(password);
                    dataRow.createCell(2).setCellValue("Fail");
                }
            }else {
                d.findElement(By.name("password")).sendKeys(password);
                d.findElement(By.id("passwordNext")).click();
                d.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
                Thread.sleep(4000);
                try {
                    if(d.findElement(By.xpath("//a[contains(@class,'gb_b gb_ib gb_R')]")).isDisplayed()){
                        String currenturl = d.getCurrentUrl();
                        System.out.println(currenturl);
                        screenshot(d, "inbox");
                        d.findElement(By.xpath("//a[contains(@class,'gb_b gb_ib gb_R')]")).click();
                        Thread.sleep(2000);
                        d.findElement(By.id("gb_71")).click();
                        d.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
                        Thread.sleep(2000);
                        /*d.findElement(By.xpath("//a[@id='account-chooser-link']")).click();
                    Thread.sleep(2000);
                    d.findElement(By.xpath("//a[@id='account-chooser-add-account']")).click();
                    Thread.sleep(2000);*/
                       
                        d.findElement(By.xpath("//div[contains(@class,'a9cric')]")).click();
                        Thread.sleep(2000);
                        d.findElement(By.xpath("//button[contains(@class,'q4UYxb')]")).click();
                        Thread.sleep(2000);
                        d.findElement(By.xpath("//div[contains(@class,'k6Zj8d asG8Cb')]")).click();
                        Thread.sleep(3000);
                        d.findElement(By.xpath("//div[starts-with(@class,'tk3N6e-LgbsSe-n2to0e')][1]")).click();
                        //d.findElement(By.xpath("//div[contains(@class,'tk3N6e-LgbsSe-n2to0e x81T2e tk3N6e-LgbsSe-ZmdkE tk3N6e-LgbsSe-JbbQac-i5vt6e')]")).click();
                        Thread.sleep(2000);
                        data="pass";
                        if(data=="pass") {
                            dataRow.createCell(0).setCellValue(username);
                            dataRow.createCell(1).setCellValue(password);
                            dataRow.createCell(2).setCellValue("Pass");
                        }
                    }
                }catch(Exception e) {
                    System.out.println("Given Password is wrong");
                    screenshot(d, "passwordwrong");
                    d.findElement(By.xpath("//div[contains(@class,'a9cric')]")).click();
                    Thread.sleep(2000);
                    d.findElement(By.name("identifier")).clear();
                    d.navigate().refresh();
                    d.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
                    data="fail";
                    if(data=="fail") {
                        dataRow.createCell(0).setCellValue(username);
                        dataRow.createCell(1).setCellValue(password);
                        dataRow.createCell(2).setCellValue("Fail");
                    }
                }
            }
           
        }
        try {
            FileOutputStream out =  new FileOutputStream(new File("D:\\test.xlsx"));
            wb.write(out);
            out.close();
            System.out.println("Excel with foumula cells written successfully");
             
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Thread.sleep(1000);
    }
   

@BeforeMethod
    public void setUp()
    {
        // Launch browser - check browser driver exe file location in your system
        /*System.setProperty("webdriver.gecko.driver", "D:\\browsers\\geckodriver.exe");
        d = new FirefoxDriver();*/
        System.setProperty("webdriver.chrome.driver", "D:\\browsers\\chromedriver.exe");
        d=new ChromeDriver();
        d.manage().window().maximize();
        d.manage().deleteAllCookies();
        d.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
        d.manage().timeouts().pageLoadTimeout(3,TimeUnit.MINUTES);
    }
   

@AfterMethod
    public void tearDown()
    {
        // Close browser
        d.quit();
    }
   
    public void screenshot(WebDriver d, String filename) {
        File srcfile = ((TakesScreenshot)d).getScreenshotAs(OutputType.FILE);
        try {
            FileUtils.copyFile(srcfile, new File("D:\\"+filename+".png"));
        }catch(Exception e) {
            e.printStackTrace();
        }
       
    }

}

No comments:

Post a Comment