I am using TestNG for my selenium tests.
There is a test called “Create Firm” which needs to be run multiple times on my laptop.
So I have written a class called “CreateFirm” for this, and the data for various firms reside in an excel spreadsheet.
At various times, I need to create various sets of firms, which I control using a column in Excel spreadsheet , which holds my computer name.
I use the @Factory to create my “CreateFirm” classes, which has one @Test method to create a Firm.
In excel spreadsheet If i have assigned Firm1,Firm2,Firm3,Firm4 in the same order to my laptop, @Factory creates them in a random order like Firm4,Firm3,Firm1,Firm2
My question is how to get @Factory to create test instances in the order that I want ?
My @Factory method is
@Factory
public Object[] runCreateFirm()
{
//This is where I get the list of test cases assigned to my laptop
this.test_id_list=get_test_ids_for_test_run("Create Firm (class approach).xls", "Global");
Object[] result = new Object[this.test_id_list.size()];
int index=0 ;
for (String firm_id: this.test_id_list)
{
//This is where I get all the test data from the Excel spreadsheet
HashMap<String,String> test_data_row=this.get_row_from_excel("Create Firm (class approach).xls", "Global", "test_case_id", firm_id);
System.out.println("Inside Firm Factory ,index="+index +", test case id="+ test_data_row.get("test_case_id"));
//CreateFirm is the class which will use the data and do all the UI actions to create a Firm
result[index]=new CreateFirm(test_data_row);
index++;
}
return result;
}
XML is
<?xml version="1.0" encoding="UTF-8"?>
<suite name="CreateFirm Suite">
<test name="Create Firm Test" order-by-instances="false">
<classes>
<class name="regressionTests.factory.CreateFirmFactory"/>
</classes>
</test>
</suite>
Managed to write the interceptor method for my requirement.
To start with I introduced a new field in my class called “priority” . I couldnt use @Priority because, my test class had only one test method, in which case all my instantiations will have the same priority.
So I introduced a new field called priority which will have the order in which which the class should be instantiated. I use that field in the interceptor method to set the order of instantiation