Esempio n. 1
0
        public void GetValuesFromNodes_Test()
        {
            var doc = new HtmlDocument();

            doc.LoadHtml(HtmlNode);

            var nodes = new HtmlExtractorNode[] {
                new HtmlExtractorNode {
                    Name = "Head", ShouldShowInOutPut = true, XPath = "html/head"
                },
                new HtmlExtractorNode {
                    Name = "H1Text", ShouldShowInOutPut = true, XPath = "html/body/h1"
                },
                new HtmlExtractorNode {
                    Name = "H2Text", ShouldShowInOutPut = true, XPath = "html/body/h2", Type = "float"
                },
                new HtmlExtractorNode {
                    Name = "Cars", ShouldShowInOutPut = true, XPath = "./html/body/div/div", Childs = new HtmlExtractorNode[] {
                        new HtmlExtractorNode {
                            Name = "Name", ShouldShowInOutPut = true, XPath = "./span[1]"
                        },
                        new HtmlExtractorNode {
                            Name = "Year", ShouldShowInOutPut = true, XPath = "./span[2]"
                        },
                        new HtmlExtractorNode {
                            Name = "CarModels", ShouldShowInOutPut = true, XPath = "./div/span", Childs = new HtmlExtractorNode[] {
                                new HtmlExtractorNode {
                                    Name = "Name", ShouldShowInOutPut = true, XPath = "./text()"
                                },
                            }
                        },
                    }
                }
            };

            var response = _htmlExtractorService.GetValuesFromNodes(nodes, doc.DocumentNode);


            var result = Newtonsoft.Json.JsonConvert.DeserializeObject <GetValuesFromNodesOutPut>(response.ToString());

            Assert.AreEqual("test", result.Head);
            StringAssert.Contains("Mojtaba", result.H1Text);
            Assert.AreEqual(12.1f, result.H2Text);
            Assert.That(result.Cars, Has.Count.EqualTo(3));
            Assert.That(result.Cars[0].CarModels, Is.Null);
            Assert.That(result.Cars[1].CarModels, Has.Count.EqualTo(2));
            Assert.That(result.Cars[2].CarModels, Has.Count.EqualTo(4));
        }
Esempio n. 2
0
        public void ValidateNodes_InValidData_Test()
        {
            var doc = new HtmlDocument();

            doc.LoadHtml(HtmlNode);

            var validNodes = new HtmlExtractorNode[] {
                new HtmlExtractorNode {
                    Name = "Head", ShouldShowInOutPut = true, XPath = "html/head", ShouldCheckInValidation = true
                },
                new HtmlExtractorNode {
                    Name = "H1Text", ShouldShowInOutPut = true, XPath = "html/body/h1", ShouldCheckInValidation = true
                },
                new HtmlExtractorNode {
                    Name = "H2Text", ShouldShowInOutPut = true, XPath = "html/body/h2", Type = "float", ShouldCheckInValidation = true
                },
                new HtmlExtractorNode {
                    Name = "Cars", ShouldShowInOutPut = true, XPath = "./html/body/div/div", Childs = new HtmlExtractorNode[] {
                        new HtmlExtractorNode {
                            Name = "Name", ShouldShowInOutPut = true, XPath = "./span[1]", ShouldCheckInValidation = true
                        },
                        new HtmlExtractorNode {
                            Name = "Year", ShouldShowInOutPut = true, XPath = "./span[2]", ShouldCheckInValidation = true
                        },
                        new HtmlExtractorNode {
                            Name = "CarModels", ShouldShowInOutPut = true, XPath = "./div/span", Childs = new HtmlExtractorNode[] {
                                new HtmlExtractorNode {
                                    Name = "Name", ShouldShowInOutPut = true, XPath = "./text()"
                                },
                                new HtmlExtractorNode {
                                    Name = "Model", ShouldShowInOutPut = true, XPath = "./span/div", ShouldCheckInValidation = true
                                }
                            }
                        },
                    }
                }
            };

            var response = _htmlExtractorService.ValidateNodes(validNodes, doc.DocumentNode);

            Assert.IsNotNull(response);
        }