public void DFSwalk() { pApp = Process.Start(@"D:\Users\Dennis\Desktop\Hackathon\guitesting\WpfApplication1\WpfApplication1\bin\Debug\WpfApplication1.exe"); //pApp = Process.Start(@"D:\Users\Dennis\RPCPro.NET_local\RPCPro .NET\Bin\ProjectManager.exe"); while (pApp.MainWindowHandle == IntPtr.Zero) { Thread.Sleep(1000); } AutomationElement ParentWindow = AutomationElement.FromHandle(pApp.MainWindowHandle); //AutomationElementCollection DesktopChildren = AutomationElement.RootElement.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.IsEnabledProperty, true)); Random rand = new Random(); while (true) { AutomationElementCollection ElementCollection = ParentWindow.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.IsEnabledProperty, true)); List <AutomationElement> windows = new List <AutomationElement> { }; foreach (AutomationElement myIter in ElementCollection) { if (myIter.Current.ControlType == ControlType.Window) { windows.Add(myIter); } } AutomationElement iter = elemPicker.GetElementPriority(ElementCollection); if (iter == null) { continue; } AutomationPattern[] validPatterns = iter.GetSupportedPatterns(); if (validPatterns.Count() > 0) { int patternIndex = rand.Next(0, validPatterns.Count() - 1); patternManager.executePattern(iter, validPatterns[patternIndex]); StreamManager.WriteLine(validPatterns[patternIndex].ProgrammaticName + " " + iter.Current.Name.ToString(), true, false); StreamManager.WriteLine(iter.Current.Name + " " + iter.Current.AutomationId + " " + validPatterns[patternIndex].ProgrammaticName, false); } } }
public AutomationElement GetElementPriority(AutomationElementCollection inCollection) { //gather the current elements in the GUI. List <ElementEntry> currentElements = new List <ElementEntry> { }; foreach (AutomationElement iter in inCollection) { if (SkipElement(iter)) { continue; } var elementLog = from myElem in mElementTracker where (ElementIsEntry(iter, myElem)) select myElem; if (elementLog.Count() == 0) { //new element mElementTracker.Add(new ElementEntry { Name = iter.Current.Name, AutomationId = iter.Current.AutomationId, mControlType = iter.Current.ControlType, Priority = 100 }); } if (elementLog.Count() <= 1) { //recover old element currentElements.Add(elementLog.First()); } else { //multiple matches to element. Add more criteria. Debug.Assert(false); } } //sort current elements by rank. //currentElements.Sort(PrioritizeElementEntries); foreach (var elem in currentElements.OrderBy(p => p.Priority)) { StreamManager.WriteLine(elem.Name + " " + elem.AutomationId + " " + elem.Priority); } int sumPriority = 0; foreach (ElementEntry elem in currentElements) { sumPriority += elem.Priority; } //randomly choose a ElementEntry weighted by priority. AutomationElement returnElement = null; int randomVal = rand.Next(sumPriority); int count = randomVal; foreach (ElementEntry myEntry in currentElements) { if (count < myEntry.Priority && returnElement == null) { myEntry.Priority--; var elementLog = from AutomationElement myElem in inCollection where (ElementIsEntry(myElem, myEntry)) select myElem; StreamManager.WriteLine("CHOOSE: " + elementLog.First().Current.Name + elementLog.First().Current.AutomationId); myEntry.Priority = 100; returnElement = elementLog.First(); } else { count -= myEntry.Priority; myEntry.Priority++; } var updateLog = from myElem in mElementTracker where (myElem.Name == myEntry.Name) select myElem; updateLog.First().Priority = myEntry.Priority; } return(returnElement); }