示例#1
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);
        }
        /// <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 = (By)ctor.Invoke(new object?[] { usingValue });

                return(finder);
            }

            throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Did not know how to construct How from how {0}, using {1}", how, usingValue));
        }
示例#3
0
        public void TestEqualityOfTwoNullInstances()
        {
            FindsByAttribute first  = null;
            FindsByAttribute second = null;

            // Must test order of arguments for overridden operators
            Assert.That(first == second, Is.True);
            Assert.That(first != second, Is.False);
            Assert.That(second == first, Is.True);
            Assert.That(second != first, Is.False);
        }
示例#4
0
        public void TestSameInstanceEquality()
        {
            FindsByAttribute first = new FindsByAttribute()
            {
                How = How.Id, Using = "Test"
            };
            FindsByAttribute second = first;

            Assert.That(first == second, Is.True);
            Assert.That(second == first, Is.True);
            Assert.That(first.Equals(second), Is.True);
            Assert.That(second.Equals(first), Is.True);
            Assert.That(object.ReferenceEquals(first, second), Is.True);
        }
示例#5
0
        public void TestComparison()
        {
            FindsByAttribute first = new FindsByAttribute()
            {
                How = How.Id, Using = "Test", Priority = 1
            };
            FindsByAttribute second = new FindsByAttribute()
            {
                How = How.Id, Using = "Test", Priority = 2
            };

            Assert.Less(first, second);
            Assert.Greater(second, first);
        }
示例#6
0
        public void TestInequalityOfPriority()
        {
            FindsByAttribute first = new FindsByAttribute()
            {
                How = How.Id, Using = "Test", Priority = 1
            };
            FindsByAttribute second = new FindsByAttribute()
            {
                How = How.Id, Using = "Test", Priority = 2
            };

            Assert.That(first.Equals(second), Is.False);
            Assert.That(first == second, Is.False);
            Assert.That(first != second, Is.True);
        }
示例#7
0
        public void TestInequalityOfUsing()
        {
            FindsByAttribute first = new FindsByAttribute()
            {
                How = How.Id, Using = "Hello"
            };
            FindsByAttribute second = new FindsByAttribute()
            {
                How = How.Id, Using = "World"
            };

            Assert.That(first.Equals(second), Is.False);
            Assert.That(first == second, Is.False);
            Assert.That(first != second, Is.True);
        }
示例#8
0
        public void TestInequalityOfHow()
        {
            FindsByAttribute first = new FindsByAttribute()
            {
                How = How.Name, Using = "Test"
            };
            FindsByAttribute second = new FindsByAttribute()
            {
                How = How.Id, Using = "Test"
            };

            Assert.IsFalse(first.Equals(second));
            Assert.IsFalse(first == second);
            Assert.IsTrue(first != second);
        }
示例#9
0
        public void TestInequalityOfNull()
        {
            FindsByAttribute first = new FindsByAttribute()
            {
                How = How.Id, Using = "Test"
            };
            FindsByAttribute second = null;

            Assert.That(first.Equals(second), Is.False);

            // Must test order of arguments for overridden operators
            Assert.That(first == second, Is.False);
            Assert.That(first != second, Is.True);
            Assert.That(second == first, Is.False);
            Assert.That(second != first, Is.True);
        }
示例#10
0
        public void TestEquality()
        {
            FindsByAttribute first = new FindsByAttribute()
            {
                How = How.Id, Using = "Test"
            };
            FindsByAttribute second = new FindsByAttribute()
            {
                How = How.Id, Using = "Test"
            };

            Assert.IsTrue(first.Equals(second));
            Assert.IsFalse(object.ReferenceEquals(first, second));
            Assert.IsTrue(first == second);
            Assert.IsFalse(first != second);
        }
示例#11
0
        /// <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);
        }