Monday, October 15, 2018

Loading Properties file for Test Automation

I am trying to load a simple.properties file into my test suite as soon as I run my test automation.

I tried following tutorial on Apache Commons, but I am missing the configuration2 package. I don't have time to download this and install since I'm working on a secure network.
https://commons.apache.org/proper/commons-configuration/userguide/howto_properties.html

To be exact, I need the configuration2 package for the Parameters class.
https://commons.apache.org/proper/commons-configuration/apidocs/org/apache/commons/configuration2/builder/fluent/Parameters.html

Okay, now what?

Well, let's first use basic Java to load a Properties file and test it.

I can't remember the website where I got this code from, but it is similar to the basic Java tutorial with the exception of using a BufferedReader instead of the InputStreamReader or the like. I added this into my init() method.

private Properties properties;

@Before
public void init() {
    Log.Info("Starting my test automation...");

    BufferedReader reader;
    try {
        reader = new BufferedReader( new FileReader( "src/test/resources/simple.properties"));
        properties = new Properties();
        try {
            properties.load(reader);
            reader.close();
        } catch (IOException ioEx) {
            ioEx.printStackTrace();
        }
    } catch (FileNotFoundException fileEx) {
        fileEx.printStackTrace();
        throw new RuntimeException("simple.properties file not found!");
    }
}

// Note, inside of my simple.properties file is: 
// testword = hello
@Test
public void testPropertiesFilesLoaded() {
    Assert.assertEquals(properties.get("testword"), "hello");
}

I ran this from the command line using Maven.
$ mvn -Dtest=SimpleTestAUT test -pl :app-ui

And the test passed! Yay!

Now, I need to know how to really use this properties file across my test suite. I have a Sample1AUT.java and Sample2AUT.java. In Sample1AUT.java, I have the following.

@BeforeClass
public static void setup() {
    MyProperties.loadProperties("myPropertiesFileName");
}

@Test
public void testPropertiesLoaded() {
    Assert.assertEquals("user", MyProperties.getProperty("biz.username"));
}

This is what my MyProperties class looks like.

public class MyProperties {

  static Properties properties;

  public static void loadProperties(String fileName) {

    BufferedReader reader;

    try {
        reader = new BufferedReader( new FileReader( "src/test/resources/"+fileName+".properties"));
        properties = new Properties();
        try {
            properties.load(reader);
            reader.close();
        } catch (IOException ioEx) {
            ioEx.printStackTrace();
        }
    } catch (FileNotFoundException fileEx) {
        fileEx.printStackTrace();
        throw new RuntimeException(fileName+".properties file not found!");
    }
  }

  public static String getProperty(String key) {
    return (String) properties.get(key);
  }
}

It worked, yay!

What I need to try next is loading Properties file via Krausening in case my "test/resources" folder is not deployed with the WAR including the Test Suite.

References


No comments:

Post a Comment