JD2775
JD2775

Reputation: 3801

Adding dictionary values as a list of strings

I am trying to create a dictionary where the values are stored as a list of strings that I can iterate over. The end goal being something I can mix and match values for passing into parameters in the method, for different Selenium tests. Basically, the Dictionary is for storing test data.

  public void EnterSearchInfo (string username, string cropname, string stationname, string applicationname)
{
    Dictionary<string, List<string>> searchValues = new Dictionary<string, List<string>>();
    searchValues.Add("User", ["USER1", "USER2"]);
    searchValues.Add("Crop", ["Corn", "Soy"]);
    searchValues.Add("Station", ["AA", "JH", "HW"]);


    _driver.FindElement(user).SendKeys(username);
    _driver.FindElement(crop).SendKeys(cropname);
    _driver.FindElement(station).SendKeys(stationname);
    _driver.FindElement(application).SendKeys(applicationname);
    _driver.FindElement(findAssocBtn).Click();
}

The above is not working however, specifically this piece (I am not adding the values correctly):

searchValues.Add("User", ["USER1", "USER2"]);
searchValues.Add("Crop", ["Corn", "Soy"]);
searchValues.Add("Station", ["AA", "JH", "HW"]);

Any ideas? Or if there is a better way to handle this, please let me know

Upvotes: 1

Views: 63

Answers (2)

Reza Biglari
Reza Biglari

Reputation: 74

You're just missing the instantiation for each list element of the dictionary. If you know the values at the time of dictionary creation you can use:

Dictionary<string, List<string>> searchValues = new Dictionary<string, List<string>>
{
    { "User", new List<string>() {"USER1", "USER2" } },
    { "Crop", new List<string>() {"Corn", "Soy" } },
    { "Station", new List<string>() {"AA", "JH", "HW" } }
};

Alternatively, if your values are unknown at time of dictionary creation i.e. when values are retrieved from a text file or database, you can use the Add method from List class.

Dictionary<string, List<string>> searchValues = new Dictionary<string, List<string>>();
searchValues.Add("User", new List<string>() { "USER1", "USER2" });
searchValues.Add("Crop", new List<string>() { "Corn", "Soy" });
searchValues.Add("Station", new List<string>() { "AA", "JH", "HW" });    

Upvotes: 1

Jortega
Jortega

Reputation: 3790

This is what you are looking for GenerareSetLitere:

Dictionary<string, List<Piese>> SetPiese = new Dictionary<string, List<Piese>>
{
    { "User", GenerareSetLitere("USER1", "USER2") },
    { "Crop", GenerareSetLitere("Corn", "Soy") }
    { "Station", GenerareSetLitere("AA", "JH", "HW") }
};

Upvotes: 0

Related Questions