コード例 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="wrapper"></param>
        /// <returns></returns>
        public static List <Mark> GetMarks(NodeWrapper wrapper)
        {
            List <Mark> marks = new List <Mark>();
            HtmlNode    node  = wrapper.Node;

            if (!node.Id.NullOrWhiteSpace())
            {
                wrapper.XpathList.Add($"//{node.Name}[@id='{node.Id}']");
                wrapper.CssList.Add($"{node.Name}#{node.Id}");
            }
            foreach (HtmlNode child in node.ChildNodes)
            {
                bool hasMark = false;
                Mark mark    = new Mark();
                if (!child.Id.NullOrWhiteSpace())
                {
                    mark.Locators.Add(By.Id(child.Id));
                }
                if (!child.IsW3cNode())
                {
                    mark.Locators.Add(By.TagName(child.Name));
                }
                if (child.HasAttr("name"))
                {
                    mark.Locators.Add(By.Name(child.Attributes["name"].Value));
                }
                if (child.HasAttr("class"))
                {
                    string classes     = child.Attributes["class"].Value;
                    string cssSelector = classes.Trim().Replace(" ", ".").Replace(" ", "");
                    mark.Locators.Add(By.CssSelector($"{child.Name}.{cssSelector}"));
                    mark.Locators.Add(By.XPath($"//{child.Name}[@class='{classes}']"));
                    mark.ClusterLocators.Add(By.CssSelector($"{child.Name}.{cssSelector}"));
                    mark.ClusterLocators.Add(By.XPath($"//{child.Name}[@class='{classes}']"));
                }
                foreach (var attr in child.Attributes)
                {
                    if (attr.Name.StartsWith(EnvManager.MarkPrefix))
                    {
                        hasMark    = true;
                        mark.Name  = attr.Name.Replace($"{EnvManager.MarkPrefix}-", "");
                        mark.Value = attr.Value;
                    }
                }
                NodeWrapper childWrapper = new NodeWrapper()
                {
                    Node = child
                };
                string childXpath = child.XPath;
                foreach (string xpath in wrapper.XpathList)
                {
                    string xpathValue = $"{xpath}{childXpath.Substring(childXpath.LastIndexOf('/'))}";
                    childWrapper.XpathList.Add(xpathValue);
                    if (hasMark)
                    {
                        mark.Locators.Add(By.XPath(xpathValue));
                        mark.ClusterLocators.Add(By.XPath(xpathValue.Substring(0, xpathValue.LastIndexOf('['))));
                        childWrapper.XpathList.Clear();
                        childWrapper.XpathList.Add($"//{child.Name}"); // 重新开始,以便计算后续标记节点的相对定位值
                    }
                }
                foreach (string css in wrapper.CssList)
                {
                    string xpathValue = $"{css}{childXpath.Substring(childXpath.LastIndexOf('/'))}";
                    string cssValue   = xpathValue.Replace("/", " ").Replace("[", ":nth-child(").Replace("]", ")").Trim();
                    childWrapper.CssList.Add(cssValue);
                    if (hasMark)
                    {
                        mark.Locators.Add(By.CssSelector(cssValue));
                        mark.ClusterLocators.Add(By.CssSelector(cssValue.Substring(0, cssValue.LastIndexOf(':'))));
                        childWrapper.CssList.Clear();
                        childWrapper.CssList.Add($"{child.Name}"); // 重新开始,以便计算后续标记节点的相对定位值
                    }
                }
                if (hasMark)
                {
                    marks.Add(mark);          // 将当前标记节点计入MarkList
                }
                marks.AddRange(GetMarks(childWrapper));
            }
            return(marks);
        }