/// <summary> /// Sets the property of the locator by the filter. /// </summary> /// <param name="tag">The locator collection.</param> /// <param name="attributeName">Name of the attribute.</param> /// <param name="value">The value.</param> /// <param name="filterFunc">The filter function.</param> private static void SetAttribute(XPathTag tag, string attributeName, string value, Func <bool> filterFunc = null) { if (value != null && (filterFunc == null || filterFunc())) { tag.AddAttribute(attributeName, value); } }
/// <summary> /// Gets the element locators. /// </summary> /// <param name="attribute">The attribute.</param> /// <returns>The list of locators to use.</returns> public static List <By> GetElementLocators(ElementLocatorAttribute attribute) { var locators = new List <By>(3); SetProperty(locators, attribute, a => By.Id(a.Id), a => a.Id != null); SetProperty(locators, attribute, a => By.Name(a.Name), a => a.Name != null); SetProperty(locators, attribute, a => By.ClassName(a.Class), a => a.Class != null); SetProperty(locators, attribute, a => By.LinkText(a.Text), a => a.Text != null); SetProperty(locators, attribute, a => By.CssSelector(a.CssSelector), a => a.CssSelector != null); SetProperty(locators, attribute, a => By.XPath(a.XPath), a => a.XPath != null); var xpathTag = new XPathTag(attribute.NormalizedTagName); // Alt attribute. SetAttribute(xpathTag, "alt", attribute.Alt); // URL for Image and Hyperlink SetAttribute(xpathTag, "src", attribute.Url, () => attribute.NormalizedTagName == "img"); SetAttribute(xpathTag, "href", attribute.Url, () => attribute.NormalizedTagName == "a" || attribute.NormalizedTagName == "area"); // Value attribute SetAttribute(xpathTag, "value", attribute.Value); // Title attribute SetAttribute(xpathTag, "title", attribute.Title); // Type attribute SetAttribute(xpathTag, "type", attribute.Type); // Index attribute if (attribute.Index > 0) { xpathTag.Index = attribute.Index - 1; } // Only set tag if xpath is empty SetProperty(locators, attribute, a => By.TagName(a.TagName), a => a.TagName != null && !xpathTag.HasData); // Add the XPath tag if data exists if (xpathTag.HasData) { if (string.IsNullOrWhiteSpace(xpathTag.TagName)) { throw new ElementExecuteException("Element Locator contains a locator but is missing a TagName property: {0}", xpathTag.CreateLocator()); } locators.Add(By.XPath(xpathTag.CreateLocator())); } return(locators); }