【问题标题】:Passing data using dataprovider in PageObjectModel in TestNG在 TestNG 的 PageObjectModel 中使用 dataprovider 传递数据
【发布时间】:2018-04-16 12:12:40
【问题描述】:

我有一个场景,在我调用一个方法(它有创建工作流的代码 - 在页面 POM 框架中定义)时,我编写了一个通用方法来使用 testNG 中的 dataProvider 从 excel 文件中获取数据

现在我有一个@Test 方法,它执行如下创建工作流的操作

@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");
    }

现在,如果我在创建第一个工作流后运行 @Test 失败,因为 @test 方法会从头开始再次运行,而不是为“createWorkflow 方法”创建多个工作流。

您能否告诉我如何才能实现这一目标或更好的解决方案。

【问题讨论】:

  • 将导航代码移动到 BeforeClass 或 BeforeTest 方法以运行一次。之后运行的任何代码都移至 AfterClass 或 AfterTest...
  • @Grasshopper .....谢谢,它成功了。只是想检查一下这是否是使用 dataProvider 的正确方法。

标签: selenium-webdriver testng dataprovider


【解决方案1】:
      @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");
}

【讨论】:

  • @Pritam .....谢谢,它奏效了。只是想检查一下这是否是使用 dataProvider 的正确方法。
  • @RaghuHoskote :您能否通过单击正确的刻度线来接受答案.. 如果它解决了您的问题
猜你喜欢
  • 1970-01-01
  • 2015-07-08
  • 2021-12-23
  • 2010-10-14
  • 1970-01-01
  • 1970-01-01
  • 2011-05-23
  • 1970-01-01
  • 2019-11-27
相关资源
最近更新 更多