Dictionary Compare in C# for Selenium
Dictionary compare can be used to compare two set of data in C# and it’s often used by Automation testers
public static Boolean CompareDictionary(Dictionary<string, Dictionary<string, object>> D1, Dictionary<string, Dictionary<string, object>> D2) { var dic1a=new Dictionary<string, object>(); if (D1 == null && D2 == null) return true; else if (D1 == null || D2 == null) return false; if (D1.Count != D2.Count) return false; if (D1.Keys.Except(D2.Keys).Any()) return false; if (D2.Keys.Except(D1.Keys).Any()) return false; foreach (string Key in D1.Keys) { if (!D2.ContainsKey(Key)) return false; foreach(var subkeys in D1[Key].Keys) { var a1 = ((D1[Key][subkeys]).ToString()).Trim(); var a2 = ((D2[Key][subkeys]).ToString()).Trim(); if(a1.Equals(a2)) { } else { return false; } } } return true; }
All the testers have to do is prepare two set of dictionaries one is Expected and another one is Actual