Friday, 9 January 2015

Gmail Login Program using data driven framework

Hi Friends, in my last post i explained how to write simple Gmail login program, in this post I'm going to explain you how to write Gmail login program using Data Driven framework, in short I'm gonna read Gmail login values from Excel file(.xls), please follow the below steps carefully.

1) In Eclipse, Create a new project, then package then one class.

2) Add respective jar files.

3) Then download jxl jar files from the website:http://www.java2s.com/Code/Jar/j/Downloadjxljar.htm

4) In the above mentioned website download zip file with a name "jxl/jxl.jar.zip( 672 k)".

5) Once you download then extract zip file at an appropriate place, and after extracting then you will see one folder with a name "jxl.jar", now open that you will see one jar file with a name "jxl.jar".

6) Now you need to add 'jxl.jar' into your project , please see below steps.
    a) Right click on your project then select properties.
    b)Properties window will open now click on 'Java Build Path' then on 'libraries'.
    c) In Libraries click 'Add External JARs' and select 'jxl.jar' from appropriate place and click ok, this is how you have to add 'jxl.jar' into your project.

7) Create .xls file and place it at an appropriate place so that you can use that in your program, below I'm attaching screenshot and this is how you need to enter values in excel file.





8) Once you are done with all above steps, then start writing program, below I'm pasting the code.

package GmailLoginExcel;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.FileInputStream;






// Please include these two
import jxl.Sheet;
import jxl.Workbook;

public class GmailLogin {
   
    public static void main(String[] args) {
        try{
           
                                                                           //eg: ("D:\\Gmail\\GmailLogin.xls")
            FileInputStream fi=new FileInputStream("Enter path of excel file");
            Workbook w=Workbook.getWorkbook(fi);
            Sheet s=w.getSheet(0);
           
            WebDriver driver = new FirefoxDriver();
            driver.get("https://www.gmail.com");
           
            driver.manage().window().maximize();
            //Wait
            WebDriverWait wait = new WebDriverWait(driver, 300);
           
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='Email']")));
           
          
            //Read data from excel sheet
            String s1 = s.getCell(0,0).getContents();
            String s2 = s.getCell(0,1).getContents();
           
         
           
           
            driver.findElement(By.xpath(".//*[@id='Email']")).sendKeys(s1);
           
           
            driver.findElement(By.xpath(".//*[@id='Passwd']")).sendKeys(s2);
           
           
            driver.findElement(By.xpath(".//*[@id='signIn']")).click();   
           
            Thread.sleep(19000);
           
            driver.findElement(By.xpath(".//*[@id='gb']/div[1]/div[1]/div[2]/div[5]/div[1]/a/span")).click();
           
            Thread.sleep(3000);
           
            driver.findElement(By.xpath(".//*[@id='gb_71']")).click();
           
            System.out.println("Success");
           
            Thread.sleep(3000);
           
            driver.close();
           
       
        }
        catch (Exception e)
        {
        }
        }

}

9) Above code will run perfectly, now modify above code according to your requirements..cheers..:)




2 comments:

  1. This could be one of the most useful blogs I have ever come across on this subject. I am a newbie in this topic that's why I appreciate and understand all your effort.
    Selenium Course in Chennai
    Selenium Courses in Chennai

    ReplyDelete