Issue
As IS:
Step 1: I am creating instance of testng in servelt.doPost method.
public void dummyDoPost(){
TestListenerAdapter adapter = new TestListenerAdapter();
testNG = new TestNG();
List<Class> listnerClasses = new ArrayList<Class>();
List<String> suiteNameList = new ArrayList<String>();
Class[] classList = new Class[]{
csvOperation.class
};
listnerClasses.add(straight2bank.csvOperation.class);
testNG.setDefaultSuiteName("suite");
testNG.setListenerClasses(listnerClasses);
testNG.setTestClasses(classList);
testNG.run();
Step 2: I have class created which will read the user choice of platform returned by servelet (Say ios, Android or Chrome).
Pro gram as below. That an another operation
Class B{
public void platformController (Map<String,String> testDataValues){
System.out.println("Platform Controller started.");
String platformToBeExecuted = testDataValues.get("JourneyId");
System.out.println("Journey ID returned to platformController " +platformToBeExecuted);
if(platformToBeExecuted.contains("chrome")){
System.out.println("Platform to be executed: Chrome");
System.setProperty("webdriver.chrome.driver",pathToChromeDriver);
/****
To remove message "You are using an unsupported command-line flag: --ignore-certificate-errors.
Stability and security will suffer."
Add an argument 'test-type'
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
*****/
driver = new ChromeDriver();
driver.get(urlOfApplication);
System.out.println("3");
}else if(platformToBeExecuted.contains("ie")){
System.setProperty("webdriver.ie.driver", pathToIEDriver);
driver = new InternetExplorerDriver();
driver.get(urlOfApplication);
System.out.println("2");
}else if(platformToBeExecuted.contains("iOS")){
System.out.println("Platform to be executed: iOS");
System.out.println("Platform to be executed: iOS");
suites.add(".//iostestng.xml");<-----------------
dummyServletClass.testNG.setTestSuites(suites);
dummyServletClass.testNG.run();
}
SO here I have execute the iosTestng.xml using testng.
To do this :-
1) Do I have to declare testng
as static in servelt
class and use the same here ?
2) Do I need to create an another instance for testng
in class B
?
3) Is there any way to pass an argument constructor in setTestClasses
?
I'm confused, as we may be working on to run the program in parallel on a long run.
Solution
If every POST call basically represents the intention of an end-user to run some tests, then I would suggest that you resort to creating a TestNG instance per invocation. That way, you would be able to isolate the test results etc., for every invocation
1) Do I have to declare testng as static in servelt class and use the same here ?
No don't do that. You will end up causing race conditions. You should instead be declaring TestNG object as a local data member in your POST method implementation. If you don't want your POST call to be a blocking call, you can basically have your POST call create a request to run some tests into a queue and you can have a polling mechanism driven by a separate thread that feeds off of the queue and runs them using TestNG.
2) Do I need to create an another instance for testng in class B?
Yes you would need to. Essentially the idea here is to localise the TestNG instance to the set of tests that it is executing, so that there is no overlapping of results, listener invocations etc.,.
3) Is there any way to pass an argument constructor in setTestClasses?
I didn't quite understand this question. What do you mean by this? Please elaborate.
Answered By - Krishnan Mahadevan
Answer Checked By - Mildred Charles (JavaFixing Admin)