TestNG - @DataProvider Loop

117 views
Skip to first unread message

Rii Rii

unread,
Mar 30, 2020, 10:06:32 AM3/30/20
to testng-users

Capture1.JPG

  • please can someone assist me, I am trying to loop within a dataProvider method, I need to loop depending on the size of bundleValidationSize which is just an integer, so if this is 3, it should execute the test cases once then return here and continue through the loop. Please can someone let me know if this is possible? The reason why i have two return statements is because i cannot only have the one within the loop.
  • Test Case 1-17 are just referencing the actual worksheets contains the keyword driven information to invoke the screenflow actions.
  • This functions as follows: first member it would complete the workflow above in the WebFlowsController worksheet screenshot, for the first member (iteration 1) from testCase 1 to testCaseId 17 . But each member can have 3 different offer types, if count is 1/2/3 it is stored in bundleValidationSize, e.g. Member 1 (OfferType1, OfferType2 then bundleValidateSize = 2), which differs from member to member. So for each offerType I would like to repeat the workflow based on this bundleValidationSize. I hope this makes sense.
  • It should then exit the loop, because the next member in the list will have a different number of offerTypes and then continues through the workflow steps for this member two and so forth. 
The code follows for the data provider function:

@DataProvider(name = "ScreenFlowsFromExcel",indices = {2})
 public static Iterator<Object[]> buildObjectFromExcel() throws Exception {
    Iterator<Object[]> ;
    int i=0;
     while (i<bundleValidationSize) {
         testCaseArray = new TestDataIterator().CreateArrayFromExcel(testDatafilename, "WebFlowsController");
         i+=1;
         return testCaseArray.entrySet().stream().map(input -> new Object[]{input.getKey(), input.getValue()}).iterator();
    }
    return null;
}

Below is the test cases, the it will iterate through the Key Value pairs in the testCaseArray HashMap from TestCase1-17:

Capture4.JPG


What I am trying to achieve is that it should repeat Steps 1-17 from the WebControllers Sheet depending on the BundleValidationSize number.

Krishnan Mahadevan

unread,
Mar 30, 2020, 12:35:17 PM3/30/20
to testng...@googlegroups.com

I didn’t quite understand what you are trying to solve. Can you please help rephrase/elaborate/explain more in a different way ?

 

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 Scribbings @ http://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/93cef56c-3bbd-4de3-8b93-04a6b0910467%40googlegroups.com.

Rii Rii

unread,
Mar 31, 2020, 3:38:03 AM3/31/20
to testng...@googlegroups.com
Hi Krishnan,

Thank you for your email, its much appreciated. 

This is a Hybrid Framework, but I am no expert

So to break it down for you,


The following DataProvider function is responsible for reading this worksheet called WebFlowsController, each test case here from TestCaseId 1-17 points to a worksheet containing the keyword actions.
image.png
@DataProvider(name = "ScreenFlowsFromExcel")

public static Iterator<Object[]> buildObjectFromExcel() throws Exception {
    int i = 0;

while (i < bundleValidationSize) {
testCaseArray = new TestDataIterator().CreateArrayFromExcel(testDatafilename, "WebFlowsController");
i += 1;
return testCaseArray.entrySet().stream().map(input -> new Object[]{input.getKey(), input.getValue()} ).iterator();

}
This test case code below will execute the actions pertaining to Steps 1-17, this is calls the ActionExecutor, which is not important in this case and should'nt change
because it only goes to the worksheet for example "Login" and executes the steps to login to the application and so on, and works as expected.
@Test(dataProvider = "ScreenFlowsFromExcel",dependsOnMethods = { "DatasetFromExcel" })
//Commented out for Test Steps count 31012020
public void ScreenFlowsFromExcel(String testFlowExecution, ArrayList<LocatorModel> testStepsToExecute) throws Exception {
int testStepsEntryCount=0;
testStepsEntryCount = Integer.parseInt(System.getProperty("menu.count"));
testStepsEntryCount = testStepsEntryCount - 1;
for (LocatorModel testStepEntry : testStepsToExecute) {
executionSteps.webActionStepExecutor(mydriver, testStepEntry, testFlowExecution, testStepsEntryCount);
}
}
So this line of code from the data provider function is responsible for mapping each Test Case 1-17 to a k/v pair above in the Picture as it iterates through the WebFlowsController sheet.

>> return   testCaseArray.entrySet().stream().map(input -> new Object[]{input.getKey(), input.getValue()} ).iterator();

But the reason for this  while loop >> while (i < bundleValidationSize) { << is that I want this dataProvider function to loop 3 times or read WebFlowsController 3 more times so depending on 
the BundleValidationSize (this just holds an integer value) if its 1 or 2 or 3, I want it to repeat Test Case IDs 1-17. I want it to come back here after steps 1-17 was completed
and then repeat steps 1-17 again and so on. 


Kindly let me know if this makes sense



⇜Krishnan Mahadevan⇝

unread,
Mar 31, 2020, 9:04:42 AM3/31/20
to testng...@googlegroups.com
Am afraid I still didn't get the problem statement.

Would it be possible for you to create a simple standalone project (please keep it simple without any of the other complexities) that can be used to understand your problem statement better?


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/


Rii Rii

unread,
Mar 31, 2020, 9:13:02 AM3/31/20
to testng-users
Ok will do, I will get this off to you.

Thanks

To unsubscribe from this group and stop receiving emails from it, send an email to testng...@googlegroups.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...@googlegroups.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...@googlegroups.com.

Rii Rii

unread,
Apr 17, 2020, 6:32:40 AM4/17/20
to testng...@googlegroups.com
@Krishnan.

This is a very late response or update to your response, I apologize.

I have managed to resolve this issue recently by using the InvocationCount attribute and this achieves the looping. The only drawback which is not an issue at all is that I have to set the InvocationCount value to the size of the dataset entry, but it works and I am happy with the outcome. I appreciate the help. I noticed another post regarding the invocation count also.

@Test(dataProvider = "ScreenFlowsFromExcel",dependsOnMethods = { "DatasetFromExcel" }, invocationCount=10 )
public void ScreenFlowsFromExcel(String testFlowExecution, ArrayList<LocatorModel> testStepsToExecute) throws Exception {....
This will loop this test case if I wanted to repeat any workflow steps.

Regards,
Rii

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/c0cd6132-a347-40cb-91b4-f3e3a5378079%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages