Home > Net >  Update .property at the runtime with commons.configuration2
Update .property at the runtime with commons.configuration2

Time:02-10

I have test case where I wanna run API request, get token and set it as variable in property file, after that use it in following API requests in the same test case. I'm trying to use apache.commons.configurations2. Framework based on Selenium, TestNG, RestTemplate ... Example of test case

 @Test(groups = { }, description = "", priority = )
     public void testSmthng() {
       variables...
       class instances ....
 
       getToken()  // API request to get token 

       ^^ insige method I use saveConfigurationProperty(new AuthApi().getAuthToken(requestProcessor));

       createCustomer() // API request to create customer where I wanna use token ^^ 

===================================================================
public void saveConfigurationProperty(String configProperty) {

        Parameters params = new Parameters();
        File propertiesFile = new File("src/main/resources/settings.properties");

        ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> builder =
                new ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class)
                        .configure(params.properties().setFile(propertiesFile));
//        builder.setAutoSave(true);

        builder.addEventListener(ConfigurationBuilderEvent.CONFIGURATION_REQUEST, new EventListener<ConfigurationBuilderEvent>() {
            @Override
            public void onEvent(ConfigurationBuilderEvent event) {
                if(builder.getReloadingController().checkForReloading(null)) {
                    log.info("Reloading config ....");
                }
            }
        });

        try {
            Configuration config = builder.getConfiguration();
            config.setProperty("apiBearerToken", configProperty);
            builder.save();
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
    }

My question is: How to update settings.property file where I store env variables dynamically during test execution?

CodePudding user response:

  •  Tags:  
  • Related