vendredi 4 décembre 2015

C# Unit Test: Use Variable from IEnumerable Method

I have adapted a C# test framework that uses a page object pattern. Basically, the test navigates to a site, fills in a contact form using values from a utility class, submits the form and then closes the browser.

There are (3) classes in this test framework to run the test: First is the Test Case Class, next is the Page Class and last is the Utility Class.

The utility class has a IEnumerable Method that lists the U.S. states (code sample below). My end goal is this: Each test execution will select a state value from the IEnumerable Method (starting with 'Alabama' and ending with 'Wyoming'). I already have Dictionary Structures in the utility class to lookup valid city and zip code values for each state value generated. For example, if the generated state is 'Alabama', then the valid city and zip from the utility class would be 'Huntsville' and '35801'. Last, there is a random state generator method that generates a random state value. However, I would like to discontinue using the random state generator method and use state values from the IEnumerable Method.

        public static IEnumerable<string> ListStates()
        {
            var lst = new List<string>();
            lst.Add("Alabama");
            lst.Add("Alaska");
...
            return lst;
        }

In the Test Case Class, there is a for each loop that executes for every state in the ListStates() Method (See code below)

[TestMethod]
public void TestMethod1()
{
    var states = GenerateCityStateZip.ListStates();
    foreach (var state in states)
    {
        Browser.Open();
        Pages.ContactUs.Goto();
        Pages.ContactUs.InputContactDetails();
        Browser.Quit();
    }
}

Last, I have methods in the page class that (1) call the generate random state function in the utility class and (2) associate a valid city and zip code that correspond to this state.

// Generate Random State using GenerateCityStateZip Class
public static string getRandomState()
{
    string randomState = GenerateCityStateZip.GenRandomState();
    return randomState;
}
public string _RandomState = getRandomState();

// Generate City using GenerateCityStateZip Class
public static string getCity(string state)
{
    string city = GenerateCityStateZip.GetCity(state);
    return city;
}

// Generate Zip using GenerateCityStateZip Class
public static string getZip(string state)
{
    string zip = GenerateCityStateZip.GetZip(state);
    return zip;
}

The Issue: In the Page Class, I would like to avoid using the random state generator method for the state value. Instead, I would like to use the IEnumerable Method that the Test Class uses. Can someone suggest code that I need to include in the Page Class to accomplish this? Thanks.

Aucun commentaire:

Enregistrer un commentaire