如何使用excel

时间:2018-02-20 19:56:50

标签: xml excel selenium testng

我想知道如何使用不同的数据集多次运行testng xml。 我基于testng xml中存在的testcaseno参数从excel文件中获取数据,并且它正在按预期工作。现在我想通过对多组数据使用相同的xml文件来扩展我的测试。

testng.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Sample Test Suite 1">
   <parameter name="browser" value="Firefox"/>
   <parameter name="TestCaseNo" value="1"/>
     <test name="Testcase1">

      <classes>
            <class name="packagename.ABCTest">
            <methods> 
            <include name="method_A" />
            <include name="method_B" />
            <include name="method_C"/>
            <include name="method_D"/>
            </methods>
            </class>
      </classes>
        </test> 
</suite>

截至目前,我的excel实用程序从testng xml获取testcaseno值并获取与所需行对应的数据。 我想为5个测试用例运行相同的testng xml。

请帮助我实现这一目标。 我可以实现基于测试用例号的调用计数,因为我依赖于来自testng xml的参数来从excel获取值。

提前致谢!

1 个答案:

答案 0 :(得分:0)

以下是您的问题的解决方法,使用@DataProvider和@Factory注释在运行时创建测试。根据您的需要,根据事情修改...

  • 根据您的要求更改getExcelData()方法
  • 根据要求
  • 更改测试方法,即firstTest(),secondTest()....

这将使用一个参数执行整个测试套件一次,然后使用第二个参数执行。

Class SomeTest

import org.testng.annotations.Test;

public class SomeTest {

    private String username;
    private String password;

    public SomeTest(String username, String password) {
        this.username = username;
        this.password = password;
    }

    @Test
    public void firstTest() {
        System.out.println("Test #1 with data: "+username+" and "+password);
    }

    @Test
    public void secondTest() {
        System.out.println("Test #2 with data: "+username+" and "+password);
    }

    @Test
    public void thirdTest() {
        System.out.println("Test #3 with data: "+username+" and "+password);
    }
}

Class TestFactory

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;

public class TestFactory {

        private static FileInputStream fileInputStream;
        private static XSSFWorkbook workbook;
        private static XSSFSheet sheet;
        private static XSSFCell cell;

        @Factory(dataProvider="data")
        public Object[] createInstances(String username, String password) {
            return new Object[] {new SomeTest(username, password)};
        }

        @DataProvider(name="data")
        public Object[][] dataMethod() {
            Object[][] arrayObjects = getExcelData("C:/Eclipse/testdata.xlsx");
            return arrayObjects;    
        }

        public String[][] getExcelData(String fileName){

            String[][] arrayExcelData = null;
            try{
                fileInputStream = new FileInputStream(fileName);
                workbook = new XSSFWorkbook(fileInputStream);
                sheet = workbook.getSheetAt(0);

                int totalRows = sheet.getPhysicalNumberOfRows();
                int totalCols = sheet.getRow(0).getPhysicalNumberOfCells();

                arrayExcelData = new String[totalRows-1][totalCols];
                int startRow = 1;
                int startCol = 0;

                    for(int i = startRow; i < totalRows; i++){
                        for(int j = startCol; j < totalCols; j++){
                            arrayExcelData[i-1][j] = getCellData(i,j);
                        }
                    } 
                fileInputStream.close();
            }catch(FileNotFoundException e){
                e.printStackTrace();
            }catch(IOException e){
                e.printStackTrace();
            }

            return arrayExcelData;
        }

        public static String getCellData(int rowNum, int colNum){
            String cellData = "";
            try{
                cell = sheet.getRow(rowNum).getCell(colNum);
                switch(cell.getCellTypeEnum()) {
                    case BOOLEAN:
                        cellData = cell.getRawValue();
                        break;
                    case NUMERIC:
                        cellData = cell.getRawValue();
                        break;
                    case STRING:
                        cellData = cell.getStringCellValue();
                        break;
                    default:
                        break;
                }

            }catch(Exception e){
                e.printStackTrace();
            }
            return cellData;
        }
    }

TestFactory.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TestFactory multiple test together" verbose="10">

  <test name="TestFactory" group-by-instances="true">
    <classes>
      <class name="com.knexuscloud.TestFactory"></class>
    </classes>
  </test>
</suite>