public void FindElementTwoBy()
        {
            var driver = new Mock <IAllDriver>();

            var elem1 = new Mock <IAllElement>();
            var elem2 = new Mock <IAllElement>();
            var elem3 = new Mock <IAllElement>();
            var elems12 = new List <IWebElement> {
                elem1.Object, elem2.Object
            }.AsReadOnly();
            var elems23 = new List <IWebElement> {
                elem2.Object, elem3.Object
            }.AsReadOnly();

            driver.Setup(_ => _.FindElements(by.Mechanism, by.Criteria)).Returns(elems12);
            driver.Setup(_ => _.FindElements(by2.Mechanism, by2.Criteria)).Returns(elems23);

            var byAll = new ByAll(by, by2);

            // findElement
            Assert.That(byAll.FindElement(driver.Object), Is.EqualTo(elem2.Object));

            //findElements
            var result = byAll.FindElements(driver.Object);

            Assert.That(result.Count, Is.EqualTo(1));
            Assert.That(result[0], Is.EqualTo(elem2.Object));

            driver.Verify(_ => _.FindElements(by.Mechanism, by.Criteria), Times.AtLeastOnce);
            driver.Verify(_ => _.FindElements(by2.Mechanism, by2.Criteria), Times.AtLeastOnce);
        }
        public void FindElementDisjunct()
        {
            var driver = new Mock <IAllDriver>();

            var elem1 = new Mock <IAllElement>();
            var elem2 = new Mock <IAllElement>();
            var elem3 = new Mock <IAllElement>();
            var elem4 = new Mock <IAllElement>();
            var elems12 = new List <IWebElement> {
                elem1.Object, elem2.Object
            }.AsReadOnly();
            var elems34 = new List <IWebElement> {
                elem3.Object, elem4.Object
            }.AsReadOnly();

            driver.Setup(_ => _.FindElements(by.Mechanism, by.Criteria)).Returns(elems12);
            driver.Setup(_ => _.FindElements(by2.Mechanism, by2.Criteria)).Returns(elems34);

            var byAll = new ByAll(by, by2);

            Assert.Throws <NoSuchElementException>(() => byAll.FindElement(driver.Object));

            var result = byAll.FindElements(driver.Object);

            Assert.That(result.Count, Is.EqualTo(0));
            driver.Verify(_ => _.FindElements(by.Mechanism, by.Criteria), Times.AtLeastOnce);
            driver.Verify(_ => _.FindElements(by2.Mechanism, by2.Criteria), Times.AtLeastOnce);
        }
        public void FindElementTwoBy()
        {
            var driver = new Mock <IAllDriver>();

            var elem1 = new Mock <IAllElement>();
            var elem2 = new Mock <IAllElement>();
            var elem3 = new Mock <IAllElement>();
            var elems12 = new List <IWebElement> {
                elem1.Object, elem2.Object
            }.AsReadOnly();
            var elems23 = new List <IWebElement> {
                elem2.Object, elem3.Object
            }.AsReadOnly();

            driver.Setup(_ => _.FindElementsByName(It.Is <string>(x => x == "cheese"))).Returns(elems12);
            driver.Setup(_ => _.FindElementsByName(It.Is <string>(x => x == "photo"))).Returns(elems23);

            var by = new ByAll(By.Name("cheese"), By.Name("photo"));

            // findElement
            Assert.That(by.FindElement(driver.Object), Is.EqualTo(elem2.Object));

            //findElements
            var result = by.FindElements(driver.Object);

            Assert.That(result.Count, Is.EqualTo(1));
            Assert.That(result[0], Is.EqualTo(elem2.Object));

            driver.Verify(_ => _.FindElementsByName("cheese"), Times.AtLeastOnce);
            driver.Verify(_ => _.FindElementsByName("photo"), Times.AtLeastOnce);
        }
        public void FindElementDisjunct()
        {
            var driver = new Mock <IAllDriver>();

            var elem1 = new Mock <IAllElement>();
            var elem2 = new Mock <IAllElement>();
            var elem3 = new Mock <IAllElement>();
            var elem4 = new Mock <IAllElement>();
            var elems12 = new List <IWebElement> {
                elem1.Object, elem2.Object
            }.AsReadOnly();
            var elems34 = new List <IWebElement> {
                elem3.Object, elem4.Object
            }.AsReadOnly();

            driver.Setup(_ => _.FindElementsByName(It.Is <string>(x => x == "cheese"))).Returns(elems12);
            driver.Setup(_ => _.FindElementsByName(It.Is <string>(x => x == "photo"))).Returns(elems34);

            var by = new ByAll(By.Name("cheese"), By.Name("photo"));

            Assert.Throws <NoSuchElementException>(() => by.FindElement(driver.Object));

            var result = by.FindElements(driver.Object);

            Assert.That(result.Count, Is.EqualTo(0));
            driver.Verify(_ => _.FindElementsByName("cheese"), Times.AtLeastOnce);
            driver.Verify(_ => _.FindElementsByName("photo"), Times.AtLeastOnce);
        }
        public void FindElementZeroBy()
        {
            var driver = new Mock <IAllDriver>();

            var byAll = new ByAll();

            Assert.Throws <NoSuchElementException>(() => byAll.FindElement(driver.Object));
            Assert.That(byAll.FindElements(driver.Object), Is.EqualTo(new List <IWebElement>().AsReadOnly()));
        }
Exemple #6
0
        /// <summary>
        /// Creates a list of <see cref="By"/> locators based on the attributes of this member.
        /// </summary>
        /// <param name="member">The <see cref="MemberInfo"/> containing information about
        /// the member of the Page Object class.</param>
        /// <returns>A list of <see cref="By"/> locators based on the attributes of this member.</returns>
        protected static ReadOnlyCollection <By> CreateLocatorList(MemberInfo member)
        {
            if (member == null)
            {
                throw new ArgumentNullException(nameof(member), "member cannot be null");
            }

            var  useSequenceAttributes = Attribute.GetCustomAttributes(member, typeof(FindsBySequenceAttribute), true);
            bool useSequence           = useSequenceAttributes.Length > 0;

            var  useFindAllAttributes = Attribute.GetCustomAttributes(member, typeof(FindsByAllAttribute), true);
            bool useAll = useFindAllAttributes.Length > 0;

            if (useSequence && useAll)
            {
                throw new ArgumentException("Cannot specify FindsBySequence and FindsByAll on the same member");
            }

            List <By> bys        = new List <By>();
            var       attributes = Attribute.GetCustomAttributes(member, typeof(AbstractFindsByAttribute), true);

            if (attributes.Length > 0)
            {
                Array.Sort(attributes);
                foreach (var attribute in attributes)
                {
                    var castedAttribute = (AbstractFindsByAttribute)attribute;

                    var findsByAttribute = attribute as FindsByAttribute;
                    if (findsByAttribute != null && findsByAttribute.Using == null)
                    {
                        findsByAttribute.Using = member.Name;
                    }

                    bys.Add(castedAttribute.Finder);
                }

                if (useSequence)
                {
                    ByChained chained = new ByChained(bys.ToArray());
                    bys.Clear();
                    bys.Add(chained);
                }

                if (useAll)
                {
                    ByAll all = new ByAll(bys.ToArray());
                    bys.Clear();
                    bys.Add(all);
                }
            }

            return(bys.AsReadOnly());
        }
        public void FindElementOneByEmpty()
        {
            var driver = new Mock <IAllDriver>();
            var empty  = new List <IWebElement>().AsReadOnly();

            driver.Setup(_ => _.FindElements(by.Mechanism, by.Criteria)).Returns(empty);

            var byAll = new ByAll(by);

            // one element
            Assert.Throws <NoSuchElementException>(() => byAll.FindElement(driver.Object));
            Assert.That(byAll.FindElements(driver.Object), Is.EqualTo(empty));

            driver.Verify(_ => _.FindElements(by.Mechanism, by.Criteria), Times.AtLeastOnce);
        }
        public void FindElementOneByEmpty()
        {
            var driver = new Mock <IAllDriver>();
            var empty  = new List <IWebElement>().AsReadOnly();

            driver.Setup(_ => _.FindElementsByName(It.Is <string>(x => x == "cheese"))).Returns(empty);

            var by = new ByAll(By.Name("cheese"));

            // one element
            Assert.Throws <NoSuchElementException>(() => by.FindElement(driver.Object));
            Assert.That(by.FindElements(driver.Object), Is.EqualTo(empty));

            driver.Verify(_ => _.FindElementsByName("cheese"), Times.AtLeastOnce);
        }
        public void FindElementOneBy()
        {
            var driver = new Mock <IAllDriver>();
            var elem1 = new Mock <IAllElement>();
            var elem2 = new Mock <IAllElement>();
            var elems12 = new List <IWebElement> {
                elem1.Object, elem2.Object
            }.AsReadOnly();

            driver.Setup(_ => _.FindElements(by.Mechanism, by.Criteria)).Returns(elems12);
            var byAll = new ByAll(by);

            // findElement
            Assert.That(elem1.Object, Is.EqualTo(byAll.FindElement(driver.Object)));
            //findElements
            Assert.That(byAll.FindElements(driver.Object), Is.EqualTo(elems12));

            driver.Verify(_ => _.FindElements(by.Mechanism, by.Criteria), Times.AtLeastOnce);
        }
        public void FindElementOneBy()
        {
            var driver = new Mock <IAllDriver>();
            var elem1 = new Mock <IAllElement>();
            var elem2 = new Mock <IAllElement>();
            var elems12 = new List <IWebElement> {
                elem1.Object, elem2.Object
            }.AsReadOnly();

            driver.Setup(_ => _.FindElementsByName(It.Is <string>(x => x == "cheese"))).Returns(elems12);
            var by = new ByAll(By.Name("cheese"));

            // findElement
            Assert.AreEqual(by.FindElement(driver.Object), elem1.Object);
            //findElements
            Assert.That(by.FindElements(driver.Object), Is.EqualTo(elems12));

            driver.Verify(_ => _.FindElementsByName("cheese"), Times.AtLeastOnce);
        }