1

I am trying to implement parallel running TestNG tests with selenium grid, where grid gets dynamic nodes attached. Based on the number of nodes in the grid I want to increase or decrease the number of threads in the TestNG.

Is it possible if so How?

I have tried following cases.

public class SuiteAlterer implements IAlterSuiteListener {

    @Override
    public void alter(List<XmlSuite> suites) {
        // TODO Auto-generated method stub
        System.err.println("**Alter is invoked**");
        int count = NodeInfoFromGrid.getNumberOfNodes();//returns number of grid nodes that are free.
        if (count <= 0) {
            return;
        }
        for (XmlSuite suite : suites) {
            suite.setThreadCount(count);
        }
    }

}

In the Listener OnStart(ITestContext context) added following statement:

context.getSuite().getXmlSuite().setThreadCount(NodeInfoFromGrid.getNumberOfNodes());

But TestNG is not honoring above statement.

Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
  • Did you check this? Adding it in before suite using dependency injected ITestContext? https://stackoverflow.com/questions/26556952/testng-changing-thread-counts-at-runtime – Ahamed Abdul Rahman Sep 24 '19 at 05:39

2 Answers2

1

You cannot do this the way you are attempting. TestNG lets you determine the thread pool size, before starting execution. But once the tests have started executing, you cannot alter the pool size (i.e., increase or decrease it) after the tests have started executing.

You can set the thread pool size via the IAlterSuiteListener implementation, when TestNG starts executing. But after that you cannot change it.

Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
1
@BeforeSuite(alwaysRun = true)
public void beforeSuite(ITestContext context) throws Exception {
    try {

        context.getSuite().getXmlSuite().setThreadCount(3);
        //context.getSuite().getXmlSuite().getVerbose();
    } catch (Exception e) {
        throw e;
    }
}