Monday, 6 March 2017

100 Day Deep Work - Day 4: Configuration Files for Usernames and Passwords




Day 4 of the 100 Day Deep Work challenge actually happened on Friday, but as I don’t do IT stuff at the weekends here’s the posting today!

One thing I’ve been doing for simplicity sake is putting the various usernames and passwords used in tests into its own Class file. As I’m always working on test systems it didn’t seem to be an issue. However, the general convention is that these Usernames and Passwords should be protected to align with the security policy of the company you’re testing for.

That makes sense, after all you might have an Admin user who’s details make it into a live deployment. With tester tests integrated into the same place as unit tests and code, the risk is there. 

So, how to move those details out of the Class file and include them in the Configuration file?

Configuration Settings
In Visual Studio’s ‘Solution Explorer’ you’ll see your project has an app.config file, double click and open it. Inside the configuration elements we can add appSettings and include details of our username and password as a key/value pair.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="AdminUsername" value="testuser" />
    <add key="AdminPassword" value="Password1"/>
  </appSettings>
</configuration>

Naturally a lot more can be added here, such as system configuration details, but for now let’s stick with these two items.

Calling the appSettings items
To call the items from the app.Config file we need to add a call to our test like this:
        LoginCommand
            .LoginAs(ConfigurationManager.AppSettings["AdminUsername"])
            .WithPassword(ConfigurationManager.AppSettings["AdminPassword"]);

In practice however, I’m seeing the norm appears to be to call the AppSettings items via a separate file. I’m not 100% sure why at this point, but I’m assuming this is further abstraction. Moving anything that might change even further up to the page object level and out of the test. For example we might create a separate class to hold the user list for this test

    public class UserList
    {
        public const string TheUsername = "AdminUsername";
        public const string ThePassword = "AdminPassword";

    }

In which case we’d change our test to a more generic:

        LoginCommand
            .LoginAs(ConfigurationManager.AppSettings[UserList.TheUsername])
            .WithPassword(ConfigurationManager.AppSettings[UserList.ThePassword]);

Not a complete understanding but a good start - more tomorrow

0 comments: