Exemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="WebElementProxy"/> class.
 /// </summary>
 /// <param name="searchContext">The driver used to search for elements.</param>
 /// <param name="bys">The list of methods by which to search for the element.</param>
 /// <param name="cache"><see langword="true"/> to cache the lookup to the element; otherwise, <see langword="false"/>.</param>
 /// <param name="locatorFactory">The <see cref="IElementLocatorFactory"/> implementation that
 /// determines how elements are located.</param>
 internal WebElementProxy(ISearchContext searchContext, IEnumerable<By> bys, bool cache, IElementLocatorFactory locatorFactory)
 {
     this.locatorFactory = locatorFactory;
     this.searchContext = searchContext;
     this.bys = bys;
     this.cache = cache;
 }
Exemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="WebElementListProxy"/> class.
 /// </summary>
 /// <param name="searchContext">The driver used to search for elements.</param>
 /// <param name="bys">The list of methods by which to search for the elements.</param>
 /// <param name="cache"><see langword="true"/> to cache the lookup to the element; otherwise, <see langword="false"/>.</param>
 /// <param name="locatorFactory">The <see cref="IElementLocatorFactory"/> implementation that
 /// determines how elements are located.</param>
 internal WebElementListProxy(ISearchContext searchContext, IEnumerable <By> bys, bool cache, IElementLocatorFactory locatorFactory)
 {
     this.locatorFactory = locatorFactory;
     this.searchContext  = searchContext;
     this.bys            = bys;
     this.cache          = cache;
 }
Exemplo n.º 3
0
        public static T InitElements <T>(IWebDriver driver, IElementLocatorFactory locatorFactory)
        {
            if (locatorFactory == null)
            {
                throw new ArgumentNullException("locatorFactory", "locatorFactory cannot be null");
            }

            return(InitElements <T>(locatorFactory.CreateLocator(driver)));
        }
Exemplo n.º 4
0
        public static void InitElements(ISearchContext driver, object page, IElementLocatorFactory locatorFactory)
        {
            if (locatorFactory == null)
            {
                throw new ArgumentNullException("locatorFactory", "locatorFactory cannot be null");
            }

            InitElements(page, locatorFactory.CreateLocator(driver));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Initializes the elements in the Page Object with the given type.
        /// </summary>
        /// <typeparam name="T">The <see cref="Type"/> of the Page Object class.</typeparam>
        /// <param name="driver">The <see cref="IWebDriver"/> instance used to populate the page.</param>
        /// <param name="locatorFactory">The <see cref="IElementLocatorFactory"/> implementation that
        /// determines how elements are located.</param>
        /// <returns>An instance of the Page Object class with the elements initialized.</returns>
        /// <remarks>
        /// The class used in the <typeparamref name="T"/> argument must have a public constructor
        /// that takes a single argument of type <see cref="IWebDriver"/>. This helps to enforce
        /// best practices of the Page Object pattern, and encapsulates the driver into the Page
        /// Object so that it can have no external WebDriver dependencies.
        /// </remarks>
        /// <exception cref="ArgumentException">
        /// thrown if no constructor to the class can be found with a single IWebDriver argument
        /// <para>-or-</para>
        /// if a field or property decorated with the <see cref="FindsByAttribute"/> is not of type
        /// <see cref="IWebElement"/> or IList{IWebElement}.
        /// </exception>
        public static T InitElements <T>(IWebDriver driver, IElementLocatorFactory locatorFactory)
        {
            T               page          = default(T);
            Type            pageClassType = typeof(T);
            ConstructorInfo ctor          = pageClassType.GetConstructor(new Type[] { typeof(IWebDriver) });

            if (ctor == null)
            {
                throw new ArgumentException("No constructor for the specified class containing a single argument of type IWebDriver can be found");
            }

            page = (T)ctor.Invoke(new object[] { driver });
            InitElements(driver, page, locatorFactory);
            return(page);
        }
 /// <summary>
 ///     Initializes a new instance of the <see cref="HtmlElementDecorator" /> class.
 /// </summary>
 /// <param name="locatorFactory">The locator factory.</param>
 public HtmlElementDecorator(IElementLocatorFactory locatorFactory) : base(locatorFactory)
 {
 }
 public DefaultFieldDecorator(IElementLocatorFactory locatorFactory)
 {
     this.factory = locatorFactory;
 }
Exemplo n.º 8
0
        private static object CreateProxyObject(Type memberType, ISearchContext driver, List <By> bys, bool cache, IElementLocatorFactory locatorFactory)
        {
            object proxyObject = null;

            if (memberType == typeof(IList <IWebElement>))
            {
                proxyObject = new WebElementListProxy(driver, bys, cache, locatorFactory);
            }
            else if (memberType == typeof(IWebElement))
            {
                proxyObject = new WebElementProxy(driver, bys, cache, locatorFactory);
            }

            return(proxyObject);
        }
Exemplo n.º 9
0
        /// <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="locatorFactory">The <see cref="IElementLocatorFactory"/> implementation that
        /// determines how elements are located.</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, IElementLocatorFactory locatorFactory)
        {
            if (page == null)
            {
                throw new ArgumentNullException("page", "page cannot be null");
            }

            if (locatorFactory == null)
            {
                throw new ArgumentNullException("locatorFactory", "locatorFactory cannot be null");
            }

            // 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.
            var type    = page.GetType();
            var members = new List <MemberInfo>();
            const BindingFlags PublicBindingOptions = BindingFlags.Instance | BindingFlags.Public;

            members.AddRange(type.GetFields(PublicBindingOptions));
            members.AddRange(type.GetProperties(PublicBindingOptions));
            while (type != null)
            {
                const BindingFlags NonPublicBindingOptions = BindingFlags.Instance | BindingFlags.NonPublic;
                members.AddRange(type.GetFields(NonPublicBindingOptions));
                members.AddRange(type.GetProperties(NonPublicBindingOptions));
                type = type.BaseType;
            }

            // Examine each member, and if it is both marked with an appropriate attribute, and of
            // the proper type, set the member's value to the appropriate type of proxy object.
            foreach (var member in members)
            {
                List <By> bys = CreateLocatorList(member);
                if (bys.Count > 0)
                {
                    bool cache = ShouldCacheLookup(member);

                    object proxyObject = null;
                    var    field       = member as FieldInfo;
                    var    property    = member as PropertyInfo;
                    if (field != null)
                    {
                        proxyObject = CreateProxyObject(field.FieldType, driver, bys, cache, locatorFactory);
                        if (proxyObject == null)
                        {
                            throw new ArgumentException("Type of field '" + field.Name + "' is not IWebElement or IList<IWebElement>");
                        }

                        field.SetValue(page, proxyObject);
                    }
                    else if (property != null)
                    {
                        proxyObject = CreateProxyObject(property.PropertyType, driver, bys, cache, locatorFactory);
                        if (proxyObject == null)
                        {
                            throw new ArgumentException("Type of property '" + property.Name + "' is not IWebElement or IList<IWebElement>");
                        }

                        property.SetValue(page, proxyObject, null);
                    }
                }
            }
        }
 /// <summary>
 ///     Initializes a new instance of the <see cref="DefaultPropertyDecorator" /> class.
 /// </summary>
 /// <param name="locatorFactory">The locator factory.</param>
 public DefaultPropertyDecorator(IElementLocatorFactory locatorFactory)
 {
     Factory = locatorFactory;
 }
Exemplo n.º 11
0
 public RuntimeServices(IElementLocatorFactory locatorFactory)
 => ElementLocatorFactory = locatorFactory;
Exemplo n.º 12
0
 public DefaultFieldDecorator(IElementLocatorFactory locatorFactory)
 {
     this.factory = locatorFactory;
 }
 public HtmlElementDecorator(IElementLocatorFactory locatorFactory)
     : base(locatorFactory)
 {
 }