4

Want to take urls one by one at @BeforeClass method and perform action in different @Test methods.

But @dataProvider only can used along with @Test method not with @BeforeClass in TestNG

constraint:-

  • Actually, All @Test method are independent on each other and So cant use single test method.

  • urls in @dataProvider is always be changing. We gets urls in run-time

How could we manage such scenarios ?

In short architecture of framework:-

     @BeforeClass(dataProvider = "getTestUrls") 
        public void testPage(){
            driver.get(testUrls);
       }

      @Test(priority=1)
      @Test(priority=2)

     @DataProvider
        public Object [][] getTestUrls(){
            return new Object[][]   { { 1,"http://www.yahoo.com" }, {2,"http://www.google.com" } };
    }
Rama
  • 815
  • 2
  • 17
  • 37

4 Answers4

2

Use Factory method with dataProvider method , It create a multiple instance.

 @Factory(dataProvider = "getUrls")
        public SEOErrorFactoryResetBeforeClass(String pagUrl) {
            this.pagUrl = pagUrl;
        }
0

These constraints can be overcome by using data driven framework using POI jar files. You can fetch the test urls from a excel file, data provider have some limitations.

Ram Pasala
  • 4,931
  • 3
  • 16
  • 26
  • Fetching is data from excel or array is of same thing. But the my concern here is use of "dataProvider" in "Beforeclass" or "BeforeTest" method – Rama Aug 10 '15 at 09:05
0

Yes , @Factory annotation will solved your concern

@Factory(dataProvider = "getUrls")
        public SEOErrorFactoryResetBeforeClass(String pagUrl) {
            this.pagUrl = pagUrl;
        }
0

Just use

context.getCurrentXmlTest().getParameters();

like this:

  @SuppressWarnings("deprecation")
  @BeforeClass
  public void setUp(ITestContext context) {
  System.out.println(context.getCurrentXmlTest().getParameters());    

  }
Ben Herron
  • 13
  • 5