Search code examples
selenium-webdrivertestngdataprovider

Passing data using dataprovider in PageObjectModel in TestNG


i have a scenario where in im calling a method(which has code to create workflow - defined in pages POM framework), i have written a generic method to get the data from excel file using dataProvider in testNG

Now i have a @Test method which perform the action of creating the workflow as below

@DataProvider(name="wf")
public static String[][] getExcelData() throws Exception{

    ExcelReader read = new ExcelReader();
        String filePath = "path of excelfile";
      return read.getCellData(filePath, "Sheet1");
    }

@Test(dataProviderClass = ExcelReader.class, dataProvider="wf")
        public void testing(String workflow, String type, String unit){

            System.out.println("-------------Test case started -------------");
            System.out.println("Call to login to the application");
            System.out.println("Navigating to Some Page");
            System.out.println("Navigating to WorkflowPage");

            SampleClass s = new SampleClass();
            s.createWorkflow(workflow,type,unit);

            System.out.println("-----'--------Test case Ended ----------------");
            System.out.println();   

        }

public void createWorkflow(String wf, String wf, String unit){

        System.out.println("Creating WF");

        System.out.println(wf);
        System.out.println(type);
        System.out.println(unit);

        System.out.println("CREATED wf");
    }

now if i run the @Test fails after creating the 1st workflow, becoz the @test method is run again from beginning instead of creating multiple workflow's, for 'createWorkflow method.

Can you let me know how can i achieve this or a better solution.


Solution

  •       @BeforeMethod
           public void beforeMethod(){
               System.out.println("Call to login to the application");
               System.out.println("Navigating to Some Page");
               System.out.println("Navigating to WorkflowPage");
          }
    
        @Test(dataProviderClass = ExcelReader.class, dataProvider="wf")
        public void testing(String workflow, String type, String unit){
    
            System.out.println("-------------Test case started -------------");
    
            SampleClass s = new SampleClass();
            s.createWorkflow(workflow,type,unit);
    
            System.out.println("-----'--------Test case Ended ----------------");
            System.out.println();   
    
        }
    
    public void createWorkflow(String wf, String wf, String unit){
    
        System.out.println("Creating WF");
    
        System.out.println(wf);
        System.out.println(type);
        System.out.println(unit);
    
        System.out.println("CREATED wf");
    }