10/17/15

WebDriver Scripts

1)   WebDriver First Script
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;    

public class myClass {

public static void main(String[] args)

{
     // declaration and instantiation of objects/variables

            WebDriver driver;
            driver=new FirefoxDriver();
           
              //WebDriver driver = new FirefoxDriver();

       String baseUrl = "http://newtours.demoaut.com";
      
       // launch Fire fox and direct it to the Base URL

       driver.get(baseUrl);

       String expectedTitle = "Welcome: Mercury Tours";
         
       String actualTitle = "";
 
        // get the actual value of the title

        actualTitle = driver.getTitle();


        /* * compare the actual title of the page with  the expected one and print

        * the result as "Passed" or "Failed"

        */

       if (actualTitle.contentEquals(expectedTitle))
       {

            System.out.println("Test Passed!");

       }
       else
       {

           System.out.println("Test Failed");

       }

       //close Fire fox

       driver.close();

       driver.quit();
}    

}

2)   Locating Elements/Objects

/*
 * 1. Open the browser
 * 2. Enter user name
 * 3. Enter password
 * 4. Click on sign in
 */

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

public class locatingobjects {

            public static void main(String[] args) {
                       
                        WebDriver driver=new FirefoxDriver();
                       
                        driver.get("http://newtours.demoaut.com/");  // Invoke the browser and open the URL
                                   
                        driver.findElement(By.name("userName")).sendKeys("mercury"); // Enter user name
                        driver.findElement(By.name("password")).sendKeys("dsafasfdas"); // Enter password
                        driver.findElement(By.name("login")).click();
                       
                        // Checking the successfull login
                        String expectedtitle="Find a Flight: Mercury Tours:";
                        String actualtitle=driver.getTitle();
                       
                        if (actualtitle.contentEquals(expectedtitle))
                                    System.out.println("Login successfull");
                        else
                                    System.out.println("Login Un successfull");
                       
                        driver.close(); // closing the browser
            }

}

3)   Get Commands

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

public class getcommandsEx {

            public static void main(String[] args) {
                       
                        WebDriver driver=new FirefoxDriver();  // Web Driver object creation
                       
                        driver.get("https://www.facebook.com/");  // invoke the browser and open the URL
                        System.out.println(driver.getTitle());   // Returns title of the page
                        System.out.println(driver.getCurrentUrl());
                        System.out.println(driver.getPageSource());
                        driver.close();  // close the browser
            }

}


4)   Close() and quit() methods

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

public class CloseandquiteEx {

            public static void main(String[] args)
           
           
            {
                        WebDriver driver=new FirefoxDriver();
                       
                        driver.get("http://www.popuptest.com/popuptest2.html");
                       
                        driver.close();
                        driver.quit();
            }

5)   Navigation Commands

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

public class NavigateCommandsEx {

            public static void main(String[] args)
           
            {
                       
     WebDriver driver=new FirefoxDriver();
    
     driver.get("https://www.google.co.in/");   // get() method
     System.out.println(driver.getCurrentUrl());
    
        
     driver.navigate().to("https://www.facebook.com/");   // navigate().to() method
     System.out.println(driver.getCurrentUrl());
    
    
     driver.navigate().back();    //navigate.back() method
     System.out.println(driver.getCurrentUrl());
    
     
     driver.navigate().forward();     // navigate.forward() method
     System.out.println(driver.getCurrentUrl());   
    
                       
}

}

6)   Switching Popups

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

public class swichbetweenPopups {

            public static void main(String[] args) {
                       
                        String alertmsg;
                       
                        WebDriver driver=new FirefoxDriver();
                       
                        driver.get("http://jsbin.com/usidix/1");
                       
                        //driver.findElement(By.cssSelector("input[type=button]")).click(); // click the Go button\\
                       
                        driver.findElement(By.xpath("html/body/input")).click();
                       
                                                alertmsg= driver.switchTo().alert().getText();  // this will read message from altert msg.
                       
                                                System.out.println(alertmsg);
                                               
                               driver.switchTo().alert().accept();

            }

}


7)   Switching Frames

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

public class switchingFrames {

            public static void main(String[] args) {

                        WebDriver driver = new FirefoxDriver();
                       
                        driver.get("http://selenium.googlecode.com/svn/trunk/docs/api/java/index.html");
                       
                        driver.switchTo().frame("classFrame");
                       
                        driver.findElement(By.linkText("Deprecated")).click();
                                   
                        driver.close();
                       
}

}


8)   Switching Windows

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class switchingwindows {

            public static void main(String[] args) {
                       
                        WebDriver driver=new FirefoxDriver();
                        driver.get("https://www.facebook.com/");
                        driver.manage().window().maximize();
                       
            }
}
9)   Counting Web Page 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;

public class pagelinks {

            public static void main(String[] args) {

                        WebDriver driver = new FirefoxDriver();

                        driver.get("http://newtours.demoaut.com/");

                        List links = driver.findElements(By.tagName("a"));

                        System.out.println(links.size());
                       
                        for (int i = 0; i<links.size(); i=i+1)

                        {

                                    System.out.println(links.get(i).getText());

                        }

            }

}


10)         Wait methods

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class waitExample
{
            public static void main(String args[]) throws InterruptedException
            {
                        WebDriver driver=new FirefoxDriver();
                       
                        //Implicit wait
                        driver.manage().timeouts().implicitlyWait(1000,TimeUnit.MILLISECONDS);
                       
                        driver.get("http://opensource.demo.orangehrm.com/");
                       
                        driver.findElement(By.xpath(".//*[@id='txtUsername']")).sendKeys("Admin");
                        driver.findElement(By.xpath(".//*[@id='txtPassword']")).sendKeys("admin");
                        driver.findElement(By.xpath(".//*[@id='btnLogin']")).click();
                       
              //Thread.sleep(10000);
                       
                        WebDriverWait mywait=new WebDriverWait(driver,1000);
                       
                        mywait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='dashboard-quick-launch-panel-menu_holder']/table/tbody/tr/td[1]/div/a/img")));
                       
                        driver.findElement(By.xpath(".//*[@id='dashboard-quick-launch-panel-menu_holder']/table/tbody/tr/td[1]/div/a/img")).click();
                       
                        mywait.until(ExpectedConditions.titleContains("OrangeHRM"));
                       
                                   
            }
            }


11)         Implicitwait

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


public class implicitwait
   {

            public static void main(String[] args)
           
            {
                       
                       
                        WebDriver driver=new FirefoxDriver();

                        driver.manage().timeouts().implicitlyWait(1000,TimeUnit.MILLISECONDS); // used for waiting
                       
                                                           
                        driver.get("https://www.facebook.com/");
                       
                                               
                                    driver.quit();
                        }

}

12)         Explicit wait

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;


public class explicitWaitEx {

            public static void main(String[] args) {
                        // TODO Auto-generated method stub
                       
                        WebDriver driver=new FirefoxDriver();
                       
                        WebDriverWait myWaitVar=new WebDriverWait(driver,20);  // For Explicit wait
                       
                        driver.get("http://newtours.demoaut.com/");
                       
                        myWaitVar.until(ExpectedConditions.visibilityOfElementLocated(By.name("userName")));
                                   
                        driver.findElement(By.name("userName")).sendKeys("mercury");
                                               
                        driver.quit();
                        }

}

13)         Conditional Methods
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class conditionalMethodsEx {

            public static void main(String[] args) {
                       
                        WebDriver driver=new FirefoxDriver();
                       
                        driver.get("http://newtours.demoaut.com/");

           
            WebElement txt_user=driver.findElement(By.name("userName"));
            WebElement txt_pass=driver.findElement(By.name("password"));
            WebElement btn_sign=driver.findElement(By.name("login"));
           
           
            //if (driver.findElement(By.name("userName")).isDisplayed() && driver.findElement(By.name("userName")).isEnabled()  )
           
            if (txt_user.isDisplayed() && txt_user.isEnabled())
            {
                        txt_user.sendKeys("mercury");
                       
            }
                       
            if (txt_pass.isDisplayed() && txt_pass.isEnabled())
            {
                        txt_pass.sendKeys("mercury");
                       
            }
           
            if (btn_sign.isDisplayed())
            {
           
            btn_sign.click();
                       
            }
           
            WebElement radio_oneway=driver.findElement(By.cssSelector("input[value=oneway]"));
            WebElement radio_twoway=driver.findElement(By.cssSelector("input[value=roundtrip]"));
           
            if (radio_oneway.isSelected())
                       
                        {
                        radio_twoway.click();
                        }
           
            if (radio_twoway.isSelected())
            {
                        radio_oneway.click();
            }
                                   
                       

            }

}

14)         CheckBox
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class checkboxtest
{

            public static void main(String[] args)
            {
                       
                        WebDriver driver=new FirefoxDriver();
                       
                        driver.get("https://www.facebook.com/");
                       
            WebElement checkbox=driver.findElement(By.id("persist_box"));
                       
                        for (int i=0;i<=5;i++)
                        {
                                    checkbox.click();  // same method we can it for select and unselect
                                    //Boolean status=checkbox.isSelected();
                                    System.out.println(checkbox.isSelected());
                        }

            }
           
}

15)         Links & Dropdown box
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class LinksAndDropDownEx
{

            public static void main(String[] args)
            {
                       
                       
                        WebDriver driver=new FirefoxDriver();
                       
                        driver.get("http://newtours.demoaut.com/");
                       
                        driver.findElement(By.linkText("REGISTER")).click();
                       
                                               
                        // For Select class we need to import special package
                       
                        WebElement drcontry=driver.findElement(By.name("country"));
                       
                       
                        Select dropcountry=new Select(drcontry);
                       
                        //dropcountry.selectByVisibleText("INDIA");               
                        //dropcountry.selectByIndex(2);
                        dropcountry.selectByValue("10");
                                   
                       
            }

}

16)         Multiple Select
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 MultipleSELECT {

            public static void main(String[] args) {
                        // TODO Auto-generated method stub
                       
                        WebDriver driver=new FirefoxDriver();
                       
                        driver.get("http://jsbin.com/osebed/2");
                       
                        Select fruits=new Select(driver.findElement(By.id("fruits")));
                       
                        //fruits.selectByVisibleText("Apple");
                        fruits.selectByVisibleText("Banana");
                        //fruits.selectByVisibleText("Grape");
                       
                       
                        //fruits.selectByIndex(2);
                        //fruits.selectByValue("grape");
                       

}
}

17)         Web Links
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class AccessingLinksEx {

            public static void main(String[] args)
           
            {
           
                        //String baseUrl="file:///C:/ExactMatch.html";
                        String baseUrl="file:///C:/PartialMatch.html";


                        WebDriver driver=new FirefoxDriver();

                        driver.get(baseUrl);
                       
                        //Exact Match
                                    //driver.findElement(By.linkText("click here")).click();
                                    //driver.findElement(By.xpath("html/body/a[2]")).click();
                                    //System.out.println("Title of the page is :" + driver.getTitle());
                       
                       
                        //Partial Match
                                    driver.findElement(By.partialLinkText("click")).click();
                                    System.out.println("Title of the page is :" + driver.getTitle());
                                               
                       
                        //driver.quit();
            }

}


18)         Web Links (Case Sensitivity)

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

public class caseSensitivityEx {

            public static void main(String[] args) {
           
     String baseUrl="http://newtours.demoaut.com/mercurywelcome.php";

   WebDriver driver=new FirefoxDriver();

                        driver.get(baseUrl);
                       
                                   
                        //Partial Match for Case sensitive Links
                       
                                    String linkText1= driver.findElement(By.partialLinkText("egis")).getText();
                                   
                                    System.out.println(linkText1);
                                   
                                    String linkText2= driver.findElement(By.partialLinkText("EGIS")).getText();
                                   
                                    System.out.println(linkText2);
           
                       
                        driver.quit();
            }

}


19)         Checking Web Links
import java.util.List;
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;

public class flightlinks {

            public static void main(String[] args) {
                       
                        WebDriver driver =new FirefoxDriver();
                        driver.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
                       
                        driver.get("http://newtours.demoaut.com/");
                       
                        List linkElements= driver.findElements(By.tagName("a"));
                        String links[]=new String[linkElements.size()];
                       
                       
                        // Extracting Links and storing into an array
                        for (int i=0; i<linkElements.size();i++)
                        {
                                    links[i]=linkElements.get(i).getText();
                        }
                                   
                       
                        //Reading links from an array and clicking them
                        for( int i=0; i<links.length;i++)
                        {
                                                           
                                    driver.findElement(By.linkText(links[i])).click(); 
                                   
                                    if ( driver.getTitle().equals("Under Construction: Mercury Tours"))
                                    {
                                                System.out.println(links[i]+"  "+"Link is under construction");
                                               
                                    }
                                   
                                    else
                                    {
                                                System.out.println(links[i]+"  "+"Link is working");
                                               
                                    }
                       
                         driver.navigate().back();
                         
                        }
                       
                                                driver.quit();
            }

}

20)         Web Table-1





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:/Table1.html");
           
                       
                        int rowCount=driver.findElements(By.xpath("html/body/table/tbody/tr")).size();// Row count
                        System.out.println(rowCount);
                       
                        int columnCount=driver.findElements(By.xpath("html/body/table/tbody/tr/th")).size();  // Column count
                        System.out.println(columnCount);
                       
            //Reading data from Web Table                               
            for (int r=2;r<=3;r++)
                        {
                                    for ( int c=1; c<=3;c++)
                                    {
                                                System.out.println(driver.findElement(By.xpath("html/body/table/tbody/tr["+r+"]/td["+c+"]")).getText());
                                    }
                        }
                                   
            }
}

21)         Web Table-2





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

public class webtable2 {

            public static void main(String[] args) {
                       
                        WebDriver driver=new FirefoxDriver();
                        driver.get("file:///C:/sample.html");
           
                       
                        int rowCount=driver.findElements(By.xpath("html/body/table/tbody/tr")).size();// Row count
                        System.out.println(rowCount);
                       
                        int columnCount=driver.findElements(By.xpath("html/body/table/tbody/tr/th")).size();  // Column count
                        System.out.println(columnCount);
                       
            //Reading data from Web Table                               
            for (int r=2;r<=5;r++)
                        {
                                    for ( int c=1; c<=3;c++)
                                    {
                                                System.out.println(driver.findElement(By.xpath("html/body/table/tbody/tr["+r+"]/td["+c+"]")).getText());
                                    }
                        }
                                   
            }
}

22)         Web Table Test (Orange HRM)
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WebTableHR {

            public static void main(String[] args) {
                       
                        WebDriver driver=new FirefoxDriver();
                        //driver.manage().timeouts().implicitlyWait(1000,TimeUnit.MILLISECONDS);
                        driver.get("http://opensource.demo.orangehrm.com/");
                       
                        //Login to OrangeHRM
                        driver.findElement(By.id("txtUsername")).sendKeys("Admin");
                        driver.findElement(By.id("txtPassword")).sendKeys("admin");
                        driver.findElement(By.id("btnLogin")).click();
                       
                        //Admin--> User management--> Users
                       
                        driver.findElement(By.id("menu_admin_viewAdminModule")).click();
                        driver.findElement(By.id("menu_admin_UserManagement")).click();
                        driver.findElement(By.id("menu_admin_viewSystemUsers")).click();
                       
                        //int rowCount=driver.findElements(By.xpath("html/body/div[1]/div[3]/div[2]/div/div/form/div[4]/table/tbody/tr")).size();// Row count
                        int rowCount=driver.findElements(By.xpath(".//*[@id='resultTable']/tbody/tr")).size();// Row count
                       
                       
                        System.out.println(rowCount);
                       
                        int statuscount=0;
                       
                        for( int i=1;i<=rowCount;i++)
                        {
                                    String status=driver.findElement(By.xpath(".//*[@id='resultTable']/tbody/tr["+i+"]/td[5]")).getText();
                                   
                                    if (status.equals("Enabled"))
                                    {
                                                statuscount=statuscount+1;
                                               
                                    }
                        }
                       
                        System.out.println("No. of users enabled"+ statuscount);
                        }
                       
            }


23)         Excel Test-1 : Reading columns from Excel sheet





import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;

import org.apache.poi.xssf.usermodel.*;

public class Exceltest1 {

            public static void main(String[] args) throws IOException {
                       
                        FileInputStream file=new FileInputStream("C://data1.xlsx"); // creating file object
                       
                        XSSFWorkbook workbook=new XSSFWorkbook(file);   // creating workbook object
                       
                        XSSFSheet sheet=workbook.getSheetAt(0);   // creating sheet object
                       
                        XSSFRow row=sheet.getRow(0);  // creating row object
                       
                        // focussing on cells/columns
                        XSSFCell cella1=row.getCell(0);     
                        XSSFCell cellb1=row.getCell(1);
                        XSSFCell cellc1=row.getCell(2);
                        XSSFCell celld1=row.getCell(3);
                        XSSFCell celle1=row.getCell(4);
                       
                        //Extracting data from cells
                        double a=cella1.getNumericCellValue();
                        int b= (int)cellb1.getNumericCellValue();
                        String c=cellc1.getStringCellValue();
                        Date d=celld1.getDateCellValue();
                        Boolean e=celle1.getBooleanCellValue();
                       
                       
                        System.out.println(a);
                        System.out.println(b);
                        System.out.println(c);
                        System.out.println(d);
                        System.out.println(e);
                       
            }

24)         Excel Test-2 : Reading rows from Excel sheet


import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.*;

public class Exceltest2 {

            public static void main(String[] args) throws IOException {
                       
                        FileInputStream file=new FileInputStream("C://data2.xlsx");
                       
                        XSSFWorkbook workbook=new XSSFWorkbook(file);
                       
                        XSSFSheet sheet=workbook.getSheetAt(0);
                       
                        int rowcount=sheet.getLastRowNum();     // counting number of rows present in the sheet
                       
                        for( int i=0;i<=rowcount;i++)
                                   
                        {
                                    XSSFRow row=sheet.getRow(i);
                                    XSSFCell cell=row.getCell(0);
                                    String s=cell.toString();   // for reading multiple data types from Excel sheet
                                    System.out.println(s);
                                                           
                        }

            }

}


25)         Excel Test-3 : Reading rows & columns from Excel sheet




import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.*;

public class ExcelTest3 {

            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);
                       
                        int rowcount=sheet.getLastRowNum();
                                    System.out.println(rowcount);
                       
                        XSSFRow row=sheet.getRow(0);
                        int colcount=row.getLastCellNum();
                                    System.out.println(colcount);
                                   
                        for(int i=1;i<=rowcount;i++)
                        {
                                    XSSFRow r=sheet.getRow(i);
                                   
                                    for(int j=0;j<colcount;j++)
                                    {
                                               
                                                XSSFCell col=r.getCell(j);
                                                                                                                                   
                                                //System.out.println(col.getCellType());
                                                //String value=col.toString();
                                                //System.out.println(value);
                                               
                                                switch (col.getCellType()) // Method will return the cell type
                                                {
                                               
                                                case XSSFCell.CELL_TYPE_STRING:
                                                            System.out.println(col.getStringCellValue());break;
                                                case XSSFCell.CELL_TYPE_BOOLEAN:
                                                            System.out.println(col.getBooleanCellValue());break;
                                                case XSSFCell.CELL_TYPE_NUMERIC:
                                                            System.out.println((int)col.getNumericCellValue());break;
                                                default:System.out.println("null");
                                                }
                                               
                                               
                                    }
                                                                       
                        }
                                   
           
            }

}

26)         Excel Test-4 : Data Driven Test for Register user on Flight Reservation.





/*----------------------------------------------------------
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.getSheetAt(0);
                       
                        //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();
                                   
                                                }
                                               
                                                }         


27)         Excel Test-5 : Writing data into excel sheet columns

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

import org.apache.poi.xssf.usermodel.*;

// How to write a single row into Excel
public class Excelwritingtest1 {

            public static void main(String[] args) throws IOException {
                       
                        FileOutputStream file=new FileOutputStream("C://Test1.xlsx");
                       
                        XSSFWorkbook workbook=new XSSFWorkbook();
                        XSSFSheet sheet=workbook.createSheet("FirstSheet");
                       
                        XSSFRow row=sheet.createRow(0);
                       
                        XSSFCell cell0=row.createCell(0);
                                    cell0.setCellValue("abcd");
                                   
                        XSSFCell cell1=row.createCell(1);
                                    cell1.setCellValue("XYZ");       
                                   
                        XSSFCell cell2=row.createCell(2);
                                    cell2.setCellValue(12345);                 
                                   
                        XSSFCell cell3=row.createCell(3);
                                    cell3.setCellValue(true);
                       
                        workbook.write(file);
                                   
            }

}

28)         Excel Test-6 : Writing data into excel sheet rows

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

import org.apache.poi.xssf.usermodel.*;


public class Excelwritingtest2 {

            public static void main(String[] args) throws IOException {
                       
                        FileOutputStream file=new FileOutputStream("C://Test2.xlsx");
                       
                        XSSFWorkbook workbook=new XSSFWorkbook();
                       
                        XSSFSheet sheet=workbook.createSheet("Data");
                       
                        for(int i=0;i<=5;i++)
                        {
                                   
                                    XSSFRow row=sheet.createRow(i);
                                    XSSFCell cell=row.createCell(0);
                                    cell.setCellValue("ABCD");
                                   
                        }
                       
                        workbook.write(file);
                        file.close();
                                   
                       
            }

}

29)         Excel Test-7 : Writing data into multiple rows and colums in excel sheet.

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

import org.apache.poi.xssf.usermodel.*;
public class Excelwritingmultiplerowsandcolumns {

            public static void main(String[] args) throws IOException {
                       
                        FileOutputStream file=new FileOutputStream("C://Test3.xlsx");
                       
                        XSSFWorkbook workbook=new XSSFWorkbook();
                       
                        XSSFSheet sheet=workbook.createSheet("Data");
                       
                       
                        for(int i=0;i<=5;i++)
                        {
                                    XSSFRow row=sheet.createRow(i);
                                   
                                    for(int j=0;j<=3;j++)
                                    {
                                                XSSFCell cell=row.createCell(j);
                                                cell.setCellValue("ADASDA");
                                    }
                                               
                        }
                                   
                        workbook.write(file);
                        file.close();
                       
            }

}

30)         Excel Test-8 : Calculator Data Driven Test





import java.io.FileInputStream;
import java.io.FileNotFoundException;
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 CalculationDataDrivenTest {

            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");
                       
                        FileInputStream file=new FileInputStream("C://caldata.xlsx");
                       
                        XSSFWorkbook workbook=new XSSFWorkbook(file);
                       
                        XSSFSheet sheet=workbook.getSheet("Sheet1");
                       
                        int rowcount=sheet.getLastRowNum();
                       
                        for(int i=1;i<=rowcount;i++){
                                   
                                    XSSFRow row=sheet.getRow(i);
                                   
                                    XSSFCell princeplecell=row.getCell(0);
                                    int princ=(int)princeplecell.getNumericCellValue();
                                   
                                   
                                    XSSFCell roi=row.getCell(1);
                                    int rateofinterest=(int)roi.getNumericCellValue();
           
                                    XSSFCell period=row.getCell(2);
                                    int per=(int)period.getNumericCellValue();
                       
                                    XSSFCell Frequency=row.getCell(3);
                                    String Freq=Frequency.getStringCellValue();
                                   
                                    XSSFCell MaturityValue=row.getCell(4);
                                    int Exp_mvalue=(int) MaturityValue.getNumericCellValue();
                                   
                                                                       
                                    driver.findElement(By.id("principal")).sendKeys(String.valueOf(princ));
                                    driver.findElement(By.id("interest")).sendKeys(String.valueOf(rateofinterest));
                                    driver.findElement(By.id("tenure")).sendKeys(String.valueOf(per));
                                   
                                    Select periodcombo=new Select (driver.findElement(By.id("tenurePeriod")));
                                    periodcombo.selectByVisibleText("year(s)");
                                                           
                                    Select frequency=new Select (driver.findElement(By.id("frequency")));
                                    frequency.selectByVisibleText(Freq);

                                    driver.findElement(By.xpath(".//*[@id='fdMatVal']/div[2]/a[1]/img")).click();
                                   
                                    String actual_mvalue= driver.findElement((By.xpath(".//*[@id='resp_matval']/strong"))).getText();
                                                                       
                                   
                                   
                                    if((Double.parseDouble(actual_mvalue))==Exp_mvalue)
                                                            {
           
                                                            System.out.println("Test Passed");
                                                            }
                                   
                                    driver.findElement(By.id("principal")).clear();
                                    driver.findElement(By.id("interest")).clear();
                                    driver.findElement(By.id("tenure")).clear();
                       
                        }
            }

                        }


31)         Excel Test-9 : Flight Reservation Login Data Driven Test.

  



import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
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;


public class loginDataDrivenTestExcel {

            public static void main(String[] args) throws IOException,NullPointerException {
                        // TODO Auto-generated method stub
                       
              WebDriver driver= new FirefoxDriver();
              driver.get("http://www.newtours.demoaut.com/");
             
              // file input from file1
             
                        FileInputStream file1=new FileInputStream("C://Login.xlsx");
                        XSSFWorkbook workbook1= new XSSFWorkbook(file1);
                        XSSFSheet sheet1=workbook1.getSheet("Sheet1");
                       
            // file output to file2
                        FileOutputStream file2=new FileOutputStream("C://Login.xlsx");
                        XSSFWorkbook workbook2=new XSSFWorkbook();
                        XSSFSheet sheet2=workbook2.createSheet("Sheet1");
                       
                       
            //Writing headers into file2
                        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");
                       
                        // No. of rows in file1
                        int rownum =sheet1.getLastRowNum();
                       
                        // Reading data from file1
                        for(int i=1;i<=rownum;i++)
                        {
                                    XSSFRow row1=sheet1.getRow(i);
                                   
                                    // first cell value
                                    XSSFCell cell1=row1.getCell(0);
                                    String val1= cell1.getStringCellValue();
                                   
                                    //second cell value
                                    XSSFCell cell2=row1.getCell(1);
                                    String val2=cell2.getStringCellValue();
                                   
                                    driver.findElement(By.name("userName")).sendKeys(val1);
                                    driver.findElement(By.name("password")).sendKeys(val2);
                                    driver.findElement(By.name("login")).click();
                                   
                                    //Writing username, password & result into file2                                                        
                                   
                                    XSSFRow row2=sheet2.createRow(i);
                       
                                    XSSFCell usercell=row2.createCell(0);
                                    usercell.setCellValue(val1);
                                   
                                    XSSFCell passwordcell=row2.createCell(1);
                                    passwordcell.setCellValue(val2);
                                                                       
                                    XSSFCell resultcell=row2.createCell(2);
                                   
                                                                                               
                                    if(driver.getTitle().equals("Find a Flight: Mercury Tours:"))
                                    {
                                               
                                                resultcell.setCellValue("User login PASSED");
                               // System.out.println("user login PASSED");
                                driver.navigate().back();
                                //driver.findElement(By.linkText("Home")).click();
                                    }
                                   
                                    else
                                    {
                                                resultcell.setCellValue("User login FAILED");
                                        //System.out.println("User login FAILED");
                                        driver.findElement(By.linkText("Home")).click();
                                    }
                                   
                                    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                                   
                                    }
                                   
                                    workbook2.write(file2);
                                               
            driver.close();
            file1.close();
            file2.close();
           
            }

}

32)         Cross Bwser Testing on Firefox, IE & Chrome.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class multiBrowsers
{

            public static void IETest()
            {
                                       //Internet Explorer
                                               
                                        System.setProperty("webdriver.ie.driver", "C://Program Files/eclipse/IEDriverServer.exe");
                                        WebDriver driverIE = new InternetExplorerDriver();
                                                   String baseUrl = "http://newtours.demoaut.com";
                                                   String expectedTitle = "Welcome: Mercury Tours";
                                                   String actualTitle = "";
                                                   driverIE.get(baseUrl);
                                                   actualTitle = driverIE.getTitle();

                    if (actualTitle.contentEquals(expectedTitle)){

                        System.out.println("Test Passed!");

                   } else {

                       System.out.println("Test Failed");

                   }
                  
                   driverIE.findElement(By.name("userName")).sendKeys("mercury");
                   driverIE.findElement(By.name("password")).sendKeys("mercury");
                   driverIE.findElement(By.name("login")).click();
                  
                   driverIE.quit();
            }
           
                       
            public static void CHTest()
            {
                                       //Chrome Explorer
                                               
                                        System.setProperty("webdriver.chrome.driver", "C://Program Files/eclipse/chromedriver.exe");
                                        WebDriver driverch = new ChromeDriver();
                                                 
                                        String baseUrl = "http://newtours.demoaut.com";
                                                   String expectedTitle = "Welcome: Mercury Tours";
                                                   String actualTitle = "";
                                                   driverch.get(baseUrl);
                                                   actualTitle = driverch.getTitle();

                    if (actualTitle.contentEquals(expectedTitle)){

                        System.out.println("Test Passed!");

                   } else {

                       System.out.println("Test Failed");

                   }
                  
                    driverch.findElement(By.name("userName")).sendKeys("mercury");
                    driverch.findElement(By.name("password")).sendKeys("mercury");
                    driverch.findElement(By.name("login")).click();
                    driverch.quit();
            }
           

            public static void FTest()
            {
                                       //Firefox
                                               
                                       
                                        WebDriver driver = new FirefoxDriver();
                                                 
                                        String baseUrl = "http://newtours.demoaut.com";
                                                   String expectedTitle = "Welcome: Mercury Tours";
                                                   String actualTitle = "";
                                                   driver.get(baseUrl);
                                                   actualTitle = driver.getTitle();

                    if (actualTitle.contentEquals(expectedTitle)){

                        System.out.println("Test Passed!");

                   } else {

                       System.out.println("Test Failed");

                   }
                  
                    driver.findElement(By.name("userName")).sendKeys("mercury");
                    driver.findElement(By.name("password")).sendKeys("mercury");
                    driver.findElement(By.name("login")).click();
                   
                    driver.quit();
            }
           
public static void main(String[] args)

{
            IETest();
            CHTest();
            FTest();
}

 }
33)         Database Connectivity with Oracle (Insert, Update & Delete)

import java.sql.*;

public class dmlOperationsOracle {
           
  public dmlOperationsOracle() throws ClassNotFoundException, SQLException
 
  {
  
             //String insertquery = "INSERT INTO EMPLOYEE (EMPLOYEE_ID,LAST_NAME,FIRST_NAME)VALUES(2222,'SARAN','KUMAR')";
             //String updatequery ="UPDATE EMPLOYEE SET FIRST_NAME='KUMAR' WHERE EMPLOYEE_ID=7521";
            String deletequery ="DELETE FROM EMPLOYEE WHERE EMPLOYEE_ID=2222";
             
             Class.forName("oracle.jdbc.driver.OracleDriver");
  
             Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@ 143.54.6.0 :1521:ORA11G","scott","tiger"); 
    
             Statement stmt = conn.createStatement();
               
     //stmt.executeQuery(insertquery);    // stmt.executeQuery(updatequery);
    stmt.executeQuery(deletequery);
       
                    
  }
     public static void main(String[] args) throws ClassNotFoundException, SQLException
    
     {
     new dmlOperationsOracle();
     }
  }

34)         Database Connectivity with Oracle (Select)


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class OracleConnection {
           
  public OracleConnection() throws ClassNotFoundException, SQLException {
 
             
              Class.forName("oracle.jdbc.driver.OracleDriver");
  
      Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@ 143.54.6.0:1521:ORA11G","scott","tiger");    
    
      Statement stmt = connection.createStatement();
     
      String query = "select FIRST_NAME,LAST_NAME from EMPLOYEE";
     
      ResultSet rs = stmt.executeQuery(query);
         
       while(rs.next())
       {
          String fName=rs.getString("FIRST_NAME");
          String lName=rs.getString("LAST_NAME");
          System.out.println(fName +"   "+ lName);
          
         }
      
      
     }
     public static void main(String[] args) throws ClassNotFoundException, SQLException
    
     {
     new OracleConnection();
     }
  }

35)         Database Connectivity with SQL Server (Insert, Update & Delete)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class dmlOperationsSQLServer {
           
            public void sqltest() throws ClassNotFoundException, SQLException {
  
             //String insertquery = "INSERT INTO EMPLOYEE (EMPLOYEE_ID,Last_Name,FIRST_NAME)VALUES(2222,'SARAN','KUMAR')";
             //String updatequery ="UPDATE EMPLOYEE SET FIRST_NAME='kumar' WHERE EMPLOYEE_ID=420";
             //String deletequery ="DELETE FROM EMPLOYEE WHERE EMPLOYEE_ID=2222";
             
             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  
                       
             Connection conn =DriverManager.getConnection("jdbc:sqlserver://INVR28ILM6;port=1533;user=sa;password=Welcome1;database=HR;");
    
             Statement stmt = conn.createStatement();
               
     //stmt.executeQuery(insertquery);
     //stmt.executeQuery(updatequery);
     //stmt.executeQuery(deletequery);
    
     String query = "select FIRST_NAME,LAST_NAME from EMPLOYEE";
    
     ResultSet rs = stmt.executeQuery(query);
        
      while(rs.next())
      {
         String fName=rs.getString("FIRST_NAME");
         String lName=rs.getString("LAST_NAME");
         System.out.println(fName +"   "+ lName);
         
      }
    
                             
  }
     public static void main(String args[]) throws ClassNotFoundException, SQLException
    
     {
             dmlOperationsSQLServer obj=new dmlOperationsSQLServer();
            obj.sqltest();
             
   }
    
  }

36)         Log4J for logging

Xml File

"http://jakarta.apache.org/log4j/"
debug="false">
                        "fileAppender"
class="org.apache.log4j.FileAppender">
                        "Threshold"
value="INFO" />
                        "File"
value="logfile.log"/>
                        class
="org.apache.log4j.PatternLayout">
                                    "ConversionPattern"
value="%d %-5p [%c{1}] %m %n" />
                       

                       


           
                        "INFO"
/>
                        "fileAppender"
/>
           




WebDriver Script

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

public class Log4jExample {

            public static void main(String[] args)
            {
                       
                        // Here we need to create logger instance so we need to pass Class name for
                        Logger          logger=         Logger.getLogger("Log4jExample");
                        DOMConfigurator.configure("Log4j.xml");
                       
                                   
                        WebDriver driver=new FirefoxDriver();
                       
                        logger.info("browser opend");
                       
           
                        driver.get("http://newtours.demoaut.com/");
                       
                        logger.info("Clicked on Regiser");
                        driver.findElement(By.linkText("REGISTER")).click();
                       
                        logger.info("Entering contact information");

                        driver.findElement(By.name("firstName")).sendKeys("pavan");
                       
                       
                        driver.findElement(By.name("lastName")).sendKeys("Kumar");
                        driver.findElement(By.name("phone")).sendKeys("1234567895");
                        driver.findElement(By.name("userName")).sendKeys("pavan@gmail.com"); // email id
                       
                        logger.info("Entering mailing information");
           
                        driver.findElement(By.name("address1")).sendKeys("Nizampet");
                        driver.findElement(By.name("address2")).sendKeys("Kukatpally");
                        driver.findElement(By.name("city")).sendKeys("Hyderabad");
                        driver.findElement(By.name("state")).sendKeys("AP");
                        driver.findElement(By.name("postalCode")).sendKeys("500073");
                       
                        logger.info("selecting country");

                        Select dropcountry=new Select(driver.findElement(By.name("country")));
                        dropcountry.selectByVisibleText("INDIA");
           
                        logger.info("Entering user information");

                        driver.findElement(By.name("email")).sendKeys("training2"); // user name
                        driver.findElement(By.name("password")).sendKeys("training1");
                        driver.findElement(By.name("confirmPassword")).sendKeys("training1");
                       
                        logger.info("clicking on register link");
                        driver.findElement(By.name("register")).click();
                       

                        logger.info("validation started");
                          if(driver.getPageSource().contains("Thank you for registering"))
                          {
                              System.out.println("Your registered successfully- TEST PASSED");
                                    logger.info("validation DONE");
                           }
                         
                          else
                         
                          {
                                      System.out.println("Your registration failed - TEST FAILED");
                                      logger.info("validation DONE");
                           }

                         logger.info("Exit Test");
                         
                         driver.quit();

}
}


37)         Page Object Model (POM)

Pom class for User Registration in Flight Application.

import org.openqa.selenium.*;

public class PageObjectsOfFlightRegistation
{
   private static WebElement element = null;

   //Registration link
   public static WebElement registration_lnk(WebDriver driver)
   {
      element = driver.findElement(By.linkText("REGISTER"));
      return element;
   }
  
       
   // Contact Info
    
   public static WebElement contact_fname_txt(WebDriver driver)
   {
      element = driver.findElement(By.name("firstName"));
      return element;
   }
           
  
    
   public static WebElement contact_lname_txt(WebDriver driver)
   {
      element = driver.findElement(By.name("lastName"));
      return element;
   }
  
   public static WebElement contact_phone_txt(WebDriver driver)
   {
      element = driver.findElement(By.name("phone"));
      return element;
   }
  
   public static WebElement contact_uname_txt(WebDriver driver)
   {
      element = driver.findElement(By.name("userName"));
      return element;
   }
  
  
// Mailing Info
  
   public static WebElement mail_address1_txt(WebDriver driver)
   {
      element = driver.findElement(By.name("address1"));
      return element;
   }
  
  
   public static WebElement mail_address2_txt(WebDriver driver)
   {
      element = driver.findElement(By.name("address2"));
      return element;
   }
  
  
   public static WebElement mail_city_txt(WebDriver driver)
   {
      element = driver.findElement(By.name("city"));
      return element;
   }
  
   public static WebElement mail_state_txt(WebDriver driver)
   {
      element = driver.findElement(By.name("state"));
      return element;
   }
  
  
   public static WebElement mail_postal_txt(WebDriver driver)
   {
      element = driver.findElement(By.name("postalCode"));
      return element;
   }
  
   
   public static WebElement mail_country_dropdown(WebDriver driver)
   {
      element = driver.findElement(By.name("country"));
      return element;
   }
  
   //User Details
  
   public static WebElement user_email_txt(WebDriver driver)
   {
      element = driver.findElement(By.name("email"));
      return element;
   }
  
   public static WebElement user_password_txt(WebDriver driver)
   {
      element = driver.findElement(By.name("password"));
      return element;
   }
  
   public static WebElement user_confpassword_txt(WebDriver driver)
   {
      element = driver.findElement(By.name("confirmPassword"));
      return element;
   }
  
  
   //Register link
  
   public static WebElement reg_submit_lnk(WebDriver driver)
   {
      element = driver.findElement(By.name("register"));
      return element;
   }
     
}


WebDriver  script for User Registration in Flight Application.

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

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

                 
                 PageObjectsOfFlightRegistation.registration_lnk(driver).click();
                
                             
                                   
                                    // Entering Contact information
                 PageObjectsOfFlightRegistation.contact_fname_txt(driver).sendKeys("pavan");
                 PageObjectsOfFlightRegistation.contact_lname_txt(driver).sendKeys("Kumar");
                 PageObjectsOfFlightRegistation.contact_phone_txt(driver).sendKeys("1234567895");
                 PageObjectsOfFlightRegistation.contact_uname_txt(driver).sendKeys("pavan@gmail.com"); // email id
                                   
                                    //Entering mailing information
                                   
                 PageObjectsOfFlightRegistation.mail_address1_txt(driver).sendKeys("Nizampet");
                 PageObjectsOfFlightRegistation.mail_address2_txt(driver).sendKeys("Kukatpally");
                 PageObjectsOfFlightRegistation.mail_city_txt(driver).sendKeys("Hyderabad");
                 PageObjectsOfFlightRegistation.mail_state_txt(driver).sendKeys("AP");
                 PageObjectsOfFlightRegistation.mail_postal_txt(driver).sendKeys("500073");
                                   
                                    //Country selection
                                    Select dropcountry;
                                   
                                    dropcountry=new Select (PageObjectsOfFlightRegistation.mail_country_dropdown(driver));
                            dropcountry.selectByVisibleText("INDIA");
                       
                                    //Entering User information
                                   
                            PageObjectsOfFlightRegistation.user_email_txt(driver).sendKeys("training2"); // user name
                            PageObjectsOfFlightRegistation.user_password_txt(driver).sendKeys("training1");
                            PageObjectsOfFlightRegistation.user_confpassword_txt(driver).sendKeys("training1");
                                   
                                    //Submitting form
                                   
                            PageObjectsOfFlightRegistation.reg_submit_lnk(driver).click();
                                   
                        //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");
                                       }
             
            }
                 
}

38)         Downloading Files


import java.io.IOException;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class downloadFile {

public static void main(String[] args)
{
String baseUrl = "http://messenger.yahoo.com/";
WebDriver driver = new FirefoxDriver();

driver.get(baseUrl);
WebElement downloadButton = driver.findElement(By.id("messenger-download"));
String sourceLocation = downloadButton.getAttribute("href");
String wget_command = "cmd /c wget -P c: --no-check-certificate " + sourceLocation;
System.out.println(wget_command);
try {
Process exec = Runtime.getRuntime().exec(wget_command);
//Process execN=Runtime.getRuntime().exec("notepad.exe");
int exitVal = exec.waitFor();

System.out.println("Exit value: " + exitVal);
} catch (InterruptedException | IOException ex) {
System.out.println(ex.toString());
}
driver.quit();
}
}



0 comments:

Post a Comment