简体   繁体   中英

Selenium TestNG @DataProvider runtime value

I am creating a Selenium Framework which uses the POM structure with TestNG. I am using the @DataProvider annotation to pass value to @Test .

My question is if output of one @Test method is required to be used at later @Test method then how will I do that because with data provider I can pass only values that it contains.

OP does not mention language or need for parallel execution, hence this can be easily solved with a static variable. In Java the below should print pass:

public static int outputOfMethodA=0;

@Test
   methodA(){
   outputOfMethodA=outputOfMethodA+1;
}

@Test
methodB(){
   if(outputOfMethodA=1){
   System.out.println("pass!");
   }
   else{system.out.println("fail!")
   }
}

POM he meant was the Page Object Model type of Mapping for his WebElements.

It is always a good practice to loosely couple your Test Methods(hence result of one @Test shouldn't be depending on another @Test).

Still if you want to achieve it: @DataProvider can be used on any number of @Test methods you have in your Test Class. Just define a class level Map and use this among your test.

Sample:

TestingClass{

// define global variable
Map<String,String> resultMap=new HashMap<String, String>();

@DataProvider
public Object{

// some logic for dataProvider
}
@Test(dataProvider="dp")
method1(){

//  your logic
// then put your final result to be used by method2 into appropriate obj               

 resultMap.put("key","value");// assign result to defined global variable

  }

  @Test(dataProvider="dp")
  method2(){

  // use the resultMap to take results from previous method.

  }


  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM