private static void FinishDecorate(object page, IPageObjectMemberDecorator decorator) { if (decorator is ICustomPageObjectMemberDecorator custom) { custom.FinishDecorate(page); } }
/// <summary> /// Initializes the elements in the Page Object. /// </summary> /// <param name="page">The Page Object to be populated with elements.</param> /// <param name="locator">The <see cref="IElementLocator"/> implementation that /// determines how elements are located.</param> /// <param name="decorator">The <see cref="IPageObjectMemberDecorator"/> implementation that /// determines how Page Object members representing elements are discovered and populated.</param> /// <exception cref="ArgumentException"> /// thrown if a field or property decorated with the <see cref="FindsByAttribute"/> is not of type /// <see cref="IWebElement"/> or IList{IWebElement}. /// </exception> public static void InitElements(object page, IElementLocator locator, IPageObjectMemberDecorator decorator) { if (page == null) { throw new ArgumentNullException(nameof(page), "page cannot be null"); } if (locator == null) { throw new ArgumentNullException(nameof(locator), "locator cannot be null"); } if (decorator == null) { throw new ArgumentNullException(nameof(locator), "decorator cannot be null"); } if (locator.SearchContext == null) { throw new ArgumentException("The SearchContext of the locator object cannot be null", nameof(locator)); } const BindingFlags PublicBindingOptions = BindingFlags.Instance | BindingFlags.Public; const BindingFlags NonPublicBindingOptions = BindingFlags.Instance | BindingFlags.NonPublic; // Get a list of all of the fields and properties (public and non-public [private, protected, etc.]) // in the passed-in page object. Note that we walk the inheritance tree to get superclass members. Type?type = page.GetType(); var members = new List <MemberInfo>(); members.AddRange(type.GetFields(PublicBindingOptions)); members.AddRange(type.GetProperties(PublicBindingOptions)); while (type != null) { members.AddRange(type.GetFields(NonPublicBindingOptions)); members.AddRange(type.GetProperties(NonPublicBindingOptions)); type = type.BaseType; } foreach (var member in members) { // Examine each member, and if the decorator returns a non-null object, // set the value of that member to the decorated object. object?decoratedValue = decorator.Decorate(member, locator); if (decoratedValue == null) { continue; } if (member is FieldInfo field) { field.SetValue(page, decoratedValue); } else if (member is PropertyInfo property && property.CanWrite) { property.SetValue(page, decoratedValue, null); } } }
public void Ctor_ArgumentNullExceptions(IElementLocator elementLocator, IPageObjectMemberDecorator pageObjectMemberDecorator) { new Action(() => new PageObjectFactory(elementLocator, null)).Should() .Throw <ArgumentNullException>(); new Action(() => new PageObjectFactory(null, pageObjectMemberDecorator)).Should() .Throw <ArgumentNullException>(); }
public static void InitElements(object page, IElementLocator locator, IPageObjectMemberDecorator decorator) { if (page == null) { throw new ArgumentNullException("page", "page cannot be null"); } if (locator == null) { throw new ArgumentNullException("locator", "locator cannot be null"); } if (decorator == null) { throw new ArgumentNullException("locator", "decorator cannot be null"); } if (locator.SearchContext == null) { throw new ArgumentException("The SearchContext of the locator object cannot be null", "locator"); } Type type = page.GetType(); List <MemberInfo> list = new List <MemberInfo>(); list.AddRange(type.GetFields(BindingFlags.Instance | BindingFlags.Public)); list.AddRange(type.GetProperties(BindingFlags.Instance | BindingFlags.Public)); while (type != null) { list.AddRange(type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic)); list.AddRange(type.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic)); type = type.BaseType; } foreach (MemberInfo current in list) { object obj = decorator.Decorate(current, locator); if (obj != null) { FieldInfo fieldInfo = current as FieldInfo; PropertyInfo propertyInfo = current as PropertyInfo; if (fieldInfo != null) { fieldInfo.SetValue(page, obj); } else if (propertyInfo != null) { propertyInfo.SetValue(page, obj, null); } } } }
/// <summary> /// Initializes the elements in the Page Object. /// </summary> /// <param name="driver">The driver used to find elements on the page.</param> /// <param name="page">The Page Object to be populated with elements.</param> /// <param name="decorator">The <see cref="IPageObjectMemberDecorator"/> implementation that /// determines how Page Object members representing elements are discovered and populated.</param> /// <exception cref="ArgumentException"> /// thrown if a field or property decorated with the <see cref="FindsByAttribute"/> is not of type /// <see cref="IWebElement"/> or IList{IWebElement}. /// </exception> public static void InitElements(ISearchContext driver, object page, IPageObjectMemberDecorator decorator) { InitElements(page, new DefaultElementLocator(driver), decorator); }
/// <summary> /// Initializes the elements in the Page Object. /// </summary> /// <param name="page">The Page Object to be populated with elements.</param> /// <param name="locator">The <see cref="IElementLocator"/> implementation that /// determines how elements are located.</param> /// <param name="decorator">The <see cref="IPageObjectMemberDecorator"/> implementation that /// determines how Page Object members representing elements are discovered and populated.</param> /// <exception cref="ArgumentException"> /// thrown if a field or property decorated with the <see cref="OpenQA.Selenium.Support.PageObjects.FindsByAttribute"/> is not of type /// <see cref="IWebElement"/> or IList{IWebElement}. /// </exception> public static void InitElements(object page, IElementLocator locator, IPageObjectMemberDecorator decorator) { PageFactory.InitElements(page, locator, decorator); FinishDecorate(page, decorator); }
/// <summary> /// Initializes the elements in the Page Object. /// </summary> /// <param name="driver">The driver used to find elements on the page.</param> /// <param name="page">The Page Object to be populated with elements.</param> /// <param name="decorator">The <see cref="OpenQA.Selenium.Support.PageObjects.IPageObjectMemberDecorator"/> implementation that /// determines how Page Object members representing elements are discovered and populated.</param> /// <exception cref="ArgumentException"> /// thrown if a field or property decorated with the <see cref="OpenQA.Selenium.Support.PageObjects.FindsByAttribute"/> is not of type /// <see cref="IWebElement"/> or IList{IWebElement}. /// </exception> public static void InitElements(object page, WebDriver driver, IPageObjectMemberDecorator decorator) { InitElements(page, new DefaultElementLocator(driver.WrappedDriver), decorator); }
public AbstractPage(BaseDriver baseDriver) { BaseDriver = baseDriver; Logger = BaseDriver.Logger; PageObjectMemberDecorator = new CustomPageObjectMemberDecorator(BaseDriver); }
public extern static void InitElements(object page, IElementLocator locator, IPageObjectMemberDecorator decorator);
public extern static void InitElements(ISearchContext driver, object page, IPageObjectMemberDecorator decorator);
/// <summary> /// Initializes a new instance of the <see cref="PageObjectFactory"/> class. /// </summary> /// <param name="elementLocator">The locator used to locate members</param> /// <param name="pageObjectMemberDecorator">The MemberDecorator to use once members are found</param> /// <exception cref="ArgumentNullException">Thrown when either ElementLocator or PageObjectMemberDecorator are null</exception> public PageObjectFactory(IElementLocator elementLocator, IPageObjectMemberDecorator pageObjectMemberDecorator) { _elementLocator = elementLocator ?? throw new ArgumentNullException(nameof(elementLocator)); _pageObjectMemberDecorator = pageObjectMemberDecorator ?? throw new ArgumentNullException(nameof(pageObjectMemberDecorator)); }
/// <summary> /// Initializes a new instance of the <see cref="PageObjectFactory"/> class. /// </summary> /// <param name="webDriver">The WebDriver used to communicate with</param> public PageObjectFactory(IWebDriver webDriver) { _elementLocator = new DefaultElementLocator(webDriver); _pageObjectMemberDecorator = new ProxyPageObjectMemberDecorator(new DefaultElementActivator(webDriver), this); }