How to convert specflow or cucumber example into keyvaluepair

35 views Asked by At

How to get selenium example as key value so that I can execute step based on example

2

There are 2 answers

3
Mohit Sharma On
public IDictionary<string,string> ConvertExampleToDic()
{
    Dictionary<string, string> keyval = new Dictionary<string, string>();
    List<string> values = new List<string>();

    foreach (var value in _scenarioContext.ScenarioInfo.Arguments.Values)
    {
       values.Add(value.ToString());
    }
    List<string> keys = new List<string>();
    foreach (var key in _scenarioContext.ScenarioInfo.Arguments.Keys)
    {
        keys.Add(key.ToString());
    }

    for(int i=0; i< keys.Count;i++)
    {
        keyval.Add(keys[i], values[i]);
    }
    return keyval;
}
0
Greg Burghardt On

The ScenarioInfo.Arguments property is an instance of IOrderedDictionary where the keys and values are of type object. This should practically be a one-liner to convert this to IDictionary<string, string>:

var scenarioArgs = _scenarioContext.ScenarioInfo.Arguments;
var data = scenarioArgs.ToDictionary(item => item.Key.ToString(), item => item.Value.ToString());