2016-08-26 1 views
0

Ich arbeite mit Excel mit Selen-Web-Treiber, wo Benutzername und Passwort übergeben wird von Excel übernommen und an die Anwendung übergeben, Pass/Fail-Status wird zurück zu Excel geschrieben.Während Ausnahmen wie keine Element gefunden etc. die Ausführung stoppt. Wie führe ich die Ausführung von dem Punkt aus fort, an dem sie im Excel angehalten hat? Im Anschluss ist mein Code:Wie setze ich die Ausführung fort, wo die Excel gestoppt hat?

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"); 

      } 
     } 
    } 

Antwort

0

Es könnte verschiedene Möglichkeiten, es zu behandeln. Was ich vorschlagen würde, ist unter einem.

Fügen Sie einen weiteren Catch Block zu fangen Exceptions in Methode runTest. Geben Sie im Fall von Ausnahmen einen Boolen Wert als false zurück. Berücksichtigen Sie anhand des Rückgabetyps, wie Sie in Ihrem Fall fortfahren können For Loop. Ich denke, dies sollte Ihnen helfen, das Problem zu lösen. Bitte lassen Sie mich im Falle von Fragen wissen.

Verwandte Themen