使用多个测试数据表运行多个测试用例

时间:2015-11-21 03:48:21

标签: selenium testng

我需要帮助硒。我正在尝试使用TestNG和Data Provider概念运行多个包含多个测试数据表的测试用例。但是我在运行测试数据时遇到了问题。如果我在Test中返回数组,从sheet获取测试数据后再从一行测试用例开始,这样它总是运行数据驱动函数的第一行。

如果我在DataDriven结束时返回,那么最后一行将在Test中打印出值。休息将会过夜。

请让我知道如何运行我的测试用例。我附上了代码和测试用例以及测试数据表。

Test Cases & test data sheet

package testCases;

import java.util.Properties;
import operation.ReadObject;
import operation.UIOperation;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.Iterator;
import java.lang.reflect.Method;
import excelExportAndFileIO.ReadExcelFile;

public class Test1 {
    private static final Boolean True = null;

    //WebDriver webdriver = null;
    public static WebDriver driver;

    private Cell Cell;
    private Cell TCCellValue;

    /*  @BeforeSuite
    public static void firefoxSetUp() {
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
    }

    @AfterSuite
    public static void closeFirefox(){
       driver.quit();
    }*/

    @Test(dataProvider = "hybridData")
    public void Permittee_Registration(String Status, String TC, String Module1, String Module2 )   {               //String Status, String TCName, String TCDesc) throws Exception {
        System.out.println("Status:"+Status +" ; TC:"+TC + " ; Module1:"+Module1 + " ; Module2:"+Module2);
    }

    // Call the test data sheet ----- PR = Permittee Registration
    @Test(dataProvider = "hybridData")
    public void PR_Applicant_Information(String TestCase, String URL, String objPermittee, String objPR, String PR_TITLE, String objLegal_Entity_Type, String Permittee_Legal_Name, String EIN  )   {
        System.out.println("PR_Applicant_Information Test");
        if (TestCase!=null){
            System.out.println("");
            System.out.println("TestCase:"+TestCase + " ; PR_Applicant_Information URL:"+URL + " ; objPermittee: "+objPermittee + " ; objPR: " +objPR  + " ; PR_TITLE: " +PR_TITLE + " ; objLegal_Entity_Type: " +objLegal_Entity_Type + " ; Permittee_Legal_Name: " + " ; EIN: " +EIN + "; dd: dd") ;
        }

    }
    @Test(dataProvider = "hybridData")
    public void PR_Choose_Qualification(String TC, String URL){
        if (TC!=null){
            System.out.println("");
            System.out.println("PR_Choose_Qualification TC:"+TC + " ; URL: "+ URL);
        }
    }

    @DataProvider(name = "hybridData")
    public Iterator[] loginData(Method method) {
        //System.out.println("Hybrid");
        String TCName="";
        Object[][] result = null;
        Object[][] testData = new String[10][10];

        Object[][] arrayObject = getExcelData(("user.dir") + "\\TestCaseSheet.xlsx", "TestCases_Modules",TCName);

        //K=Row and L=Column
        for (int k=0; k < arrayObject.length; k++){
            int colCount=arrayObject[k].length;
            //System.out.println("Column=arrayObject[k].length:"+arrayObject[k].length);
            if ("YES".equals(arrayObject[k][0])){
                //System.out.println("arrayObject[k][L]:"+arrayObject[k][L]+" ; K="+k+" ; L="+L);
                //System.out.println("arrayObject[k][0]----:"+arrayObject[k][0] +" ; K = "+k );
                for (int L=0; L< colCount ; L++){                   
                    //"Permittee_Registration"                          
                    //System.out.println("Permittee_Registration"+arrayObject[k][2]);
                    if ((arrayObject[k][L] !=null) && ((String)arrayObject[k][L]).indexOf(",") > 0) {
                        String[] pr = ((String)arrayObject[k][L] ).split(",");
                        //System.out.println("pr:"+pr.length + " ; PRValue:"+pr[0]);
                        for (int prN=0; prN < pr.length; prN++ ){
                            //System.out.println("for (int prN=0; prN < pr.length; prN++ )");
                            //pr=Applicant_Information
                            TCName=(String) arrayObject[k][1];  //fetching the Test Case Name
                            //System.out.println("TCName:"+TCName + " ; equals(pr[prN]) = "+equals(pr[prN]) + " ; pr.length="+pr.length + " ; pr[prN]="+ pr[prN]);
                            if ("Applicant_Information".equals(pr[prN])){                               
                                System.out.println("if (Applicant_Information.equals(pr[prN]))"+pr[prN] + " ;  pr.length="+pr.length );
                                if (method.getName().equals("PR_Applicant_Information")) {
                                    System.out.println("Applicant_Information Moni before calling excel sheet function:"+" ; result.length"+result.length);
                                    //result = getExcelData(("user.dir") + "\\TestCaseSheet.xlsx", "Applicant_Information",TCName);
                                    result = getExcelData(("user.dir") + "\\TestCaseSheet.xlsx", pr[prN] ,TCName);
                                    //AI(result);
                                    //System.out.println("Applicant_Information Moni afte calling excel sheet function:"+" ; result.length"+result.length);
                                    //return result;
                                };
                            }
                            else if ("Choose_Qualification".equals(pr[prN])){
                                if (method.getName().equals("PR_Choose_Qualification")) {
                                    //System.out.println("Choose_Qualification Moni before calling excel sheet function:"+pr[prN] +" ; result.length"+result.length);
                                    result = getExcelData(("user.dir") + "\\TestCaseSheet.xlsx", "Choose_Qualification",TCName);
                                    System.out.println("Choose_Qualification Moni after calling excel sheet function:"+pr[prN] +" ; result.length"+result.length);

                                };
                            }                           
                        }
                        //return result;
                    }                   
                }
            }
        }           
        return result;

    }
public void AI(Object result){
    System.out.println("resultAI:"+result);
}
    /**
     * @param File
     *            Name
     * @param Sheet
     *            Name
     * @return
     */
    public String[][] getExcelData(String fileName, String sheetName, String TCName) {
    //public List<String> getExcelData(String fileName, String sheetName, String TCName) {
        String[][] arrayExcelData = null;
        int a=0;
        int b=0;
        try {

            ReadExcelFile file = new ReadExcelFile();

            // UIOperation operation = new UIOperation(driver);
            // Read keyword sheet
            Sheet testCaseSheet = file.readExcel(System.getProperty("user.dir") + "\\", "TestCaseSheet.xlsx", sheetName);
            // Find number of rows in excel file
            int rowCount = testCaseSheet.getLastRowNum() - testCaseSheet.getFirstRowNum();
            int coulmnCount = testCaseSheet.getRow(0).getLastCellNum();
            int firstCoulmnCount = testCaseSheet.getRow(0).getFirstCellNum();
            arrayExcelData=new String[rowCount+1][coulmnCount];
            //System.out.println("rowCount:"+rowCount);
            //System.out.println("coulmnCount:"+coulmnCount);
            //Looping the row   

            for (int i = testCaseSheet.getFirstRowNum(); i < (testCaseSheet.getLastRowNum()+1) ; i++) {
                // Loop over all the rows               
                //Row row=testCaseSheet.getRow(i + 1);
                int j;
                if ("" != (TCName)){
                    //Object TCCellValues = null;
                }
                for (j = firstCoulmnCount; j < coulmnCount ; j++) {
                    Cell = testCaseSheet.getRow(i).getCell(j);
                    //System.out.println("TCName in recurring function :"+TCName);
                    //System.out.println("Cell:"+Cell);
                    //if TCName is equal to particular row then pick only that cell data.

                    TCCellValue = testCaseSheet.getRow(i).getCell(0);
                    String TCCellValues = TCCellValue.getStringCellValue();
                    //System.out.println("if ( != (TCCellValue)): " +TCCellValue + " ; TCName=" + TCName + " ; i="+i);                                      
                        String CellData="";
                        //System.out.println("Cell inside:"+Cell);
                        if (Cell!=null){
                            CellData = Cell.getStringCellValue();
                            //System.out.println(i+":"+CellData);
                        }                                           
                        if (TCName.equals("")||(TCName.equals(TCCellValues))){

                            arrayExcelData[i][j]=CellData;
                            //System.out.println("arrayExcelData[i][j]=CellData;"+CellData + " ; i="+i +" ; j="+j + " ; TCName="+TCName + " ; a="+a +" ; b="+b );
                        }
                }                   
            }

        } catch (Exception e) {
            System.out.println("Error " + e);           
        }
        //System.out.println("Stop_arrayExcelData:");
        return arrayExcelData;
        //return new List<String>{{arrayExcelData}};
    }
}

0 个答案:

没有答案