Can we access the twist properties inside the twist scenario workflow

Comments

1 comment

  • Avatar
    Angshuman

    You can autowire the "Environment" to your fixture and get your os and enviroment level properties from it. For example, if you have a Context (or Fixture), naming "Login", you can do like below to get environmental properties like os.arch, os.name etc:


    public class Login {   
        @Autowired
        org.springframework.core.env.Environment env;
        
        public void setUp() throws Exception {
             System.out.println(env.getProperty("os.arch"));
         }

        public void tearDown() throws Exception {
        }

    }


     


    If you are looking to get the properties declared in twist.properties, then you have to do a little bit more. Following is an approach where we have a custom PropertyPlaceholderConfigurer which binds the properties at the context xml level, as well provide a way to read the same properties when it is referred elsewhere.


    ** Declare your Custom property placeholder first like the class below (e.g. com.test.CustomPropertyPlaceHolderConfigurer)


    ------------------------------------------------------------------------------------------------------------------------------------


    package com.test;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Properties;

    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
    import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

    public class CustomPropertyPlaceHolderConfigurer extends
            PropertyPlaceholderConfigurer {

        private int sysPropMode;
        private Map<String, String> propertiesMap;

        @Override
        public void setSystemPropertiesMode(int systemPropertiesMode) {
            super.setSystemPropertiesMode(systemPropertiesMode);
            this.sysPropMode = systemPropertiesMode;
        }
        
        @Override
        protected void processProperties(
                ConfigurableListableBeanFactory beanFactoryToProcess,
                Properties props) throws BeansException {
            super.processProperties(beanFactoryToProcess, props);
            propertiesMap = new HashMap<String, String>();
            for (Object key : props.keySet()) {
                String keyStr = key.toString();
                String valueStr = resolvePlaceholder(keyStr, props, sysPropMode);
                propertiesMap.put(keyStr, valueStr);
            }
        }
        
        public String getPropertyValue(String name) {
            return propertiesMap.get(name);
        }
        
        public List<String> getPropertyNames() {
            ArrayList<String> propertyNames = new ArrayList<String>();
            for (Object key : propertiesMap.keySet()) {
                propertyNames.add(key.toString());
            }
            return propertyNames;
        }
    }


    ---------------------------------------------------------------------------------------------------


    ** change the applicationContext-suite.xml file to refer to your custom property placeholder like below (note, only thing that i have changed is the classname)


    <bean class="com.test.CustomPropertyPlaceHolderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
        <property name="locations">
          <value>classpath:twist.properties</value>
        </property>
    </bean>


    ---------------------------------------------------------------------------------------------------


    ** now you can autowire the custom placeholder to your context or fixture. e.g. in your Login class


    public class Login {   
        @Autowired
       com.test.CustomPropertyPlaceHolderConfigurer propertyConfigurer;
        
        public void setUp() throws Exception {


            System.out.println(propertyConfigurer.getPropertyValue("twist.generate_local_report"));
             for (String property : propertyConfigurer.getPropertyNames()) {
                System.out.println(String.format("Property %s=%s", property, propertyConfigurer.getPropertyValue(property)));
            }
         }

        public void tearDown() throws Exception {
        }

    }


    ---------------------------------------------------------------------------------------------------


     


    Hope this works for you.


    regards

Please sign in to leave a comment.