简体   繁体   中英

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

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 ie 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" });    

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") }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM