private static Dictionary <string, Locator> ReadXml(Stream stream) { const string ELEMENTMAP_MODULE_NAME = "WebElement"; const string ELEMENTMAP_ELEMENT_NAME = "add"; XDocument xdoc = XDocument.Load(stream); Dictionary <string, Locator> dict = new Dictionary <string, Locator>(); foreach (var element in xdoc.Element(ELEMENTMAP_MODULE_NAME).XPathSelectElements("//" + ELEMENTMAP_ELEMENT_NAME)) { if (element.Attribute("key") == null || element.Attribute("value") == null || element.Attribute("type") == null) { throw new ApiException(String.Format("Missing locator key, value or xpath attribute in locator map. source xml: {0}", element.ToString())); } if (String.IsNullOrEmpty(element.Attribute("key").Value) || String.IsNullOrEmpty(element.Attribute("value").Value) || String.IsNullOrEmpty(element.Attribute("type").Value)) { throw new ApiException(String.Format("Empty key, value or xpath in locator map. source xml: {0}", element.ToString())); } if (dict.ContainsKey(element.Attribute("key").Value)) { throw new ApiException(String.Format("Locator key already exists: {0}.", element.Attribute("key").Value)); } dict.Add(element.Attribute("key").Value, new Locator( //need to find out all resource variables LanguageResourceRepository.ReplaceLanguageVariables(element.Attribute("value").Value), EnumHelper.StringToEnum <ByType>(element.Attribute("type").Value) )); } return(dict); }
public static Locator GetVariableLocator(string locatorFormat, ByType locatorType, Hashtable variables) { string locatorValue = locatorFormat; string finalLocatorValue = locatorFormat; foreach (var variableName in variables.Keys) { locatorValue = locatorValue.Replace(Project.VariablePrefix + variableName.ToString(), variables[variableName].ToString()); finalLocatorValue = LanguageResourceRepository.ReplaceLanguageVariables(String.Format(locatorValue, variables[variableName].ToString())); } return(new Locator(finalLocatorValue, locatorType)); //return new Locator(locatorValue, locatorType); }
/// <summary> /// /// </summary> /// <param name="expression"></param> /// <returns></returns> public static string ReplaceLanguageVariables(string expression) { if (expression.IndexOf(Project.LanguagePrefix) < 0) { return(expression); } Regex variableFormat = new Regex(@"\" + Project.LanguagePrefix + @"(\w+(\.{1})?)+"); Match match = variableFormat.Match(expression); if (!match.Success) { return(expression); } foreach (Group matchGroup in match.Groups) { var variable = matchGroup.Value; expression = expression.Replace(variable, LanguageResourceRepository.GetLanguageVariableValue(variable)); } return(expression); }