コード例 #1
0
        public void WhenCallingFindByAllParameters_ShouldReturnElement()
        {
            string html = @"
                <html>
                    <div foo=foo bar=bar> 
                        <span foo=foo bar=bar>text</span>
                        <div foo=foo bar=bar>text</div>
                        <span foo=foo />
                        <span bar=bar />
                        <span foo=foo bar=bar>wrong</span>
                        <span foo=foo bar=bar id='span1'>text</span>
                    </div>
                </html>
                ";

            HtmlElement element = HtmlElement.Create(html);

            HtmlElementFindParams findParams = new HtmlElementFindParams();

            findParams.Attributes.Add("foo", "foo");
            findParams.Attributes.Add("bar", "bar");
            findParams.TagName   = "span";
            findParams.InnerText = "text";
            findParams.Index     = 1;
            Assert.AreEqual("span1", element.ChildElements.Find(findParams).Id);
        }
コード例 #2
0
        public void WhenCallingFindByTagInerTextAndIndex_ShouldReturnTheElement()
        {
            string html = @"
                <html>
                    <div foo='a' id='control1' > 
                        <span id='control2' foo='b'>
                            some text
                        </span>
                    </div>
                    <div id='control3' foo='c'>
                        some text
                    </div>
                    <span id='control4'>
                        some text
                    </span>
                </html>
                ";

            HtmlElement element = HtmlElement.Create(html);

            HtmlElementFindParams findParams = new HtmlElementFindParams();

            findParams.TagName   = "span";
            findParams.InnerText = "some text";
            findParams.Index     = 1;
            Assert.AreEqual("control4", element.ChildElements.Find(findParams).Id);
        }
コード例 #3
0
        public void ConvertObjectToDictionaryReturnsEmptyDictionaryWhenPassedEmptyObject()
        {
            //Arrange & Act
            Dictionary <string, object> d = HtmlElementFindParams.ConvertObjectToDictionary(new { });

            //Assert
            UnitTestAssert.AreEqual(0, d.Count);
        }
コード例 #4
0
        public void CreateWithBasicPropertiesHasProperParamsPopulated()
        {
            //Arrange & Act
            HtmlElementFindParams fp = new HtmlElementFindParams(new { id = "myID", idMatchMethod = MatchMethod.Literal, tagName = "Div", index = 7, innerText = "InnerText" });

            //Assert
            ValidateBaseHtmlElementFindParams(fp, "Div", "InnerText", 7, 0, "myID", MatchMethod.Literal);
        }
コード例 #5
0
        public void CreateWithEmptyObjectHasNoParams()
        {
            //Arrange & Act
            HtmlElementFindParams fp = new HtmlElementFindParams(new { });

            //Assert
            ValidateBaseHtmlElementFindParams(fp, null, null, 0, 0, null, MatchMethod.EndsWith);
        }
コード例 #6
0
        public void ConvertObjectToDictionaryReturnsNullWhenPassedNull()
        {
            //Arrange & Act
            Dictionary <string, object> d = HtmlElementFindParams.ConvertObjectToDictionary(null);

            //Assert
            UnitTestAssert.IsNull(d);
        }
コード例 #7
0
 private static HtmlElement GetRowContainer( HtmlElement rootElement )
 {
     HtmlElementFindParams findParams1 = new HtmlElementFindParams();
     findParams1.TagName = "div";
     findParams1.Attributes.Add( "class", "wgreylinebottom" );
     findParams1.Index = 0;
     
     return rootElement.ChildElements.Find( findParams1 );
 }
コード例 #8
0
        public void ConvertObjectToDictionaryReturnsDictionaryWhenPassedAnObject()
        {
            //Arrange & Act
            Dictionary <string, object> d = HtmlElementFindParams.ConvertObjectToDictionary(new { Id = 3, Name = "Matthew", isAwesome = true });

            //Assert
            UnitTestAssert.AreEqual(3, d.Count);
            UnitTestAssert.AreEqual(3, d["Id"]);
            UnitTestAssert.AreEqual("Matthew", d["Name"]);
            UnitTestAssert.AreEqual(true, d["IsAwesome"]);
        }
コード例 #9
0
        private void ValidateBaseHtmlElementFindParams(HtmlElementFindParams fp, string tagName, string innerText, int index, int attributeCount, string id, MatchMethod idMatchMethod)
        {
            //Base Properties
            UnitTestAssert.AreEqual(tagName, fp.TagName);
            UnitTestAssert.AreEqual(innerText, fp.InnerText);
            UnitTestAssert.AreEqual(index, fp.Index);
            UnitTestAssert.AreEqual(attributeCount, fp.Attributes.Count);

            //ID Properties
            UnitTestAssert.AreEqual(id, fp.IdAttributeFindParams.Value);
            UnitTestAssert.AreEqual(idMatchMethod, fp.IdAttributeFindParams.ValueMatchMethod);
        }
コード例 #10
0
    public void FindAllByParams()
    {
        HtmlPage p = new HtmlPage("Miscellaneous.aspx");

        p.Navigate("Miscellaneous.aspx");

        HtmlElementFindParams f = new HtmlElementFindParams();

        f.TagName = "input";
        f.Attributes.Add("value", "TestButton");
        HtmlElement e = p.Elements.Find(f);

        Assert.AreEqual("Button2", e.Id);
    }
コード例 #11
0
        public void WhenCallingFindByIdRegex_IfNoMatchFound_ShouldThrowError()
        {
            //Arrange
            string html = @"
                <html>
                </html>
                ";

            //Act,Assert
            HtmlElement           element    = HtmlElement.Create(html);
            HtmlElementFindParams findParams = new HtmlElementFindParams("foo.*bar", MatchMethod.Regex);

            ExceptionAssert.Throws <ElementNotFoundException>(
                () => element.ChildElements.Find(findParams, 0));
        }
コード例 #12
0
        public void WhenFindingByAttributes_IfMatchingAttributesAsLiteral_ShouldReturnElement()
        {
            string html = @"
				<html>
					<body>
						<input id='foo' class='style1' />
					</body>
				</html>
				"                ;

            HtmlElement root = HtmlElement.Create(html);

            HtmlElementFindParams args = new HtmlElementFindParams();

            args.Attributes.Add("class", "style1");

            Assert.AreEqual("foo", root.ChildElements.Find(args, 0).Id);
        }
コード例 #13
0
        public void WhenCallingFind_IfElementDoestNotExist_ShouldThrowAnException()
        {
            // Arrange
            string html = @"
                <html id='control1'>
                    <foo id='control2'> 
                        <bar id='control3' />
                    </foo>
                </html>
                ";

            HtmlElement           element = HtmlElement.Create(html);
            HtmlElementFindParams p       = new HtmlElementFindParams("control4");

            // Act/Assert
            ExceptionAssert.Throws <ElementNotFoundException>(
                () => element.ChildElements.Find(p, 0));
        }
コード例 #14
0
        public void WhenCallingExistsByAttributes_IfAttributeNotMatchWithEndsWith_ShouldReturnFalse()
        {
            string html = @"
				<html>
					<body>
						<input id='foo' class='style1 style2 style3' />
					</body>
				</html>
				"                ;

            HtmlElement root = HtmlElement.Create(html);

            HtmlElementFindParams args = new HtmlElementFindParams();

            args.Attributes.Add("class", "style1", MatchMethod.EndsWith);

            Assert.IsFalse(root.ChildElements.Exists(args, 0));
        }
コード例 #15
0
        public void WhenCallingExistsByAttributes_IfAllAttributesNotMatch_ShouldReturnFalse()
        {
            string html = @"
				<html>
					<body>
						<input id='foo' class='style1 style2 style3' style='height:30px;width:20px' />
					</body>
				</html>
				"                ;

            HtmlElement root = HtmlElement.Create(html);

            HtmlElementFindParams args = new HtmlElementFindParams();

            args.Attributes.Add("class", "style2", MatchMethod.Contains);
            args.Attributes.Add("style", "width:10px", MatchMethod.Regex);

            Assert.IsFalse(root.ChildElements.Exists(args, 0));
        }
コード例 #16
0
        public void WhenCallingFindByIdEndsWithAndIndex_ShouldReturnTheElement()
        {
            //Arrange
            string html = @"
                <html>
                    <div id='control1'> 
                        <div id='control1' foo=bar />
                    </div>
                    <div id='control1' />
                </html>
                ";

            HtmlElement element = HtmlElement.Create(html);

            //Act,Assert
            HtmlElementFindParams findParams = new HtmlElementFindParams("control1");

            findParams.Index = 1;
            Assert.AreEqual("bar", element.ChildElements.Find(findParams).GetAttributes().Dictionary["foo"]);
        }
コード例 #17
0
        public void CreateWithCustomAttributesObjectProperParamsPopulated()
        {
            //Arrange & Act
            HtmlElementFindParams fp = new HtmlElementFindParams(new { @class = "myClass", @classMatchMethod = MatchMethod.Contains, attribute1 = "value", attribute1MatchMethod = MatchMethod.Regex, attribute2 = "value2" });

            //Assert
            ValidateBaseHtmlElementFindParams(fp, null, null, 0, 3, null, MatchMethod.EndsWith);
            //  Class Attribute
            UnitTestAssert.AreEqual("class", fp.Attributes[0].Name);
            UnitTestAssert.AreEqual("myClass", fp.Attributes[0].Value);
            UnitTestAssert.AreEqual(MatchMethod.Contains, fp.Attributes[0].ValueMatchMethod);
            //  Attribute 1
            UnitTestAssert.AreEqual("attribute1", fp.Attributes[1].Name);
            UnitTestAssert.AreEqual("value", fp.Attributes[1].Value);
            UnitTestAssert.AreEqual(MatchMethod.Regex, fp.Attributes[1].ValueMatchMethod);
            //  Attribute 2
            UnitTestAssert.AreEqual("attribute2", fp.Attributes[2].Name);
            UnitTestAssert.AreEqual("value2", fp.Attributes[2].Value);
            UnitTestAssert.AreEqual(MatchMethod.Literal, fp.Attributes[2].ValueMatchMethod);
        }
コード例 #18
0
        public void WhenCallingFindByTagAndIndex_ShouldReturnTheElement()
        {
            //Arrange
            string html = @"
                <html>
                    <div id='control1'> 
                        <div id='control1' foo='bar' />
                    </div>
                    <div id='control1' test='a' />
                </html>
                ";

            HtmlElement element = HtmlElement.Create(html);

            //Act,Assert
            HtmlElementFindParams findParams = new HtmlElementFindParams();

            findParams.TagName = "div";
            findParams.Index   = 2;
            Assert.AreEqual("a", element.ChildElements.Find(findParams).GetAttributes().Dictionary["test"]);
        }
コード例 #19
0
        public void WhenCallingFindByTagInerTextAndIndex_IfMultipleSiblingsMatsh_ShouldReturnTheElement()
        {
            string html = @"
                <html>
                    <div foo=foo bar=bar> 
                        <span foo=foo bar=bar>text</span>
                        <div foo=foo bar=bar>text</div>
                        <span foo=foo />
                        <span bar=bar />
                        <span foo=foo bar=bar>wrong</span>
                        <span foo=foo bar=bar id='span1'>text</span>
                    </div>
                </html>
                ";

            HtmlElement element = HtmlElement.Create(html);

            HtmlElementFindParams findParams = new HtmlElementFindParams();

            findParams.TagName   = "span";
            findParams.InnerText = "text";
            findParams.Index     = 1;
            Assert.AreEqual("span1", element.ChildElements.Find(findParams).Id);
        }
コード例 #20
0
        private static IList<HtmlElement> GetElementList( HtmlElement container )
        {
            HtmlElementFindParams findParams2 = new HtmlElementFindParams();
            findParams2.TagName = "div";
            findParams2.Attributes.Add( "class", "arr" );

            IList<HtmlElement> elementList = container.ChildElements.FindAll( findParams2 ).ToList();

            return elementList;
        }