/// <summary>

        /// Compares the current instance with another object of the same type and returns an

        /// integer that indicates whether the current instance precedes, follows, or occurs

        /// in the same position in the sort order as the other object.

        /// </summary>

        /// <param name="obj">An object to compare with this instance.</param>

        /// <returns>A value that indicates the relative order of the objects being compared. The return value has these meanings:

        /// <list type="table">

        /// <listheader>Value</listheader><listheader>Meaning</listheader>

        /// <item><description>Less than zero</description><description>This instance precedes <paramref name="obj"/> in the sort order.</description></item>

        /// <item><description>Zero</description><description>This instance occurs in the same position in the sort order as <paramref name="obj"/>.</description></item>

        /// <item><description>Greater than zero</description><description>This instance follows <paramref name="obj"/> in the sort order. </description></item>

        /// </list>

        /// </returns>

        public int CompareTo(object obj)

        {
            if (obj == null)

            {
                throw new ArgumentNullException("obj", "Object to compare cannot be null");
            }



            FindsByAttribute other = obj as FindsByAttribute;

            if (other == null)

            {
                throw new ArgumentException("Object to compare must be a FindsByAttribute", "obj");
            }



            // TODO(JimEvans): Construct an algorithm to sort on more than just Priority.

            if (this.Priority != other.Priority)

            {
                return(this.Priority - other.Priority);
            }



            return(0);
        }
        /// <summary>

        /// Determines whether the specified <see cref="object">Object</see> is equal

        /// to the current <see cref="object">Object</see>.

        /// </summary>

        /// <param name="obj">The <see cref="object">Object</see> to compare with the

        /// current <see cref="object">Object</see>.</param>

        /// <returns><see langword="true"/> if the specified <see cref="object">Object</see>

        /// is equal to the current <see cref="object">Object</see>; otherwise,

        /// <see langword="false"/>.</returns>

        public override bool Equals(object obj)

        {
            if (obj == null)

            {
                return(false);
            }



            FindsByAttribute other = obj as FindsByAttribute;

            if (other == null)

            {
                return(false);
            }



            if (other.Priority != this.Priority)

            {
                return(false);
            }



            if (other.Finder != this.Finder)

            {
                return(false);
            }



            return(true);
        }
Exemple #3
0
        /// <summary>

        /// Gets an instance of the <see cref="By"/> class based on the specified attribute.

        /// </summary>

        /// <param name="attribute">The <see cref="FindsByAttribute"/> describing how to find the element.</param>

        /// <returns>An instance of the <see cref="By"/> class.</returns>

        public static By From(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));

            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 ctor = attribute.CustomFinderType.GetConstructor(new Type[] { typeof(string) });

                if (ctor == null)

                {
                    throw new ArgumentException("Custom finder type must expose a public constructor with a string argument");
                }



                By finder = ctor.Invoke(new object[] { usingValue }) as By;

                return(finder);
            }



            throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Did not know how to construct How from how {0}, using {1}", how, usingValue));
        }