ConfigurationManager | Pass elements across steps and test cases

A static variable is common to all the instances (or objects) of the class because it is a class-level variable. In other words, you can say that only a single copy of a static variable is created and shared among all the instances of the class. Memory allocation for such variables only happens once, when the class is loaded in memory.

Users often use static variables, driver code references, and element declarations when those elements occur in more than one place. But as we know from the Introduction to Quantum training, static declarations are not working well with Quantum. They are not thread safe and can lead to various issues during parallel execution, such as wrong steps being called, operations that cannot be performed until another operation has not finished, clicking the wrong objects, and so on. 

Therefore, if you need to use the same information in several steps in your Quantum project, you can pass it from one step to another or even from one test case to another through the QAF-provided ConfigurationManager.getBundle(). The ConfigurationManager works similar to HashMap: this bundle stores all your locator files, application properties files, and TestNG parameters during runtime, and you can fetch this information using getString(), getProperty(), and various other ways by using a specific key. In the same fashion, you can store data to the bundle and reach out to it from wherever you need it using .setProperty("key","value"). The value is an object, so you can store everything -- HashMap, an array list, really anything you need.

Important: The only difference between the getProperty() method and getString() method is that getString() automatically type-casts what you retrieve in the getProperty() method.

Watch this short video for a demonstration.


Functions

This section provides information about the relevant functions.

setProperty()

Sets a property. This replaces any previously set values. Set values are implicit calls to clearProperty(key), addProperty(key, value).

  • Overrides: setProperty(...) in AbstractHierarchicalFileConfiguration

  • Parameters: key - The key of the property to change value to the new value.

getProperty()

Gets a property from the configuration. This is the most basic get method for retrieving values of properties. In a typical implementation of the Configuration interface, the other get methods (that return specific data types) internally make use of this method. On this level, variable substitution is not yet performed. The returned object is an internal representation of the property value for the passed in key. It is owned by the Configuration object. So a caller should not modify this object. It cannot be guaranteed that this object will stay constant over time (meaning that further update operations on the configuration may change its internal state).

  • Specified by: getProperty(...) in Configuration

  • Overrides: getProperty(...) in HierarchicalConfiguration

  • Parameters: Key property to retrieve

  • Returns: The value to which this configuration maps the specified key, or null if the configuration contains no mapping for this key.

getString()

Gets a string associated with the given configuration key.

Parameters:

key - The configuration key.

Returns:

The associated string.

Throws:

ConversionException - thrown if the key maps to an object that is not a string.

Step-by-step instructions

On the backend, working with your stepFile.java, perform the following procedure.

To pass elements across steps and test cases:

  1. Use setProperty("key","value") to save data to the bundle and fetch it from wherever you need, as follows. Find the best logical place in your code where you can set that data so it will be already available for the steps that will need it.

    Copy
    @When("^I search for \"([^\"]*)\"$")
    public void I_search_for(String searchKey) throws Throwable {
        ConfigurationManager.getBundle().setProperty("theKey", "the value");

        .... step code...
    }
  2. Fetch the data from any of your other steps, as follows.

    Copy
    @When("^I search for \"([^\"]*)\"$")
    public void I_search_for(String searchKey) throws Throwable {
        ConfigurationManager.getBundle().getProperty("theKey");

        .... step code...
    }
  3. Pass data already saved in the bundle from the scenario itself.

    Copy
    Scenario: Search Quantum with results
        Given I am Passing the variable "${theKey}"

    In this case, on background, your step should be prepared to accept the proper value type that you pass from the scenario, be it string, array, or anything else.

    Copy
    public void I_search_for(String theKey)