Saturday, March 15, 2025

Easy methods to load Config.properties file in Cucumber TestRunner.java file?


To run my Selenium/Cucumber java venture, I must move some VM arguments, variables, properties path and so forth… now easiest method is to move it in Eclipse run configuration beneath “VM arguments” tab. I would like to present path of Log4j2.xml, sqlJdbc driver, any UserID and so forth.

Now, if i put all this in a properties file after which run the “TestRunner’, then how am i able to be certain that properties are loaded earlier than the Options run. I get NullPointerException.

Am i able to put this in a static block? THis means will it get initialized earlier than any TestNG function runs?

@CucumberOptions(glue = { "stepDefinitions" }, options = {
        "src/check/sources/options/CreateUserProfile.function" }, plugin = { "fairly",
                "json:test-output/JsonReport.json", "html:test-output/HTMLReport.html",
                "com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:", })

public class TestRunner {
    public TestNGCucumberRunner testNGCucumberRunner;

    @BeforeClass(alwaysRun = true)
    public void setUpClass() {
        if (testNGCucumberRunner == null) {
            testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
        }
    }

    @Take a look at(teams = "cucumber situations", description = "Runs Cucumber Situations", dataProvider = "situations")
    public void state of affairs(PickleWrapper pickleEvent, FeatureWrapper cucumberFeature) throws Throwable {
        testNGCucumberRunner.runScenario(pickleEvent.getPickle());
    }

    @DataProvider
    public Object[][] situations() {
        if (testNGCucumberRunner == null) {
            testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
        }
        return testNGCucumberRunner.provideScenarios();
    }

    @AfterClass(alwaysRun = true)
    public void tearDownClass() {
        testNGCucumberRunner.end();

    }

    static {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        System.setProperty("present.date", dateFormat.format(new Date()));
        String propertyFilePath = "C:EclipseWorkspaceAutomationsrcprincipalsourcesconfig.properties";
        BufferedReader reader;
        strive {
            reader = new BufferedReader(new FileReader(propertyFilePath));
            Properties properties = new Properties();
            strive {
                properties.load(reader);
                reader.shut();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException("Config.properties not discovered at " + propertyFilePath);
        }

    }

}

UPDATE:

I get this error …i’ve loaded the properties file within the TestRunner.java file, additionally loaded within the BaseTest.java class the place driver is initialized. Nonetheless i get this error. Please assist. I do not need to use eclipse VM arguments and need my properties to be loaded from the script solely.

java.lang.NullPointerException: Can't invoke "String.equalsIgnoreCase(String)" as a result of the return worth of "java.lang.System.getProperty(String)" is null

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

PHP Code Snippets Powered By : XYZScripts.com