Пример #1
0
        /// <summary>
        /// Assigns the element attributes.
        /// </summary>
        /// <param name="control">The control.</param>
        /// <param name="attribute">The attribute.</param>
        /// <param name="nativeAttributes">The native attributes.</param>
        protected override void AssignElementAttributes(IWebElement control, ElementLocatorAttribute attribute, object[] nativeAttributes)
        {
            var proxy = control as WebElement;

            if (proxy == null)
            {
                return;
            }

            // Convert any locator property to "find by" classes
            var locators = attribute != null?LocatorBuilder.GetElementLocators(attribute) : new List <By>();

            // Also try to parse the native attributes
            var nativeItems = nativeAttributes != null?nativeAttributes.OfType <FindsByAttribute>().ToList() : null;

            if (nativeItems != null && nativeItems.Count > 0)
            {
                var localLocators = locators;
                locators.AddRange(nativeItems.Where(a => a.Using != null)
                                  .OrderBy(n => n.Priority)
                                  .Select(NativeAttributeBuilder.GetLocator)
                                  .Where(l => l != null && !localLocators.Any(c => Equals(c, l))));
            }

            locators = locators.Count > 1 ? new List <By> {
                new ByChained(locators.ToArray())
            } : locators;
            proxy.UpdateLocators(locators);
        }
Пример #2
0
        public void TestAttributeWithNoTagNameAndPropertyThrowsAnException()
        {
            var attribute = new ElementLocatorAttribute {
                Type = "submit"
            };

            LocatorBuilder.GetElementLocators(attribute);
        }
Пример #3
0
        public void TestAttributeWithNoTagNameAndPropertyThrowsAnException()
        {
            var attribute = new ElementLocatorAttribute {
                Type = "submit"
            };

            Assert.Throws <ElementExecuteException>(() =>
                                                    LocatorBuilder.GetElementLocators(attribute));
        }
Пример #4
0
        public void TestAttributeWithLinkAreaTagAndUrlReturnsXPathLocator()
        {
            var attribute = new ElementLocatorAttribute {
                TagName = "area", Url = "mylink.htm"
            };

            var resultList = LocatorBuilder.GetElementLocators(attribute);

            Assert.AreEqual(1, resultList.Count);
            var item = resultList.First();

            Assert.AreEqual(By.XPath("//area[@href='mylink.htm']"), item);
        }
Пример #5
0
        public void TestAttributeWithCssClassReturnsCssClassLocator()
        {
            var attribute = new ElementLocatorAttribute {
                Class = ".something"
            };

            var resultList = LocatorBuilder.GetElementLocators(attribute);

            Assert.AreEqual(1, resultList.Count);
            var item = resultList.First();

            Assert.AreEqual(By.ClassName(".something"), item);
        }
Пример #6
0
        public void TestAttributeWithLinkTextReturnsLinkTextLocator()
        {
            var attribute = new ElementLocatorAttribute {
                Text = "Hello World"
            };

            var resultList = LocatorBuilder.GetElementLocators(attribute);

            Assert.AreEqual(1, resultList.Count);
            var item = resultList.First();

            Assert.AreEqual(By.LinkText("Hello World"), item);
        }
Пример #7
0
        public void TestAttributeWithIdReturnsCssSelectorLocator()
        {
            var attribute = new ElementLocatorAttribute {
                CssSelector = "div#MyId"
            };

            var resultList = LocatorBuilder.GetElementLocators(attribute);

            Assert.AreEqual(1, resultList.Count);
            var item = resultList.First();

            Assert.AreEqual(By.CssSelector("div#MyId"), item);
        }
Пример #8
0
        public void TestAttributeWithNameReturnsNameLocator()
        {
            var attribute = new ElementLocatorAttribute {
                Name = "MyName"
            };

            var resultList = LocatorBuilder.GetElementLocators(attribute);

            Assert.AreEqual(1, resultList.Count);
            var item = resultList.First();

            Assert.AreEqual(By.Name("MyName"), item);
        }
Пример #9
0
        public void TestAttributeWithTagNameAndTypeAndIndexReturnsComplexXPathLocator()
        {
            var attribute = new ElementLocatorAttribute {
                TagName = "input", Type = "email", Index = 1
            };

            var resultList = LocatorBuilder.GetElementLocators(attribute);

            Assert.AreEqual(1, resultList.Count);
            var item = resultList.First();

            Assert.AreEqual(By.XPath("(//input[@type='email'])[0]"), item);
        }
Пример #10
0
        public void TestAttributeWithTagNameAndTypeAndTitleReturnsCompoundXPathLocator()
        {
            var attribute = new ElementLocatorAttribute {
                TagName = "input", Type = "email", Title = "my title"
            };

            var resultList = LocatorBuilder.GetElementLocators(attribute);

            Assert.AreEqual(1, resultList.Count);
            var item = resultList.First();

            Assert.AreEqual(By.XPath("//input[@title='my title' and @type='email']"), item);
        }
Пример #11
0
        public void TestAttributeWithImageTagAndUrlReturnsXPathLocator()
        {
            var attribute = new ElementLocatorAttribute {
                TagName = "img", Url = "myimage.png"
            };

            var resultList = LocatorBuilder.GetElementLocators(attribute);

            Assert.AreEqual(1, resultList.Count);
            var item = resultList.First();

            Assert.AreEqual(By.XPath("//img[@src='myimage.png']"), item);
        }
Пример #12
0
        public void TestAttributeWithIdReturnsIdLocator()
        {
            var attribute = new ElementLocatorAttribute {
                Id = "MyId"
            };

            var resultList = LocatorBuilder.GetElementLocators(attribute);

            Assert.AreEqual(1, resultList.Count);
            var item = resultList.First();

            Assert.AreEqual(By.Id("MyId"), item);
        }
Пример #13
0
        public void TestAttributeWithXPathReturnsXPathLocator()
        {
            var attribute = new ElementLocatorAttribute {
                XPath = ".//[@element='row']"
            };

            var resultList = LocatorBuilder.GetElementLocators(attribute);

            Assert.AreEqual(1, resultList.Count);
            var item = resultList.First();

            Assert.AreEqual(By.XPath(".//[@element='row']"), item);
        }
Пример #14
0
        public void TestAttributeWithTagNameAndAltReturnsXPathLocator()
        {
            var attribute = new ElementLocatorAttribute {
                TagName = "input", Alt = "test"
            };

            var resultList = LocatorBuilder.GetElementLocators(attribute);

            Assert.AreEqual(1, resultList.Count);
            var item = resultList.First();

            Assert.AreEqual(By.XPath("//input[@alt='test']"), item);
        }
Пример #15
0
        public void TestAttributeWithTagNameReturnsTagLocator()
        {
            var attribute = new ElementLocatorAttribute {
                TagName = "input"
            };

            var resultList = LocatorBuilder.GetElementLocators(attribute);

            Assert.AreEqual(1, resultList.Count);
            var item = resultList.First();

            Assert.AreEqual(By.TagName("input"), item);
        }
Пример #16
0
        /// <summary>
        /// Gets the element locators.
        /// </summary>
        /// <param name="attribute">The attribute.</param>
        /// <returns>The list of locators to use.</returns>
        public static List <By> GetElementLocators(ElementLocatorAttribute attribute)
        {
            var locators = new List <By>(3);

            SetProperty(locators, attribute, a => By.Id(a.Id), a => a.Id != null);
            SetProperty(locators, attribute, a => By.Name(a.Name), a => a.Name != null);
            SetProperty(locators, attribute, a => By.ClassName(a.Class), a => a.Class != null);
            SetProperty(locators, attribute, a => By.LinkText(a.Text), a => a.Text != null);
            SetProperty(locators, attribute, a => By.CssSelector(a.CssSelector), a => a.CssSelector != null);
            SetProperty(locators, attribute, a => By.XPath(a.XPath), a => a.XPath != null);

            var xpathTag = new XPathTag(attribute.NormalizedTagName);

            // Alt attribute.
            SetAttribute(xpathTag, "alt", attribute.Alt);

            // URL for Image and Hyperlink
            SetAttribute(xpathTag, "src", attribute.Url, () => attribute.NormalizedTagName == "img");
            SetAttribute(xpathTag, "href", attribute.Url, () => attribute.NormalizedTagName == "a" || attribute.NormalizedTagName == "area");

            // Value attribute
            SetAttribute(xpathTag, "value", attribute.Value);

            // Title attribute
            SetAttribute(xpathTag, "title", attribute.Title);

            // Type attribute
            SetAttribute(xpathTag, "type", attribute.Type);

            // Index attribute
            if (attribute.Index > 0)
            {
                xpathTag.Index = attribute.Index - 1;
            }

            // Only set tag if xpath is empty
            SetProperty(locators, attribute, a => By.TagName(a.TagName), a => a.TagName != null && !xpathTag.HasData);

            // Add the XPath tag if data exists
            if (xpathTag.HasData)
            {
                if (string.IsNullOrWhiteSpace(xpathTag.TagName))
                {
                    throw new ElementExecuteException("Element Locator contains a locator but is missing a TagName property: {0}", xpathTag.CreateLocator());
                }

                locators.Add(By.XPath(xpathTag.CreateLocator()));
            }

            return(locators);
        }
Пример #17
0
        public void TestAttributeWithIdAndTagNameReturnsTwoLocators()
        {
            var attribute = new ElementLocatorAttribute {
                Id = "MyId", TagName = "a"
            };

            var resultList = LocatorBuilder.GetElementLocators(attribute);

            Assert.AreEqual(2, resultList.Count);

            var item = resultList.First();

            Assert.AreEqual(By.Id("MyId"), item);

            var item2 = resultList.Last();

            Assert.AreEqual(By.TagName("a"), item2);
        }
Пример #18
0
        /// <summary>
        /// Assigns the element attributes.
        /// </summary>
        /// <param name="control">The control.</param>
        /// <param name="attribute">The attribute.</param>
        /// <param name="nativeAttributes">The native attributes.</param>
        protected override void AssignElementAttributes(HtmlControl control, ElementLocatorAttribute attribute, object[] nativeAttributes)
        {
            SetProperty(control.SearchProperties, HtmlControl.PropertyNames.Id, attribute.Id);
            SetProperty(control.SearchProperties, UITestControl.PropertyNames.Name, attribute.Name);
            SetProperty(control.SearchProperties, HtmlControl.PropertyNames.TagName, attribute.TagName);
            SetProperty(control.SearchProperties, HtmlControl.PropertyNames.Type, attribute.Type);

            SetProperty(control.FilterProperties, HtmlControl.PropertyNames.Title, attribute.Title);
            SetProperty(control.FilterProperties, UITestControl.PropertyNames.ClassName, attribute.Class);
            SetProperty(control.FilterProperties, HtmlControl.PropertyNames.ValueAttribute, attribute.Value);

            SetProperty(() => attribute.Index > -1, control.FilterProperties, HtmlControl.PropertyNames.TagInstance, attribute.Index.ToString(CultureInfo.InvariantCulture));

            SetProperty(() => (control is HtmlImage), control.FilterProperties, HtmlImage.PropertyNames.Alt, attribute.Alt);
            SetProperty(() => (control is HtmlImage), control.FilterProperties, HtmlImage.PropertyNames.Src, attribute.Url);

            SetProperty(() => (control is HtmlHyperlink), control.FilterProperties, HtmlHyperlink.PropertyNames.Alt, attribute.Alt);
            SetProperty(() => (control is HtmlHyperlink), control.FilterProperties, HtmlHyperlink.PropertyNames.Href, attribute.Url);

            SetProperty(() => (control is HtmlAreaHyperlink), control.FilterProperties, HtmlAreaHyperlink.PropertyNames.Alt, attribute.Alt);
            SetProperty(() => (control is HtmlAreaHyperlink), control.FilterProperties, HtmlAreaHyperlink.PropertyNames.Href, attribute.Url);

            SetTextLocator(control, attribute.Text);
        }
Пример #19
0
 /// <summary>
 /// Assigns the page element attributes.
 /// </summary>
 /// <param name="control">The control.</param>
 /// <param name="locatorAttribute">The locator attribute.</param>
 protected override void AssignPageElementAttributes(TOutput control, ElementLocatorAttribute locatorAttribute)
 {
     this.AssignElementAttributes(control, locatorAttribute, null);
 }
Пример #20
0
 /// <summary>
 /// Assigns the element attributes.
 /// </summary>
 /// <param name="control">The control.</param>
 /// <param name="attribute">The attribute.</param>
 /// <param name="nativeAttributes">The native attributes.</param>
 protected override void AssignElementAttributes(ITestElement control, ElementLocatorAttribute attribute, object[] nativeAttributes)
 {
 }