简体   繁体   中英

How to put value on webform (like any registration page) from excel using selenium webdriver in java?

this is updated code Now i hv tried to read as well as put it on the webform but somehow both things are not working XL file not found. now m not getting how to put the values on the webform using selenium webdriver .i have put the logic in my code but due file not found its not fetching properly.Logic is correct or not .Please help me Thanks in advance!

package testdata;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
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.firefox.FirefoxDriver;

public class DatawithExcel { public static void main(String[] args) throws InterruptedException  {   
            FirefoxDriver driver = new FirefoxDriver();

            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

            driver.get("http://newtours.demoaut.com/mercurywelcome.php?osCsid=670caec15c0566144c663f12af6fa66b");

            driver.findElement(By.xpath("html/body/div[1]/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr/td[2]/a")).click();
            try {
                FileInputStream fis = new FileInputStream("C:\\testdata.xlsx");
                XSSFWorkbook wb = new XSSFWorkbook(fis);

                XSSFSheet sheet = wb.getSheet("TestData");

                for(int count=1;count<=sheet.getLastRowNum();count++)
                {
                    XSSFRow row = sheet.getRow(count);
                    System.out.println("\n----------------------------");
                    System.out.println("Running test case " + count);

                 runTest(row.getCell(0).toString(),row.getCell(1).toString(),row.getCell(2).toString(),row.getCell(3).toString(), row.getCell(4).toString(),row.getCell(5).toString(),row.getCell(6).toString());

                }
                fis.close();
                driver.close();// Closing the firefox driver instance
            } catch (IOException e) {
                System.out.println("Test data file not found");
            }   
        }

         public static  void runTest(String FN,String LN,String Phone,String userName,String Address,String city,String State ) throws InterruptedException 
            {       
                System.out.println("Inputing First name: "+FN+"  Last Name:"+LN+" Phone: "+Phone+"  Email: "+userName+"Address:"+Address+"city"+city+"State:"+State+"");
                FirefoxDriver driver = new FirefoxDriver();
            driver.findElement( By.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[1]/td[2]/input")).clear();
                driver.findElement( By.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[2]/td[2]/input")).clear();
                driver.findElement( By.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[2]/td[2]/input")).clear();
                driver.findElement( By.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[2]/td[2]/input")).clear();
                driver.findElement( By.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[2]/td[2]/input")).clear();
                driver.findElement( By.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[2]/td[2]/input")).clear();
                driver.findElement( By.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[2]/td[2]/input")).clear();
                driver.findElement( By.name("firstName")).sendKeys(FN);
                driver.findElement( By.name("lastName")).sendKeys(LN);
                driver.findElement( By.name("phone")).sendKeys(Phone);
                driver.findElement( By.name("userName")).sendKeys(userName);
                driver.findElement( By.name("address1")).sendKeys(Phone);
                driver.findElement( By.name("city")).sendKeys(Address);
                System.out.println("Inputing First name: "+FN+"  Last Name:"+LN+"Phone: "+Phone+"  Email: "+userName+"Address:"+city+"State:"+State+"");

                Thread.sleep(2000); // Sleeping 2 seconds so that each entry is detected.

            }
        } 
successfully executed both the parts reading from xcel and putting on to webpage thanks for ur helpful suggestions make this possible.now both things are working.  

  import java.io.FileInputStream;
    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;
    import driverMain.ReadExcel;


    public class testNewTour {

        public static WebDriver driver = null;
         public static void main(String[] args) throws InterruptedException, IOException
            {   


                driver = new FirefoxDriver();

                driver.manage().window().maximize();
                driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

                driver.get("http://newtours.demoaut.com/mercurywelcome.php?osCsid=670caec15c0566144c663f12af6fa66b");

                driver.findElement(By.xpath("html/body/div[1]/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr/td[2]/a")).click();

                FileInputStream fis = new FileInputStream("C:\\Ruchi\\TestData.xlsx");
                XSSFWorkbook wb = new XSSFWorkbook(fis);
                XSSFSheet ws = wb.getSheet("Sheet1");

               XSSFRow dataRow = ws.getRow(1);
                      XSSFCell r3c2 = dataRow.getCell(0);
                      String FirstName = r3c2.getStringCellValue();


                      System.out.println("FirstName: " + FirstName);

                      for(int count=1;count<=ws.getLastRowNum();count++)
                        {
                            XSSFRow row = ws.getRow(count);
                            System.out.println("\n----------------------------");
                            System.out.println("Running test case " + count);

                            runTest(row.getCell(0).toString(),row.getCell(1).toString(),row.getCell(2).toString(),row.getCell(3).toString(), row.getCell(4).toString(),row.getCell(5).toString(),row.getCell(6).toString());

                        }


                    fis.close();
                    driver.close();// Closing the firefox driver instance
                } 


             public static  void runTest(String FN,String LN,String Phone,String userName,String Address,String city,String State ) throws InterruptedException 
                {       
                    System.out.println("Inputing First name: "+FN+"  Last Name:"+LN+" Phone: "+Phone+"  Email: "+userName+"Address:"+Address+"city"+city+"State:"+State+"");

                    driver.findElement( By.name("firstName")).sendKeys(FN);
                    driver.findElement( By.name("lastName")).sendKeys(LN);
                    driver.findElement( By.name("phone")).sendKeys(Phone);
                    driver.findElement( By.name("userName")).sendKeys(userName);
                    driver.findElement( By.name("address1")).sendKeys(Address);
                    driver.findElement( By.name("city")).sendKeys(city);
                    driver.findElement( By.name("state")).sendKeys(State);



                    System.out.println("Inputing First name: "+FN+"  Last Name:"+LN+"Phone: "+Phone+"  Email: "+userName+"Address:"+city+"State:"+State+"");

                    Thread.sleep(7000); // Sleeping 2 seconds so that each entry is detected.
    //
                }
            }  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM