javaselenium-webdrivertestng

Setting global variables at runtime for TestNG Suite?


I have written a TestNG Suite in Java that will be ran on another system using a test account. The credentials for this account are stored in a password vault and are obtained through an API. Each of the tests requires this login information, and every test class will call the password vault API. I have this as a step in the setup method of each test class.

I would like to reduce it to just a single API call if possible. Would it be possible to obtain the credentials at the beginning of the suite and pass those to each of the test classes?

I looked at this as a potential solution: How to add parameter at runtime in specific test

I would prefer to not modify the XML in this way, as I found this will expose the login information in TestNG's results report.


Solution

  • Here's how you go about doing it.

        suite.setAttribute("username", 'foo');
        suite.setAttribute("password", "foopassword");
    
    org.testng.ITestResult itr = org.testng.Reporter.getCurrentTestResult();
    org.testng.ISuite currentSuite = itr.getTestContext().getSuite();
    String username = Optional.ofNullable(currentSuite.getAttribute("username"))
            .map(Object::toString)
            .orElseThrow(() -> new IllegalArgumentException("Could not find a valid user name"));
    String password = Optional.ofNullable(currentSuite.getAttribute("password"))
            .map(Object::toString)
            .orElseThrow(() -> new IllegalArgumentException("Could not find a valid password"));