如何从excel停止的地方继续执行?

时间:2016-08-26 11:19:13

标签: java excel selenium apache-poi

我正在使用selenium web驱动程序使用excel,其中传递用户名和密码是从excel获取并传递给应用程序,传递/失败状态被写回excel。在例如没有找到元素等异常时执行停止。如何从excel中停止的位置继续执行。以下是我的代码:

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

import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.UnreachableBrowserException;

public class Checkbox2 {

        static WebDriver driver;
        private static  String filePath = "C:\\TEST DATA\\Users\\test.xlsx";
        private static String sheetName = "Sheet1";
        static File fl= new File(filePath);
        public static void main(String[] args) throws InterruptedException, EncryptedDocumentException, InvalidFormatException
        {  
            System.setProperty("webdriver.firefox.bin", "C:\\Users\\vijayab\\AppData\\Local\\Mozilla Firefox\\firefox.exe");
            driver=new FirefoxDriver();

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

            driver.get("https://qa-bnymellon.correctnet.com/bnymellon/release10/me.get?DPS.home");

        //    driver.findElement(By.xpath("html/body/div[4]/div/div/div[1]/a[1]")).click();

            try {
                FileInputStream fis = new FileInputStream("C:\\Users\\vijayab\\Documents\\Work\\TEST DATA\\Users\\test.xlsx");
                Workbook wb;
                wb = WorkbookFactory.create(fis);
               Sheet sheet = wb.getSheet("Sheet1");

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

                    runTest(count, sheet, row.getCell(0).toString(),row.getCell(1).toString(),row,wb);

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

        }

        public static  void runTest(int count,Sheet sheet,String name,String mailid, Row row, Workbook wb) throws InterruptedException, InvalidFormatException, IOException 
        {       
            System.out.println("Inputing name: "+name+" and mailid: "+mailid);
            driver.findElement(By.name("USERNAME")).sendKeys(name);
            driver.findElement(By.name("PASSWORD")).sendKeys(mailid);
            driver.findElement(By.name("SIGNIN")).click();
            //driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
          try{
              if(driver.findElements(By.name("loginForm")).size() == 0){
                    System.out.println("Valid credentials"+count);
                    driver.findElement(By.name("DISCLAIMER")).click();

                    driver.findElement(By.id("ACCOUNTDOCUMENTS")).click();

                    driver.findElement(By.xpath("//*[contains(text(),'Account Profile')]")).click(); 
                  Thread.sleep(3000);

                   driver.switchTo().frame("FDX1");

                   driver.switchTo().frame("frame2-1");
                   Thread.sleep(4000);
                    driver.findElement(By.name("chk")).click();

                   driver.findElement(By.name("PROFILE_UPDATE")).click();
                    Alert alert = driver.switchTo().alert();
                    alert.accept();

                    driver.get("<logout url>");

                    int cellindex = 3;
                    WriteToFile.setExcelData(wb,filePath, sheetName, row.getRowNum(),cellindex, "PASS");
                    System.out.println("Inputted name: "+name+" and mailid: "+mailid);
                    Thread.sleep(2000); 
            }
              else if(driver.findElements(By.name("loginForm")).size() == 0){
                  driver.findElement(By.name("loginForm")).isDisplayed();

                    System.out.println("Inputted name: "+name+" and mailid: "+mailid + "does not exist");
                    int cellindex = 3;
                    WriteToFile.setExcelData(wb,filePath, sheetName, row.getRowNum(),cellindex, "Fail");
                    return;
              }
          }
          catch(UnreachableBrowserException e){
              System.out.println("Exception occured");

          }
        }
    }

1 个答案:

答案 0 :(得分:0)

可能有不同的方法来处理它。我的建议是在一个以下。

添加另一个Catch块以捕获方法Exceptions中的runTest。如果出现异常,则将Boolen值返回false。根据退货类型,决定如何进一步加强For Loop。我猜这应该可以帮助你解决问题。如有任何疑问,请通知我。