【问题标题】:how to run testng.xml multiple times with different testdata from excel如何使用来自excel的不同测试数据多次运行testng.xml
【发布时间】:2018-02-20 19:56:50
【问题描述】:

我想知道我们如何使用不同的数据集多次运行 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 中获取值。

提前致谢!

【问题讨论】:

  • @Yashica...使用@DataProvider .. 发布您的代码,以便我可以告诉您如何实现相同
  • @GauravGenius,嗨 Gaurav,我正在使用 dataprovider 从 excel 中获取数据,我的 dataprovider 正在返回 hashmap 其中键是标题,值是这些标签的值。即 username=abc 以便用户可以根据键名获取列值。我想要的是:像这样为 testcase1,2,3,4 运行相同的 testngxml。
  • @DataProvider(name="getdata") public static Object[][] getData(ITestContext context) throws Exception { HashMap ExcelDataMap= new HashMap(); String TestCaseNo = context.getCurrentXmlTest().getParameter("TestCaseNo"); ExcelDataMap=hashMap.get(TestCaseNo);返回新对象[][]{ {ExcelDataMap} }; } hashmap 包含整个 excel,其中键作为测试用例编号和相应的行值
  • 我尝试使用 dataprovider 根据 testng xml 中指定的参数返回对象,但这种方式 @Test 注释是根据对象数量运行的。我想用一个参数运行整个测试套件一次,然后用第二个参数运行。我们可以这样做吗?

标签: xml excel selenium testng


【解决方案1】:

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

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

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

类 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);
    }
}

类测试工厂

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>

【讨论】:

  • 嗨,gaurav,谢谢我尝试实现这一点。一切正常。现在唯一的问题是,如果我有多个课程,它就不起作用。所有数据集首先为第一类运行,然后为第二类运行。我想用一组数据运行包含多个类(多个方法)的整个套件,然后运行下一组数据
猜你喜欢
  • 2014-02-19
  • 2011-01-18
  • 1970-01-01
  • 2013-06-13
  • 2021-01-06
  • 2010-10-19
  • 1970-01-01
  • 2015-03-04
  • 1970-01-01
相关资源
最近更新 更多