4/19/16

Selenium WebDriver Scripts

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class FirstTest {

     public static void main(String[] args) {
    
           WebDriver driver=new FirefoxDriver();  //Opens the Firefox browser
           //FirefoxDriver driver=new FirefoxDriver();
          
           driver.get("https://www.facebook.com/"); // opens the website
          
           System.out.println(driver.getTitle());   // get the title of the page
          
           System.out.println(driver.getCurrentUrl()); // get the URL of the page.
          
          
     }

}


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Testcase2 {

     public static void main(String[] args) {
          
           WebDriver driver=new FirefoxDriver(); // Invoke the FF browser
          
          driver.get("http://newtours.demoaut.com/"); // opens the URL
          
          driver.findElement(By.name("userName")).sendKeys("merry"); //Enter User name
           driver.findElement(By.name("password")).sendKeys("mer"); // Enter password
           driver.findElement(By.name("login")).click();// clicking on sign-in button
          
           //verification part
          
           String ActualTitle=driver.getTitle();
           String ExpectedTitle="Find a Flight: Mercury Tours:";
          
           if(ActualTitle.contentEquals(ExpectedTitle))
           {
                System.out.println("Login test passed");
           }
          
           else
           {
                System.out.println("Login Test failed");
           }
          
           driver.close();

     }

}

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class getCommandsEx {

     public static void main(String[] args) {
    
           WebDriver driver=new FirefoxDriver();
          
           driver.get("https://www.facebook.com/");
          
           driver.manage().window().maximize(); // maximise the browser window
          
           //System.out.println(driver.getCurrentUrl());
          
           //System.out.println(driver.getTitle());
          
           System.out.println(driver.getPageSource());
          
          

     }

}
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Closeandquitexample {

     public static void main(String[] args) {
          
     WebDriver driver=new FirefoxDriver();
          
          
           driver.get("http://www.popuptest.com/popuptest2.html");
          
           driver.manage().window().maximize();
          
           //driver.close(); // closes only currently focused window
          
           driver.quit();  // closes all open windows
          

     }

}
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class NavigateCommands {

     public static void main(String[] args) {

           WebDriver driver=new FirefoxDriver();
          
           driver.get("https://www.google.co.in/");
          
           driver.manage().window().maximize();
          
           driver.navigate().to("https://www.facebook.com/");
          
           driver.navigate().back();
          
           driver.navigate().forward();
     }

}

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;


public class HandlingWebElements {

     public static void main(String[] args)
     {
          
           WebDriver driver=new FirefoxDriver();
          
          driver.get("https://fs2.formsite.com/meherpavan/form1/index.html");
          
           driver.manage().window().maximize();
          
           //Text Box
           WebElement textbox=driver.findElement(By.xpath("//*[@id='RESULT_TextField-2']"));
          
           if(textbox.isDisplayed()
                      && textbox.isEnabled())
           {
                driver.findElement(By.xpath("//*[@id='RESULT_TextField-2']")).sendKeys("pavan");
           }
          
           //Drop down box
          
          
           Select drpdown=new Select(driver.findElement(By.id("RESULT_RadioButton-9")));
               
           //drpdown.selectByIndex(2);
          //drpdown.selectByValue("Radio-2");
           //drpdown.selectByVisibleText("Afternoon");
          
           List ls=drpdown.getOptions();   // return the options list
          
           System.out.println(ls.size());  // returns number of options/elements present in the box
          
           for(WebElement we:ls)
           {
                System.out.println(we.getText());
           }
          
          
          
     }

}

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class CheckBoxEx {

     public static void main(String[] args) {
          
           WebDriver driver=new FirefoxDriver();
          
          //driver.get("https://fs2.formsite.com/meherpavan/form1/index.html");
          
           driver.get("https://www.facebook.com/");
           driver.manage().window().maximize();
          
           /*driver.findElement(By.id("RESULT_CheckBox-12_0")).click(); // select the check box
           driver.findElement(By.id("RESULT_CheckBox-12_1")).click();
           driver.findElement(By.id("RESULT_CheckBox-12_5")).click();
           driver.findElement(By.id("RESULT_CheckBox-12_0")).click(); */
    
           WebElement we=driver.findElement(By.xpath("//*[@id='login_form']/table/tbody/tr[3]/td[1]/div/div/label"));
    
           System.out.println(we.getText());
          
           for(int i=1;i<5 span="">i++)
           {
          driver.findElement(By.xpath("//*[@id='persist_box']")).click();
           }
     }

}

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class RadioButtonsEx {

     public static void main(String[] args) {
          
           WebDriver driver=new FirefoxDriver();
          
          driver.get("https://fs2.formsite.com/meherpavan/form1/index.html");
          
           driver.manage().window().maximize();
          
           WebElement rad=driver.findElement(By.id("RESULT_RadioButton-10_0"));
          
           System.out.println(rad.isDisplayed()); // true/false
          
           System.out.println(rad.isSelected()); // true / false
    
           if(rad.isDisplayed() )
           {
                rad.click();
           }
               
          
          
     }

}

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;


public class SwichingFrames {

     public static void main(String[] args) throws InterruptedException {
    
           FirefoxDriver driver=new FirefoxDriver();
          
          driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS); // Implicit wait
          
          driver.get("http://seleniumhq.github.io/selenium/docs/api/java/index.html");
          
           driver.manage().window().maximize();
          
           driver.switchTo().frame("packageListFrame");// focus on 1 st frame
          driver.findElement(By.linkText("com.thoughtworks.selenium")).click();
          
           Thread.sleep(1000);
          
          
           driver.switchTo().defaultContent();
           driver.switchTo().frame("packageFrame");
          driver.findElement(By.linkText("BrowserConfigurationOptions")).click();
          
           driver.switchTo().defaultContent();
           driver.switchTo().frame("classFrame");
           //driver.findElement(By.linkText("Deprecated")).click();
          driver.findElement(By.xpath("html/body/div[1]/ul/li[5]/a")).click();
     }

}
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class HandlingPopups {

     public static void main(String[] args) {
          
           WebDriver driver=new FirefoxDriver();
          
           driver.get("http://output.jsbin.com/usidix/1");
          
           driver.manage().window().maximize();
          driver.findElement(By.cssSelector("input[type=button]")).click();
          
           String alert=driver.switchTo().alert().getText();
           System.out.println(alert);
          
           driver.switchTo().alert().accept();  // this will close popups
          
           driver.close();

     }

}

import java.io.IOException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class FileUpload {

     public static void main(String[] args) throws IOException {
          
          
           WebDriver driver=new FirefoxDriver();
          
          driver.get("https://fs2.formsite.com/meherpavan/form1/index.html");
          
           driver.manage().window().maximize();
          
           //driver.findElement(By.id("RESULT_FileUpload-14")).click();
          
           driver.findElement(By.id("RESULT_FileUpload-14")).sendKeys("C:\\Users\\pbhimava\\Desktop\\FormLink.txt");
          
          //Runtime.getRuntime().exec("C:\\Users\\pbhimava\\Desktop\\uploadscript.exe");
          
          
          
          driver.findElement(By.xpath("//*[@id='q21']/div/input[2]")).click();//clcik on upload button
          
     }

}

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class LinksEx {

     public static void main(String[] args) {
          
           WebDriver driver=new FirefoxDriver();
          
          driver.get("https://fs2.formsite.com/meherpavan/form1/index.html");
                     
           driver.manage().window().maximize();
          
          //driver.findElement(By.partialLinkText("Trainings")).click();
          
           driver.findElement(By.linkText("Online Trainings")).click();
     }

}

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class LinksExample {

     public static void main(String[] args) {
    
           FirefoxDriver driver=new FirefoxDriver();
           driver.get("http://newtours.demoaut.com/");
           driver.manage().window().maximize();
          
           List links=driver.findElements(By.tagName("a"));
          
           System.out.println(links.size());
          
           for(int i=0;i<links.size();i++)
           {
                System.out.println(links.get(i).getText());
           }
          
          

     }

}

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class LinksValidation {

     public static void main(String[] args) {
          
           FirefoxDriver driver=new FirefoxDriver();
           driver.get("http://newtours.demoaut.com/");
           driver.manage().window().maximize();
          
          
           List links=driver.findElements(By.tagName("a")); // capturing all the links
          
           String linkTexts[]=new String[links.size()];
          
          int i=0;
          
           for(WebElement e:links)
           {
                linkTexts[i]=e.getText(); // capture the text from web elements(links)
                i++;
           }
          
          
           for(String s: linkTexts)   // reading link text from the string array
           {
                //System.out.println(s);
               
                driver.findElement(By.linkText(s)).click();
               
                if (driver.getTitle().equals("Under Construction: Mercury Tours"))
                {
                      System.out.println(s+ " " + "Page is Under construction");
               
                }
               
                else
                {
                      System.out.println(s+" "+"Page is working");
                }
               
                driver.navigate().back(); // this will goto home page to validate other links
               
           }
               
          

     }

}

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;


public class ListBoxExample {

     public static void main(String[] args) {
          
           WebDriver driver=new FirefoxDriver();
          
           driver.get("http://output.jsbin.com/osebed/2");
          
           driver.manage().window().maximize();
          
           Select lb=new Select(driver.findElement(By.id("fruits")));
          
           lb.selectByIndex(1);
           lb.selectByIndex(3);
           lb.selectByValue("orange");
          
           List we= lb.getOptions();
          
           System.out.println(we.size());
          
           for(WebElement e: we)
           {
                System.out.println(e.getText()); // Print all the options in the list
           }
          
     }

}
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;


public class Calender {

     public static void main(String[] args) {
          
           WebDriver driver=new FirefoxDriver();
          
          driver.get("https://fs2.formsite.com/meherpavan/form1/index.html");
          
           driver.manage().window().maximize();
          
           //driver.findElement(By.id("RESULT_TextField-15")).sendKeys("04-01-2016");
          
          
          driver.findElement(By.xpath("//*[@id='q22']/img[1]")).click(); // click on the calender control
          
           Select year=new Select(driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div/div/select")));
           year.selectByVisibleText("2016");
          
          
           for(int i=4;i<12 span="">i++)
           {
           driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div/a[2]/span")).click();
          
           String m=driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div/div/span")).getText();
          
                if(m.equals("May"))
                {
                      driver.findElement(By.linkText("10")).click();
                      break;
                }
          
           }

}
}



import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;


public class Draganddrop {

     public static void main(String[] args) {
          
           WebDriver driver=new FirefoxDriver();
          
          driver.get("http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html");
          
           driver.manage().window().maximize();
          
          driver.manage().timeouts().implicitlyWait(1000,TimeUnit.MILLISECONDS);
          
          
           WebElement from=driver.findElement(By.xpath("//*[@id='box6']"));
          
           WebElement to=driver.findElement(By.xpath("//*[@id='box107']"));
          
          
           Actions act=new Actions(driver);
          
           //drag and drop
          act.clickAndHold(from).moveToElement(to).release(to).build().perform();
          
          
               
          
          
     }

}


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class WebTable1 {

     public static void main(String[] args) {
          
           WebDriver driver=new FirefoxDriver();
          
           driver.get("file:///C:/sample.html");
          
           //Hard coded
                //System.out.print(driver.findElement(By.xpath("html/body/table/tbody/tr[2]/td[1]")).getText());
                //System.out.println(driver.findElement(By.xpath("html/body/table/tbody/tr[2]/td[3]")).getText());
          
          
           int rows= driver.findElements(By.xpath("html/body/table/tbody/tr")).size();
          
           int cols=driver.findElements(By.xpath("html/body/table/tbody/tr/th")).size();
          
          

           for(int r=2;r<=rows;r++)  // row incrementation
           {
                for(int c=1;c<=cols;c++)  // columns incrementation
                {
               
                      System.out.print("  "+ driver.findElement(By.xpath("html/body/table/tbody/tr["+r+"]/td["+c+"]")).getText());
                     
                }
               
                System.out.println();
           }
          
          
     }

}
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class HRMTable {

     public static void main(String[] args) {
          
     WebDriver driver=new FirefoxDriver();
    
     driver.get("http://opensource.demo.orangehrm.com/index.php/auth/login");
    
     driver.manage().window().maximize();
    
     driver.manage().timeouts().implicitlyWait(100,TimeUnit.MILLISECONDS);
    
    
     //Login
     driver.findElement(By.id("txtUsername")).sendKeys("Admin");
     driver.findElement(By.id("txtPassword")).sendKeys("admin");
     driver.findElement(By.id("btnLogin")).click();
    
    
     //click on admin tab
          driver.findElement(By.xpath("//*[@id='menu_admin_viewAdminModule']/b")).click();
    
     // click on user management
          driver.findElement(By.xpath("//*[@id='menu_admin_UserManagement']")).click();
    
     // click on  users tab
          driver.findElement(By.xpath("//*[@id='menu_admin_viewSystemUsers']")).click();
    
     // no.f rows in webtable
          
           int rows= driver.findElements(By.xpath("//*[@id='resultTable']/tbody/tr")).size();
          
           System.out.println(rows);
          
           int count=0;
          
           for(int i=1;i<=rows;i++)
           {
               
                String status= driver.findElement(By.xpath("//*[@id='resultTable']/tbody/tr["+i+"]/td[5]")).getText();
               
                if(status.equals("Enabled"))
                {
                      count=count+1;
                }
                     
                    
           }
          
          
           System.out.println(" No. of users are enabled:"+count );
           System.out.println(" No. of users are NOT enabled:"+ (rows-count));
    
    
          //driver.findElement(By.xpath(".//*[@id='ohrmList_chkSelectRecord_67']")).click();
          
          driver.findElement(By.xpath("html/body/div[1]/div[3]/div[2]/div/div/form/div[4]/table/tbody/tr[2]/td[1]/input")).click();
          

          
          
          
     }

}

EMPID
FIRSTNAME
LASTNAME
SALARY
111
ABC
ABC
50000
112
PQR
PQR
70000
113
MNO
MNO
40000
114
XYZ
XYZ
60000
115
ABC1
ABC1
50000
116
PQR1
PQR1
70000
117
MNO1
MNO1
40000
118
XYZ1
XYZ1
60000
119
ABC2
ABC2
50000
120
PQR2
PQR2
70000
121
MNO2
MNO2
40000
122
XYZ2
XYZ2
60000



import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class ReadingExcel {

     public static void main(String[] args) throws IOException {
          
           FileInputStream file=new FileInputStream("C://data3.xlsx");
          
           XSSFWorkbook workbook=new XSSFWorkbook(file);
          
           XSSFSheet sheet=workbook.getSheetAt(0); // XSSFSheet sheet=workbook.getSheet("Sheet1");
    
    
           int rowcount=sheet.getLastRowNum(); // No. of rows present in the sheet
    
          
           XSSFRow row=sheet.getRow(0); // Focus on row
           int colcount=row.getLastCellNum(); // No. of columns present on first row
    
           for(int i=1;i<=rowcount;i++) // row incrementation
           {
               
                XSSFRow currentrow=sheet.getRow(i);
               
                for(int j=0;j<colcount;j++) // column incrementation
                     
                {
                      XSSFCell cell=currentrow.getCell(j); // creating cell object
                     
                      //String value=cell.toString(); // get the value from the cell
                      //System.out.print(value);
                     
                      switch(cell.getCellType())
                     
                      {
                      case XSSFCell.CELL_TYPE_STRING:
                                System.out.println(cell.getStringCellValue());break;
                      case XSSFCell.CELL_TYPE_NUMERIC:
                           System.out.println((int)cell.getNumericCellValue());break;
                          
                     case XSSFCell.CELL_TYPE_BOOLEAN:
                           System.out.println(cell.getBooleanCellValue());break;
                      default: System.out.println("null");
                      }
                     

abc
abc
abc
abc
abc
abc
abc
abc
abc
abc
abc
abc
abc
abc
abc
abc
abc
abc
abc
abc
abc
abc
abc
abc


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class WritingExcel {

     public static void main(String[] args) throws IOException {
          
           FileOutputStream file=new FileOutputStream("C://Test1.xlsx");
          
          
           XSSFWorkbook wb=new XSSFWorkbook();
           XSSFSheet sh= wb.createSheet("MySheet");
          
          
           for(int i=0;i<=5;i++)
           {
                XSSFRow currentrow=sh.createRow(i);
               
                for(int j=0;j<=3;j++)
                {
                      XSSFCell cell=currentrow.createCell(j);
                      cell.setCellValue("abc");
                }
           }
          
           wb.write(file);
          
           file.close();

     }

}

UserName
Password
Results
mercury
mercury

mercury
mercury

mercury
asdasdA

mnopqr
asdasdA

mercury2
DFASFDAS

stopuv
mercury

mercury2
FADSF


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.apache.poi.xssf.usermodel.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class LoginDDT {

     public static void main(String[] args) throws IOException {
          
           WebDriver driver=new FirefoxDriver();
           driver.get("http://newtours.demoaut.com/");
          
           driver.manage().timeouts().implicitlyWait(1000, TimeUnit.MILLISECONDS);
          
           //File Input Stream
          
           FileInputStream file1=new FileInputStream("C:\\Login.xlsx");
           XSSFWorkbook workbook1=new XSSFWorkbook(file1);
           XSSFSheet sheet1=workbook1.getSheet("Sheet1");
          
          
           // File Output Stream
          
           FileOutputStream file2=new FileOutputStream("C:\\Login.xlsx");
           XSSFWorkbook workbook2=new XSSFWorkbook();
           XSSFSheet Sheet2=workbook2.createSheet("Sheet1");
          
           XSSFRow header=Sheet2.createRow(0);
          
           XSSFCell uname=header.createCell(0);
           uname.setCellValue("UserName");
          
           XSSFCell pwd=header.createCell(1);
           pwd.setCellValue("Password");
          
           XSSFCell res=header.createCell(2);
           res.setCellValue("Results");
          
                     
          
           //Reading Excel sheet columns and passing to application
           int rownum=sheet1.getLastRowNum();  // NO. OF ROWS
          
           for(int i=1;i<=rownum;i++)
           {
                XSSFRow currentrow=sheet1.getRow(i);
               
                XSSFCell cell1=currentrow.getCell(0);
                String user=cell1.getStringCellValue();
               
                XSSFCell cell2=currentrow.getCell(1);
                String pass=cell2.getStringCellValue();
               
               
                driver.findElement(By.name("userName")).sendKeys(user);
                driver.findElement(By.name("password")).sendKeys(pass);
                driver.findElement(By.name("login")).click();
               
                XSSFRow currentrow2=Sheet2.createRow(i);
               
                XSSFCell usercell=currentrow2.createCell(0);
                usercell.setCellValue(user);
               
                XSSFCell usercel2=currentrow2.createCell(1);
                usercel2.setCellValue(pass);
                          
                XSSFCell rescell=currentrow2.createCell(2);
               
                     
               
                if(driver.getTitle().equals("Find a Flight: Mercury Tours:"))
                {
                     
                      //System.out.println(user +" "+ "Login is successfull");
                     
                      rescell.setCellValue("Passed");
                      driver.navigate().back();
                     
                }
               
                else
                {
                      ///System.out.println(user+" "+ "Login Failed");
                      rescell.setCellValue("Failed");
                     driver.findElement(By.linkText("Home")).click();
                     
                }
                     
               
           }
          
          
           workbook2.write(file2);
           file1.close();
           file2.close();
          
           driver.close();
               
          
           }
          
Principle
Rate of Interest
Period(Years)
Frequency
Maturity Value
2000
10
2
Simple Interest
2400
4000
15
5
Simple Interest
8352
50000
10
3
Simple Interest
65000
75000
6
2
Simple Interest
84000

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
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.openqa.selenium.support.ui.Select;


public class CalculationDDT {

     public static void main(String[] args) throws IOException {
          
           WebDriver driver=new FirefoxDriver();
          
           driver.get("http://www.moneycontrol.com/fixed-income/calculator/state-bank-of-india-sbi/fixed-deposit-calculator-SBI-BSB001.html");
          
          
           driver.manage().window().maximize();
          
          driver.manage().timeouts().implicitlyWait(1000,TimeUnit.MILLISECONDS);
          
           FileInputStream file=new FileInputStream("C://caldata.xlsx");
           XSSFWorkbook workbook=new XSSFWorkbook(file);
           XSSFSheet sheet=workbook.getSheet("Sheet1");
          
           int rowcount=sheet.getLastRowNum(); // no of rows from xl sheet
          
           for(int i=1;i<=rowcount;i++)
           {
               
                XSSFRow currentrow=sheet.getRow(i);
               
               
                XSSFCell prince=currentrow.getCell(0);
                int princ=(int)prince.getNumericCellValue(); // captured priciple amount
               
                XSSFCell roi=currentrow.getCell(1);
                int rateofint=(int)roi.getNumericCellValue(); // captured rate of intrest
               
                XSSFCell period=currentrow.getCell(2);
                int per=(int)period.getNumericCellValue(); //captured period
               
               
                XSSFCell Frequency=currentrow.getCell(3);
                String fre=Frequency.getStringCellValue(); // captured frequency
               
               
                XSSFCell matcell=currentrow.getCell(4);
                int exp_mat=(int)matcell.getNumericCellValue(); // matirity value
               
                //passing values to application
               
                driver.findElement(By.xpath(".//*[@id='principal']")).sendKeys(String.valueOf(princ));
                driver.findElement(By.xpath(".//*[@id='interest']")).sendKeys(String.valueOf(rateofint));
                driver.findElement(By.xpath(".//*[@id='tenure']")).sendKeys(String.valueOf(per));
               
                Select periodcombo=new Select(driver.findElement(By.xpath(".//*[@id='tenurePeriod']")));
                periodcombo.selectByVisibleText("year(s)");
               
               
                Select frecombo=new Select(driver.findElement(By.name("frequency")));
                frecombo.selectByVisibleText(fre);
               
               
                driver.findElement(By.xpath(".//*[@id='fdMatVal']/div[2]/a[1]/img")).click();    
               
               
                String act_mat=driver.findElement(By.xpath(".//*[@id='resp_matval']/strong")).getText();// capturing the actual result from the application
               
                if(exp_mat==Double.parseDouble(act_mat))
                     
                {
                      System.out.println(" Test is passed");
                     
                }
               
                else
                {
                      System.out.println(" Test is failed");
                }
                          
                driver.findElement(By.xpath(".//*[@id='fdMatVal']/div[2]/a[2]/img")).click();// clearing all the values
               
               
           }
          
          

     }

}

Fist_Name
Last_Name
Phone
Email
Address
City
State
PostelCode
Country
UserName
Password
AAAAA
aaaaa
1234567890
HYDERABAD
HYDERABAD
AP
500073
INDIA
aaaaa
aaaaa
BBBBB
bbbbb
1234567891
CHENNAI
CHENNAI
TN
500074
INDIA
bbbbb
bbbbb
CCCCC
ccccc
1234567892
BANGOLORE
BANGOLORE
KR
500075
INDIA
ccccc
ccccc
DDDDD
ddddd
1234567890
DELHI
DELHI
UP
500071
INDIA
ddddd
ddddd
EEEEE
eeeee
1234567890
MUMBAI
MUMBAI
MH
500079
INDIA
eeeee
eeeee
XXXXXX
xxxxxxx
1234567891
CHENNAI
CHENNAI
TN
500080
INDIA
xxxxxxx
xxxxxxx
YYYYYY
yyyyyy
1234567892
BANGOLORE
BANGOLORE
KR
500081
INDIA
yyyyyy
yyyyyy
ZZZZZZZ
zzzzzzz
1234567893
DELHI
DELHI
UP
500082
INDIA
zzzzzzz
zzzzzzz

/*--------------------------------------------------------------
Data Driven Test for Flight Registration using Excel File
----------------------------------------------------------------*/
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class ExelDataDrivenTestRegistration {

     public static void main(String[] args) throws IOException {

           WebDriver driver=new FirefoxDriver();
          
           driver.get("http://newtours.demoaut.com/");
          
           //Getting Data from EXCEL Sheet
          
           FileInputStream file = new FileInputStream("C://Registration.xlsx");  
    
           //Get the workbook instance for XLS file
           XSSFWorkbook workbook = new XSSFWorkbook(file);
          
           //Get first sheet from the workbook
           XSSFSheet sheet = workbook.getSheet("Sheet1");
          
          
           //Get first row from the sheet
           int noOfRows = sheet.getLastRowNum();
          
           System.out.println("No. of Records in the Excel Sheet:"+ noOfRows);
          
           for(int row =1; row<= noOfRows; row++)
                      {
                                                           
                           XSSFRow current_row = sheet.getRow(row);
                     
                           XSSFCell fname = current_row.getCell(0);
                           String First_name = fname.getStringCellValue();
                                                      
                           //System.out.println(First_name);
                          
                           XSSFCell lname = current_row.getCell(1);
                           String Last_name = lname.getStringCellValue();
                           //System.out.println(Last_name);
                          
                           XSSFCell ph = current_row.getCell(2);
                           String Phone = ph.getStringCellValue();
                                                      
                           XSSFCell mail = current_row.getCell(3);
                           String Email = mail.getStringCellValue();
                          
                           XSSFCell add = current_row.getCell(4);
                           String Address = add.getStringCellValue();
                          
                           XSSFCell city = current_row.getCell(5);
                           String City = city.getStringCellValue();
                          
                           XSSFCell st = current_row.getCell(6);
                           String State = st.getStringCellValue();
                          
                           XSSFCell pin = current_row.getCell(7);
                           String PinCode = pin.getStringCellValue();
                          
                           XSSFCell ctry= current_row.getCell(8);
                           String Country = ctry.getStringCellValue();
                          
                           XSSFCell uname = current_row.getCell(9);
                           String UserName = uname.getStringCellValue();
                          
                           XSSFCell passwrd = current_row.getCell(10);
                           String Password = passwrd.getStringCellValue();
                          
                          
                           //Registration Process
                          
                                           
                           driver.findElement(By.linkText("REGISTER")).click();
                          
                          
                           // Entering Contact information
                           driver.findElement(By.name("firstName")).sendKeys(First_name);
                           driver.findElement(By.name("lastName")).sendKeys(Last_name);
                           driver.findElement(By.name("phone")).sendKeys(Phone);
                           driver.findElement(By.name("userName")).sendKeys(Email); // email id
                          
                           //Entering mailing information
                          
                           driver.findElement(By.name("address1")).sendKeys(Address);
                           driver.findElement(By.name("address2")).sendKeys(Address);
                           driver.findElement(By.name("city")).sendKeys(City);
                           driver.findElement(By.name("state")).sendKeys(State);
                           driver.findElement(By.name("postalCode")).sendKeys(PinCode);
                          
                           //Country selection
                           Select dropcountry=new Select(driver.findElement(By.name("country")));
                           dropcountry.selectByVisibleText(Country);
                          
                          
                           //Entering User information
                          
                           driver.findElement(By.name("email")).sendKeys(UserName); // user name
                           driver.findElement(By.name("password")).sendKeys(Password);
                           driver.findElement(By.name("confirmPassword")).sendKeys(Password);
                          
                           //Submitting form
                          
                           driver.findElement(By.name("register")).click();
                          
                                                                     
                             if(driver.getPageSource().contains("Thank you for registering"))
                             {
                                   System.out.println(" Registration Completed for  " + row+ " record");
                          
                                                                                                                                                                        
                              }
                            
                             else
                            
                             {
                                   System.out.println(" Registration Failed for " + row+ " record");
                                  
                                  // driver.close();
                              }

                            
                           }
                     
                     
                      System.out.println(" Data Driven Test Completed");
                     
                      driver.close();
                      driver.quit();
                     
                      file.close();
                     
                      }
                     
    
                      }   

PAGE OBJECT MODEL
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.Select;

public class RegistrationPage
{
  
     WebDriver driver;
    
     By RegLink=By.linkText("REGISTER");
     By FirstName=By.name("firstName");
     By LastName=By.name("lastName");
     By phone=By.name("phone");
     By email=By.name("userName");
     By address1=By.name("address1");
     By address2=By.name("address2");
     By city=By.name("city");
     By state=By.name("state");
     By postalCode=By.name("postalCode");
     By country=By.name("country");
     By username=By.name("email");
     By password=By.name("password");
     By confirmpassword=By.name("confirmPassword");
     By registerBtn=By.name("register");
    
    
    
     RegistrationPage(WebDriver d)
     {
           //this.driver=driver;
           driver=d;
     }
    
     public void clickRegLink()
     {
           driver.findElement(RegLink).click();
     }
    
    
    
     public void setFirstName(String fname)
     {
     driver.findElement(FirstName).sendKeys(fname);  
     }
    
     public void setLastName(String lname)
     {
           driver.findElement(LastName).sendKeys(lname);
          
     }
    
     public void setPhone(String ph)
     {
           driver.findElement(phone).sendKeys(ph);
     }
    
     public void setemail(String mail)
     {
           driver.findElement(email).sendKeys(mail);
     }
    
     public void setAddress1(String addr1)
     {
           driver.findElement(address1).sendKeys(addr1);
     }
    
     public void setAddress2(String addr2)
     {
           driver.findElement(address2).sendKeys(addr2);
     }
    
     public void setCity(String ci)
     {
           driver.findElement(city).sendKeys(ci);
     }
    
     public void setState(String st)
     {
           driver.findElement(state).sendKeys(st);
     }
    
     public void setPostalCode(String pcode)
     {
           driver.findElement(postalCode).sendKeys(pcode);
     }
    
     public void setCountry(String cn)
     {
          
           WebElement con=driver.findElement(country);
           Select drop=new Select(con);
           drop.selectByVisibleText(cn);
     }
    
     public void setUserName(String uname)
     {
           driver.findElement(username).sendKeys(uname);
     }
    
     public void setPassword(String passwrd)
     {
           driver.findElement(password).sendKeys(passwrd);
     }
    
    
     public void setConfirmedPassword(String passwrd)
     {
           driver.findElement(confirmpassword).sendKeys(passwrd);
     }
    
     public void clickRegBtn()
     {
           driver.findElement(registerBtn).click();
     }
    
          
}

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class RegistationTest {
    
     public static void main(String[] args) {
             
           WebDriver driver = new FirefoxDriver();
           RegistrationPage RGPage=new RegistrationPage(driver);
          
           driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
         driver.get("http://newtours.demoaut.com/");

         RGPage.clickRegLink();
         RGPage.setFirstName("pavan");
         RGPage.setLastName("kumar");
         RGPage.setPhone("8142402254");
         RGPage.setemail("pavan@gmail.com");
         RGPage.setAddress1("Nizampet");
         RGPage.setAddress2("Kukatpally");     
         RGPage.setCity("HYDERABAD");
         RGPage.setState("AP");
         RGPage.setPostalCode("500074");

         RGPage.setCountry("INDIA");
        
         RGPage.setUserName("PAVAN");
         RGPage.setPassword("pavan");
         RGPage.setConfirmedPassword("pavan");
        
        RGPage.clickRegBtn();
         
           //Validation
               
                  if(driver.getPageSource().contains("Thank you for registering"))
                  {
                      System.out.println("Your registered successfully- TEST PASSED");
                   }
                 
                  else
                 
                  {
                        System.out.println("Your registration failed - TEST FAILED");
                   }

     }
          
}