protected By MapFindBy(FindsByAttribute findsBy) { switch (findsBy.How) { case How.Id: return(By.Id(findsBy.Using)); case How.Name: return(By.Name(findsBy.Using)); case How.TagName: return(By.TagName(findsBy.Using)); case How.ClassName: return(By.ClassName(findsBy.Using)); case How.CssSelector: return(By.CssSelector(findsBy.Using)); case How.LinkText: return(By.LinkText(findsBy.Using)); case How.PartialLinkText: return(By.PartialLinkText(findsBy.Using)); case How.XPath: return(By.XPath(findsBy.Using)); default: throw new ArgumentException("Invalid find criteria"); } }
/// <summary> /// Find element by attribute. /// How == Custom is not supported /// </summary> public static IWebElement FindElement(IWebDriver webDriver, FindsByAttribute attr) { By by; switch (attr.How) { case How.ClassName: @by = By.ClassName(attr.Using); break; case How.CssSelector: @by = By.CssSelector(attr.Using); break; case How.Id: @by = By.Id(attr.Using); break; case How.LinkText: @by = By.LinkText(attr.Using); break; case How.Name: @by = By.Name(attr.Using); break; case How.PartialLinkText: @by = By.PartialLinkText(attr.Using); break; case How.TagName: @by = By.TagName(attr.Using); break; case How.XPath: @by = By.XPath(attr.Using); break; default: throw new NotSupportedException(); } return(webDriver.FindElement(@by)); }
private static By CreateByInstance(FindsByAttribute attribute) { switch (attribute.How) { case How.Id: return(By.Id(attribute.Using)); case How.Name: return(By.Name(attribute.Using)); case How.TagName: return(By.TagName(attribute.Using)); case How.ClassName: return(By.ClassName(attribute.Using)); case How.CssSelector: return(By.CssSelector(attribute.Using)); case How.LinkText: return(By.LinkText(attribute.Using)); case How.PartialLinkText: return(By.PartialLinkText(attribute.Using)); case How.XPath: return(By.XPath(attribute.Using)); default: throw new Exception("Invalid locator"); } }
public static By From(FindsByAttribute attribute) { How how = attribute.How; string @using = attribute.Using; switch (how) { case How.Id: return(By.Id(@using)); case How.Name: return(By.Name(@using)); case How.TagName: return(By.TagName(@using)); case How.ClassName: return(By.ClassName(@using)); case How.CssSelector: return(By.CssSelector(@using)); case How.LinkText: return(By.LinkText(@using)); case How.PartialLinkText: return(By.PartialLinkText(@using)); case How.XPath: return(By.XPath(@using)); } throw new ArgumentException(string .Format(CultureInfo.InvariantCulture, "Did not know how to construct How from how {0}, using {1}", new object[] { how, @using })); }
public static By GetLocatorFromFindBy(FindsByAttribute fbAttr) { switch (fbAttr.How) { case How.Id: return(By.Id(fbAttr.Using)); case How.Name: return(By.Name(fbAttr.Using)); case How.ClassName: return(By.ClassName(fbAttr.Using)); case How.CssSelector: return(By.CssSelector(fbAttr.Using)); case How.XPath: return(By.XPath(fbAttr.Using)); case How.TagName: return(By.TagName(fbAttr.Using)); case How.LinkText: return(By.LinkText(fbAttr.Using)); case How.PartialLinkText: return(By.PartialLinkText(fbAttr.Using)); default: return(By.Id("Undefined locator")); } }
private void waitToLoad(FindsByAttribute byAttribute) { By by = null; String how = byAttribute.How.ToString(); switch (how) { case "Name": by = By.Name(byAttribute.Using); break; case "CssSelector": by = By.CssSelector(byAttribute.Using); break; case "ClassName": by = By.ClassName(byAttribute.Using); break; case "Id": by = By.Id(byAttribute.Using); break; } WebDriverWait wait = new WebDriverWait(browser.GetBrowser(), TimeSpan.FromSeconds(30)); wait.Until(ExpectedConditions.ElementIsVisible(by)); }
/// <summary> /// Gets the locator from the attribute. /// </summary> /// <param name="attribute">The attribute.</param> /// <returns>The created locator; otherwise <c>null</c>.</returns> public static By GetLocator(FindsByAttribute attribute) { var how = attribute.How; var usingValue = attribute.Using; switch (how) { case How.Id: return(By.Id(usingValue)); case How.Name: return(By.Name(usingValue)); case How.TagName: return(By.TagName(usingValue)); case How.ClassName: return(By.ClassName(usingValue)); case How.CssSelector: return(By.CssSelector(usingValue)); case How.LinkText: return(By.LinkText(usingValue)); case How.PartialLinkText: return(By.PartialLinkText(usingValue)); case How.XPath: return(By.XPath(usingValue)); default: return(null); } }
private static FindsByAttribute GetFindsByValue(FieldInfo field, FindsByAttribute attribute) { if (attribute.Using != null) { return(attribute); } string xmlPage; string xmlField; if (!string.IsNullOrEmpty(attribute.Page) && !string.IsNullOrEmpty(attribute.Field)) { xmlPage = attribute.Page; xmlField = attribute.Field; } else { xmlPage = _page.GetType().Name; xmlField = field.Name; } attribute = ObjectRepositoryParser.GetLocatorTypeAndValue(xmlPage, xmlField); return(attribute); }
// // Static Methods // public static By From(FindsByAttribute attribute) { How how = attribute.How; string @using = attribute.Using; switch (how) { case How.Id: return(By.Id(@using)); case How.Name: return(By.Name(@using)); case How.TagName: return(By.TagName(@using)); case How.ClassName: return(By.ClassName(@using)); case How.CssSelector: return(By.CssSelector(@using)); case How.LinkText: return(By.LinkText(@using)); case How.PartialLinkText: return(By.PartialLinkText(@using)); case How.XPath: return(By.XPath(@using)); case How.Custom: { if (attribute.CustomFinderType == null) { throw new ArgumentException("Cannot use How.Custom without supplying a custom finder type"); } if (!attribute.CustomFinderType.IsSubclassOf(typeof(By))) { throw new ArgumentException("Custom finder type must be a descendent of the By class"); } ConstructorInfo constructor = attribute.CustomFinderType.GetConstructor(new Type[] { typeof(string) }); if (constructor == null) { throw new ArgumentException("Custom finder type must expose a public constructor with a string argument"); } return(constructor.Invoke(new object[] { @using }) as By); } default: throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Did not know how to construct How from how {0}, using {1}", new object[] { how, @using })); } }
private static By From(FindsByAttribute attribute) { Assembly assembly = Assembly.LoadFrom("WebDriver.Support.dll"); Type seleniumByFactory = assembly.GetType("OpenQA.Selenium.Support.PageObjects.ByFactory"); MethodInfo m = seleniumByFactory.GetMethod("From", new Type[] { typeof(FindsByAttribute) }); return((By)m.Invoke(seleniumByFactory, new object[] { attribute })); }
public static FindsByAttribute GetFindsByAttributeFromField(Type pageObject, string iwebElementFieldName) { FieldInfo fi = pageObject.GetField(iwebElementFieldName); FindsByAttribute attr = (FindsByAttribute)fi.GetCustomAttributes(typeof(FindsByAttribute), false).FirstOrDefault(); return attr; }
public override bool Equals(object obj) { if (obj == null) { return(false); } FindsByAttribute findsByAttribute = obj as FindsByAttribute; return(!(findsByAttribute == null) && findsByAttribute.Priority == this.Priority && !(findsByAttribute.Finder != this.Finder)); }
public void TestLocatorCreateLinkText() { var attribute = new FindsByAttribute { How = How.LinkText, Using = "Hello" }; var locator = NativeAttributeBuilder.GetLocator(attribute); Assert.AreEqual(By.LinkText("Hello"), locator); }
public void TestLocatorCreateCssSelector() { var attribute = new FindsByAttribute { How = How.CssSelector, Using = "btn" }; var locator = NativeAttributeBuilder.GetLocator(attribute); Assert.AreEqual(By.CssSelector("btn"), locator); }
public void TestLocatorCreateTagName() { var attribute = new FindsByAttribute { How = How.TagName, Using = "div" }; var locator = NativeAttributeBuilder.GetLocator(attribute); Assert.AreEqual(By.TagName("div"), locator); }
public void TestLocatorCreateId() { var attribute = new FindsByAttribute { How = How.Id, Using = "Foo" }; var locator = NativeAttributeBuilder.GetLocator(attribute); Assert.AreEqual(By.Id("Foo"), locator); }
public void TestCustomClass() { var attribute = new FindsByAttribute { How = How.Custom, Using = "notsupported" }; var locator = NativeAttributeBuilder.GetLocator(attribute); Assert.AreEqual(null, locator); }
public void TestLocatorCreateXPath() { var attribute = new FindsByAttribute { How = How.XPath, Using = "//tag" }; var locator = NativeAttributeBuilder.GetLocator(attribute); Assert.AreEqual(By.XPath("//tag"), locator); }
public void isLoaded(Type t) { MemberInfo[] members = t.GetMembers(BindingFlags.NonPublic | BindingFlags.Instance); foreach (MemberInfo member in members) { if (member.GetCustomAttributes(typeof(LoadElement), true).Length > 0) { FindsByAttribute findBy = (FindsByAttribute)member.GetCustomAttributes(typeof(FindsByAttribute), true)[0]; waitToLoad(findBy); } } }
public override By BuildBy() { BlockAttribute[] blocks = (BlockAttribute[])htmlElementType.GetCustomAttributes(typeof(BlockAttribute), true); if (blocks.Length > 0) { BlockAttribute block = blocks[0]; FindsByAttribute findsBy = block.Value; return(BuildByFromFindsBy(findsBy)); } throw new HtmlElementsException(string.Format("Cannot determine how to locate instance of {0}", htmlElementType)); }
// // Static Methods // protected static ReadOnlyCollection <By> CreateLocatorList(MemberInfo member) { if (member == null) { throw new ArgumentNullException("member", "memeber cannot be null"); } Attribute[] customAttributes = Attribute.GetCustomAttributes(member, typeof(FindsBySequenceAttribute), true); bool flag = customAttributes.Length > 0; Attribute[] customAttributes2 = Attribute.GetCustomAttributes(member, typeof(FindsByAllAttribute), true); bool flag2 = customAttributes2.Length > 0; if (flag && flag2) { throw new ArgumentException("Cannot specify FindsBySequence and FindsByAll on the same member"); } List <By> list = new List <By> (); Attribute[] customAttributes3 = Attribute.GetCustomAttributes(member, typeof(FindsByAttribute), true); if (customAttributes3.Length > 0) { Array.Sort <Attribute> (customAttributes3); Attribute[] array = customAttributes3; for (int i = 0; i < array.Length; i++) { Attribute attribute = array [i]; FindsByAttribute findsByAttribute = (FindsByAttribute)attribute; if (findsByAttribute.Using == null) { findsByAttribute.Using = member.Name; } list.Add(findsByAttribute.Finder); } if (flag) { ByChained item = new ByChained(list.ToArray()); list.Clear(); list.Add(item); } if (flag2) { ByAll item2 = new ByAll(list.ToArray()); list.Clear(); list.Add(item2); } } return(list.AsReadOnly()); }
private By BuildByFromHtmlElementAttributes(Type type) { FindsByAttribute[] findBys = (FindsByAttribute[])Field.GetCustomAttributes(typeof(FindsByAttribute)); if (findBys.Length > 0) { return(BuildByFromFindsBys(findBys)); } BlockAttribute[] blocks = (BlockAttribute[])type.GetCustomAttributes(typeof(BlockAttribute), true); if (blocks.Length > 0) { BlockAttribute block = blocks[0]; FindsByAttribute findsBy = block.Value; return(BuildByFromFindsBy(findsBy)); } return(BuildByFromDefault()); }
// // Methods // public int CompareTo(object obj) { if (obj == null) { throw new ArgumentNullException("obj", "Object to compare cannot be null"); } FindsByAttribute findsByAttribute = obj as FindsByAttribute; if (findsByAttribute == null) { throw new ArgumentException("Object to compare must be a FindsByAttribute", "obj"); } if (this.Priority != findsByAttribute.Priority) { return(this.Priority - findsByAttribute.Priority); } return(0); }
public static By GetBy(this FindsByAttribute attributr) { var how = attributr.How; var sourseString = attributr.Using; switch (how) { case How.ClassName: { return(By.ClassName(sourseString)); } case How.CssSelector: { return(By.CssSelector(sourseString)); } case How.Id: { return(By.Id(sourseString)); } case How.LinkText: { return(By.LinkText(sourseString)); } case How.Name: { return(By.Name(sourseString)); } case How.PartialLinkText: { return(By.PartialLinkText(sourseString)); } case How.TagName: { return(By.TagName(sourseString)); } case How.XPath: { return(By.XPath(sourseString)); } default: { return(By.XPath(sourseString)); } } }
/// <summary> /// Builds the by. /// </summary> /// <param name="findsBy">The finds by.</param> /// <returns>The value.</returns> protected virtual By BuildBy(FindsByAttribute findsBy) { var how = findsBy.How; var usingStr = findsBy.Using; var customType = findsBy.CustomFinderType; switch (how) { case How.Id: return(By.Id(usingStr)); case How.Name: return(By.Name(usingStr)); case How.TagName: return(By.TagName(usingStr)); case How.ClassName: return(By.ClassName(usingStr)); case How.CssSelector: return(By.CssSelector(usingStr)); case How.LinkText: return(By.LinkText(usingStr)); case How.PartialLinkText: return(By.PartialLinkText(usingStr)); case How.XPath: return(By.XPath(usingStr)); case How.Custom: var ctor = customType.GetConstructor(new[] { typeof(string) }); var finder = ctor.Invoke(new object[] { usingStr }) as By; return(finder); default: throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Did not know how to construct How from how {0}, using {1}", how, usingStr)); } }
private void AssertValidFindBy(FindsByAttribute findBy) { if (findBy.How == How.Custom) { if (findBy.CustomFinderType == null) { throw new ArgumentException( "If you set the 'How' property to 'Custom' value, you must also set 'CustomFinderType'"); } if (!findBy.CustomFinderType.IsSubclassOf(typeof(By))) { throw new ArgumentException("Custom finder type must be a descendent of the 'By' class"); } ConstructorInfo ctor = findBy.CustomFinderType.GetConstructor(new Type[] { typeof(string) }); if (ctor == null) { throw new ArgumentException("Custom finder type must expose a public constructor with a string argument"); } } }
internal static By GetByPageFactory(Type type, string property) { // Select a property and grab the [FindsBy()] How and Value FindsByAttribute attr = (FindsByAttribute)type .GetProperty(property) .GetCustomAttributes(false) .First(s => s.GetType() == typeof(FindsByAttribute)); // Return a converted By value for the relevant selector, i.e. [FindsBy(How=How.Id, Using = "Bob")] => By.Id("Bob") switch (attr.How) { case How.Id: return(By.Id(attr.Using)); case How.CssSelector: return(By.CssSelector(attr.Using)); case How.ClassName: return(By.ClassName(attr.Using)); case How.LinkText: return(By.LinkText(attr.Using)); case How.Name: return(By.Name(attr.Using)); case How.PartialLinkText: return(By.PartialLinkText(attr.Using)); case How.TagName: return(By.TagName(attr.Using)); case How.XPath: return(By.XPath(attr.Using)); default: throw new Exception("Selector method not recognised. Did you mean something else?"); } }
private static By CreateFinder(FindsByAttribute findsBy) { var u = findsBy.Using; switch (findsBy.How) { case How.Id: return(By.Id(u)); case How.Name: return(By.Name(u)); case How.TagName: return(By.TagName(u)); case How.ClassName: return(By.ClassName(u)); case How.CssSelector: return(By.CssSelector(u)); case How.LinkText: return(By.LinkText(u)); case How.PartialLinkText: return(By.PartialLinkText(u)); case How.XPath: return(By.XPath(u)); case How.Custom: throw new NotImplementedException("Custom is not supported"); default: throw new ArgumentOutOfRangeException(); } }
/// <summary> /// Waits until element is loaded /// </summary> /// <param name="webDriver">WebDriver instance</param> /// <param name="attr">Locator</param> /// <param name="timeout">Timeout in seconds</param> /// <returns>Loaded IWebElement</returns> public static IWebElement WaitUntilLoaded(IWebDriver webDriver, FindsByAttribute attr, int timeout) { return(new WebDriverWait(webDriver, TimeSpan.FromSeconds(timeout)).Until(w => FindElement(w, attr))); }
protected virtual By BuildByFromFindsBy(FindsByAttribute findsBy) { AssertValidFindBy(findsBy); return(BuildBy(findsBy)); }