示例#1
0
        public void PollElements(ElementIdentifier identifier, HtmlElement relativeTo, TimeSpan timeout, Action <List <HtmlElement> > callback)
        {
            //Make initial pass at finding element synchronously (in case element is there, don't waste time starting a task)
            List <HtmlElement> results = FindElements(identifier, relativeTo);

            if (results.Count > 0)
            {
                callback(results);
                return;
            }

            //If initial pass returns nothing, start polling task
            Task.Factory.StartNew(() =>
            {
                DateTime start = DateTime.Now;
                bool stop      = false;
                while (!stop)
                {
                    ThreadingUtils.InvokeControlAction(Browser, ctl =>
                    {
                        List <HtmlElement> pollResults = FindElements(identifier, relativeTo);
                        if (pollResults.Count > 0 || DateTime.Now.Subtract(start) >= timeout)
                        {
                            stop = true;
                            callback(pollResults);
                        }
                    });
                    Thread.Sleep(100);
                }
            });
        }
示例#2
0
 public ElementIdentifier RelativeTo(ElementIdentifier baseElement)
 {
     if (baseElement != null)
     {
         ElementIdentifier           relative               = new ElementIdentifier();
         Dictionary <string, string> baseAnchorDictionary   = baseElement.ToAnchorDictionary();
         Dictionary <string, string> targetAnchorDictionary = this.ToAnchorDictionary();
         foreach (string baseAnchor in baseAnchorDictionary.Keys)
         {
             string basePath = baseAnchorDictionary[baseAnchor];
             string targetPath;
             if (targetAnchorDictionary.TryGetValue(baseAnchor, out targetPath))
             {
                 string[] baseParts   = basePath.Split('/');
                 string[] targetParts = targetPath.Split('/');
                 string   relativePath;
                 if (baseParts.Length == targetParts.Length)
                 {
                     relativePath = ".";
                 }
                 else
                 {
                     relativePath = String.Join("/", targetParts, baseParts.Length, targetParts.Length - baseParts.Length);
                 }
                 relativePath = String.Format("/{0}", relativePath);
                 relative.Identifiers.Add(relativePath);
                 //We only need one path here relative to the base because all paths relative to base paths of matching anchors will be the same.
                 return(relative);
             }
         }
     }
     return(null);
 }
示例#3
0
        public ElementIdentifier Copy()
        {
            ElementIdentifier copy = new ElementIdentifier();

            copy.Identifiers.AddRange(this.Identifiers);
            return(copy);
        }
示例#4
0
        public static ElementIdentifier FromHtmlElement(HtmlElement element)
        {
            ElementIdentifier identifier = new ElementIdentifier();

            zBuildElementPaths(element, identifier.Identifiers);
            return(identifier);
        }
示例#5
0
 public static ElementIdentifier Copy(ElementIdentifier original)
 {
     if (original != null)
     {
         return(original.Copy());
     }
     return(null);
 }
示例#6
0
 public ElementIdentifier AbsoluteTo(ElementIdentifier baseElement)
 {
     if (baseElement != null)
     {
         ElementIdentifier absolute     = new ElementIdentifier();
         string            relativePath = this.PrimaryIdentifier;
         foreach (string identifier in baseElement.Identifiers)
         {
             absolute.Identifiers.Add(String.Format("{0}{1}", identifier, relativePath));
         }
         return(absolute);
     }
     return(null);
 }
示例#7
0
        public ElementIdentifier RemoveSelector()
        {
            ElementIdentifier selectorRemoved = new ElementIdentifier();

            foreach (string identifier in Identifiers)
            {
                if (!identifier.EndsWith(zGetAnchor(identifier)))
                {
                    int lastSlashIndex   = identifier.LastIndexOf('/');
                    int lastBracketIndex = identifier.LastIndexOf('[');
                    selectorRemoved.Identifiers.Add(lastBracketIndex > lastSlashIndex ? identifier.Remove(lastBracketIndex) : identifier);
                }
            }
            return(selectorRemoved);
        }
示例#8
0
        public ElementIdentifier GetParent()
        {
            ElementIdentifier parent = new ElementIdentifier();

            foreach (string identifier in Identifiers)
            {
                string[] parts = identifier.Split(new char[] { '/' });
                if (parts.Length > 1 && parts[parts.Length - 2] != String.Empty)
                {
                    string parentPath = String.Join("/", parts, 0, parts.Length - 1);
                    parent.Identifiers.Add(parentPath);
                }
            }
            return(parent);
        }
示例#9
0
        public List <HtmlElement> FindElements(ElementIdentifier identifier, HtmlElement relativeTo)
        {
            List <HtmlElement> results     = new List <HtmlElement>();
            HtmlElement        baseElement = relativeTo;

            if (baseElement == null)
            {
                baseElement = m_Browser.Document != null ? m_Browser.Document.Body : null;
            }

            //Double slashes (//) will result in a blank array entry. zRecursiveFindElements will treat the empty entry as a descendant indicator.
            //The first blank element of the array is skipped, which is why the array index starts at 1.
            foreach (string path in identifier.Identifiers)
            {
                string[] pathArray = path.Split(new char[] { '/' }, StringSplitOptions.None);
                zRecursiveFindElements(pathArray, 1, baseElement, results);
                if (results.Count > 0)
                {
                    break;
                }
            }
            return(results);
        }
示例#10
0
 public void PollElements(ElementIdentifier identifier, HtmlElement relativeTo, Action <List <HtmlElement> > callback)
 {
     PollElements(identifier, relativeTo, m_DefaultPollingTimeout, callback);
 }
示例#11
0
 public void PollElements(ElementIdentifier identifier, TimeSpan timeout, Action <List <HtmlElement> > callback)
 {
     PollElements(identifier, null, timeout, callback);
 }
示例#12
0
 public void PollElement(ElementIdentifier identifier, HtmlElement relativeTo, TimeSpan timeout, Action <HtmlElement> callback)
 {
     PollElements(identifier, relativeTo, timeout, (elementList) => callback(elementList.SingleOrDefault()));
 }
示例#13
0
 public void PollElement(ElementIdentifier identifier, Action <HtmlElement> callback)
 {
     PollElement(identifier, m_DefaultPollingTimeout, callback);
 }
示例#14
0
 public List <HtmlElement> FindElements(ElementIdentifier identifier)
 {
     return(FindElements(identifier, null));
 }
示例#15
0
 public HtmlElement FindElement(ElementIdentifier identifier, HtmlElement relativeTo)
 {
     return(FindElements(identifier, relativeTo).SingleOrDefault());
 }
示例#16
0
 public HtmlElement FindElement(ElementIdentifier identifier)
 {
     return(FindElement(identifier, null));
 }