TestNG @dataprovider @factory order execution

61 views
Skip to first unread message

Fabio Giusti

unread,
Mar 17, 2021, 4:48:04 AM3/17/21
to testng-users
Good morning,
I have a problem running my test case, using @dataprovider and @factory.
I read from an excel file (@dataprovider) of the data and with the @factory run various test methods, the problem that the order of execution does not respect the order of how the values are written inside the excel file, but it happens which performs the third value first, then the first and so on ...

is there any way to perform in order of how they are written in the excel file?

thanks

⇜Krishnan Mahadevan⇝

unread,
Mar 23, 2021, 9:32:19 AM3/23/21
to testng-users
Fabio,

Can you please check if you can use an implementation of org.testng.IDataProviderInterceptor to control the order of the instances in which they are instantiated ?

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/44480d45-4744-4ada-95b6-faf4122b4ac8n%40googlegroups.com.

Fabio Giusti

unread,
Mar 23, 2021, 10:10:26 AM3/23/21
to testng-users
Thanks for the answer, I don't think I'm using it ... I'm just trying my hand at it.

do you have any examples to follow?
my spartan code is this

Class Dataprovider read excel file
-------------------------------------------------------
public class InputDataProvider {

    @DataProvider(name = "DatiInput") //(name ="BCBEdata")
    public Object[][] excelDP(ITestContext context) throws IOException {
        String DataSheet=context.getCurrentXmlTest().getParameter("DataSheet");
        System.out.println("Lettura dati di configurazione "+context.getCurrentXmlTest().getParameter("DataSheet"));
        //We are creating an object from the excel sheet data by calling a method that reads data from the excel stored locally in our system
        InputDataProvider inputDataProvider = new InputDataProvider();

        Object[][] arrObj = inputDataProvider.getExcelData(System.getProperty("user.dir") + "\\config\\Pippo.xlsx"
                , Pippo);
        return arrObj;
    }

    //This method handles the excel - opens it and reads the data from the respective cells using a for-loop & returns it in the form of a string array
    public String[][] getExcelData(String fileName, String sheetName){
        String[][] data = null;
        try
        {
            FileInputStream fis = new FileInputStream(fileName);
            XSSFWorkbook wb = new XSSFWorkbook(fis);
            XSSFSheet sh = wb.getSheet(sheetName);
            XSSFRow row = sh.getRow(0);
            int noOfRows = sh.getPhysicalNumberOfRows();
            int noOfCols = row.getLastCellNum();
            Cell cell;
            data = new String[noOfRows-1][noOfCols];
            for(int i =1; i<noOfRows;i++){
                for(int j=0;j<noOfCols;j++){
                    row = sh.getRow(i);
                    cell= row.getCell(j);
                    DataFormatter formatter = new DataFormatter();
                    String cellLettura = formatter.formatCellValue(cell);
//                    String cellLettura = String.valueOf(cell.getStringCellValue());
                    data[i-1][j] = cellLettura;

                }
            }
        }
        catch (Exception e) {
            System.out.println("The exception is: " +e.getMessage());
        }
        return data;
    }

}
-------------------------------------------------------

class test @factory e multi @test

public class RicercaBenestareBCBEDataProvider extends BaseTest {

public String BCBE;
public String TEST1;
public String TEST2;
protected static ConfigurationProperties config;

protected static By versione = By.xpath("//div[@class='container-fluid']//strong[1]");
public static long IDSessione;

// public RicercaBenestareBCBEDataProvider(String BCBE,String TEST1) {
// this.BCBE=BCBE;
// this.TEST1=TEST1;
// }
public RicercaBenestareBCBEDataProvider(String BCBE,String TEST1,String TEST2) {
this.BCBE=BCBE;
this.TEST1=TEST1;
this.TEST2=TEST2;
}

@Factory(dataProvider = "DatiInput",dataProviderClass =  InputDataProvider.class)
static Object [] factorymethod (String BCBE,String TEST1,String TEST2)
{
BCBE=BCBE;
TEST1=TEST1;
TEST2=TEST2;
return new Object[] { new RicercaBenestareBCBEDataProvider(BCBE,TEST1,TEST2) } ;
}

@Test (groups = "windows.checkintest", description = "BenestareBCBE")
public void TestLetturaDati(ITestContext context) throws InterruptedException {
System.out.println(BCBE+" "+TEST1+" "+TEST2);
System.out.println(BrowserName);
System.out.println(System.currentTimeMillis());
IDSessione = System.currentTimeMillis();
System.out.println(IDSessione);
context.setAttribute("IdSessioneTest",IDSessione);
}

@Test (groups = "windows.checkintest" ,dependsOnMethods = "TestLetturaDati", description = "BenestareBCBE")
public void TestFindBCBE() throws InterruptedException {
System.out.println(IDSessione);
SeleniumBCBE = new SeleniumBCBE(driver);
config = new ConfigurationProperties();
SeleniumBCBE.ClickBenestare();
SeleniumBCBE.ClickRicercaBenestare();
//SeleniumBCBE.ClickCampoCodiceBCBE (config.getData("codiceBCBE2"));
SeleniumBCBE.ClickCampoCodiceBCBE(BCBE);
SeleniumBCBE.ClickSubmit();
}
@Test (groups = "windows.checkintest",dependsOnMethods = "TestFindBCBE", description = "VerificaBenestareBCBE")
public void TestVerifySearch () {
System.out.println(IDSessione);
SeleniumBCBERicercaBenestarePage = new SeleniumBCBERicercaBenestarePage(driver);
Assert.assertTrue(SeleniumBCBERicercaBenestarePage.VerificaTabella()>=1);
Assert.assertTrue(SeleniumBCBERicercaBenestarePage.VerificaNumeroBCBE(BCBE));
}

}


Thanks 

⇜Krishnan Mahadevan⇝

unread,
Mar 29, 2021, 6:42:53 AM3/29/21
to testng-users
I spent some more time taking a closer look at this.

Unfortunately this is not a straight forward use case because:

  • TestNG uses a Directed Acyclic Graph to build the execution model.
  • Now within the DAG, TestNG always keeps trying to retrieve the next free node that can be picked up for execution.
  • In the case of a Factory driven by a data provider, each of the nodes would be a free node in the DAG and as such any of them can be retrieved to be executed next.
So in a nutshell, your use case is not something that TestNG supports at the moment, to the best of my knowledge.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/

Fabio Giusti

unread,
Mar 29, 2021, 7:17:37 AM3/29/21
to testng-users
thanks for the answer and help
Reply all
Reply to author
Forward
0 new messages