多个工厂的TestNg测试

时间:2014-09-11 16:21:56

标签: java selenium selenium-webdriver testng

参考我之前的问题, " Executing multiple test sequentially with different parameters testng"

我目前遇到的困难是,我想按顺序为不同的多参数执行一个testNg测试类,不同的参数是准确的

Test-1 with data:laptop,lg

测试-2数据:笔记本电脑,三星

测试-3数据:笔记本电脑,东芝

测试-1,数据:TV,Lg

测试-2与数据:电视,三星

测试-3与数据:电视,东芝

3 个答案:

答案 0 :(得分:0)

基本的TestNG DataProvider完全符合您的要求。您只需要使用引用DataProvider的测试方法创建一个简单的TestNG项目。

答案 1 :(得分:0)

你试过这样的尝试吗?它不按顺序执行吗?

//This method will provide data to any test method that declares that its Data Provider
//is named "test1"
@DataProvider(name = "test1")
public Object[][] createData1() {
 return new Object[][] {
   { "laptop", "Lg" },
   { "laptop", "Samsung"},
   { "laptop", "Toshiba"},
   { "TV", "Lg" },
   { "TV", "Samsung"},
   { "TV", "Toshiba"},
 };
}

//This test method declares that its data should be supplied by the Data Provider
//named "test1"
@Test(dataProvider = "test1")
public void verifyData1(String device, String brand) {
 System.out.println(device + " " + brand);
}

答案 2 :(得分:0)

很好找到一个解决方案,但不是一个完美的解决方案,如果有人有一个ans然后评论,我使用以下代码来获得上述结果,但这里的情况是,有时它按顺序执行,但有时随机执行。以下是代码段

public class TestFactoryTest {

   public  static List<Object> addObject=new ArrayList<Object>();
   public static  List<String> sheetName=new ArrayList<String>();
   public static  List<Object> getworkbook=new ArrayList<Object>();
   public File folder = new File("D:\\ChannelSense");

   Object objArray[]=null;

   static
   {
          sheetName.add("Television");
          sheetName.add("Laptop");

   }

   @Factory(dataProvider="ExcelFiles")
   public Object[] runclass(String addObject1)
   {
          addObject=new ArrayList<>();
          System.out.println("addObject1>>"+addObject1);

          for(int i=0;i<sheetName.size();i++)
          {
           System.out.println("addObject1>>"+addObject1+">>"+sheetName.get(i));
           addObject.add(new TestMethod(addObject1,sheetName.get(i)));        
          }

          System.out.println("addObject.size;>>"+addObject.size());
          objArray=new Object[addObject.size()];
          for(int i=0;i<addObject.size();i++)
          {
                 objArray[i]=addObject.get(i);
          }
          return objArray;
   }

   @DataProvider(name="ExcelFiles")
   public Iterator<Object[]> passaddObject()
   {
          final File FolderName=folder;
          List<String> lst=new ArrayList<String>();
          List<Object[]> dataToBeReturned = new ArrayList<Object[]>();
          for(final File workbookname:FolderName.listFiles())
          {

                       lst.add(workbookname.getName()); 


          }
          for(String workbookname:lst)
          {
                 dataToBeReturned.add(new Object[]{workbookname});
          }
          return  dataToBeReturned.iterator();
   }

}

当我第一次执行它时,输出上面的代码,然后它给出,

-----TestCase1-----
Product:  Laptops
File: LG.xlsx
-----TestCase1-----
Product:  Television
File: LG.xlsx
-----TestCase1-----
Product:  Television
File: Toshiba.xlsx
-----TestCase1-----
Product:  Laptops
File: Toshiba.xlsx


In second execution it returns,

-----TestCase1-----
Product:  Laptops
File: LG.xlsx
-----TestCase1-----
Product:  Television
File: Toshiba.xlsx
-----TestCase1-----
Product:  Television
File: LG.xlsx
-----TestCase1-----
Product:  Laptops
File: Toshiba.xlsx

请帮助!!