diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml index bc4e81f14..df7c0124f 100644 --- a/.github/workflows/Test.yml +++ b/.github/workflows/Test.yml @@ -177,4 +177,4 @@ jobs: # path: "**/*.trx" # reporter: dotnet-trx - + \ No newline at end of file diff --git a/Ginger/GingerCore/Actions/UIAutomation/UIAComWrapperHelper.cs b/Ginger/GingerCore/Actions/UIAutomation/UIAComWrapperHelper.cs index 245687be6..1f1d53494 100644 --- a/Ginger/GingerCore/Actions/UIAutomation/UIAComWrapperHelper.cs +++ b/Ginger/GingerCore/Actions/UIAutomation/UIAComWrapperHelper.cs @@ -8075,7 +8075,7 @@ public override object GetElementAtPoint(System.Drawing.Point point) { LastElementFromPointMessage = string.Empty; object element = UIAuto.AutomationElement.FromPoint(point); - if (mPlatform == ePlatformType.PowerBuilder && element is UIAuto.AutomationElement hitElement) + if (element is UIAuto.AutomationElement hitElement && (mPlatform == ePlatformType.PowerBuilder || IsPowerBuilderWindow(CurrentWindow))) { element = GetPowerBuilderElementAtPoint(hitElement, point); } @@ -8124,7 +8124,10 @@ public override object GetElementAtPoint(System.Drawing.Point point) /// /// A DataWindow that does not resolve a point to one of its columns answers with /// itself, and a group box drawn around controls answers for the controls inside it. - /// The smallest visible element there that contains the point is taken instead. + /// A background drawn behind controls, such as a picture, a rectangle or a static + /// text, can also answer for every control drawn over it, so each of them is spied + /// as that same background. The smallest visible element there that contains the + /// point is taken instead. /// private UIAuto.AutomationElement GetPowerBuilderElementAtPoint(UIAuto.AutomationElement hitElement, System.Drawing.Point point) { @@ -8139,6 +8142,10 @@ private UIAuto.AutomationElement GetPowerBuilderElementAtPoint(UIAuto.Automation return hitElement; } } + else if (!IsDataWindow(hitElement) && !hitElement.Current.IsKeyboardFocusable) + { + container = FindSmallestElementInFrontAtPoint(hitElement, point) ?? hitElement; + } if (IsDataWindow(container)) { @@ -8153,6 +8160,48 @@ private UIAuto.AutomationElement GetPowerBuilderElementAtPoint(UIAuto.Automation } } + /// + /// The smallest visible element inside, or beside, an element that cannot take focus, + /// that contains the point. Null when the element itself is the smallest there. + /// + private UIAuto.AutomationElement FindSmallestElementInFrontAtPoint(UIAuto.AutomationElement hitElement, System.Drawing.Point point) + { + long hitArea = Area(hitElement); + UIAuto.AutomationElement inside = FindSmallestChildAtPoint(hitElement, point, hitArea); + if (hitElement.Equals(CurrentWindow)) + { + return inside; + } + + // The siblings of a top level window are the other applications' windows. + UIAuto.AutomationElement parent = UIAuto.TreeWalker.RawViewWalker.GetParent(hitElement); + if (parent == null || parent.Equals(UIAuto.AutomationElement.RootElement)) + { + return inside; + } + + UIAuto.AutomationElement beside = FindSmallestChildAtPoint(parent, point, inside == null ? hitArea : Area(inside)); + return beside ?? inside; + } + + /// + /// Whether a window is a PowerBuilder window, which the Windows driver can be spying as well. + /// PowerBuilder names its window classes FNWND followed by the runtime build. + /// + private static bool IsPowerBuilderWindow(UIAuto.AutomationElement window) + { + try + { + string className = window?.Current.ClassName; + return className != null && className.StartsWith("FNW", StringComparison.OrdinalIgnoreCase); + } + catch (Exception ex) + { + Reporter.ToLog(eLogLevel.DEBUG, "Could not read the class of the current window", ex); + return false; + } + } + private static bool IsGroupBox(UIAuto.AutomationElement element) { return General.CompareStringsIgnoreCase(element.Current.LocalizedControlType, "group"); @@ -9346,4 +9395,4 @@ public string GetPreviousSiblingInnerText(ElementInfo elementInfo) } } -} +} \ No newline at end of file diff --git a/Ginger/GingerCore/Drivers/WindowsLib/WindowsDriver.cs b/Ginger/GingerCore/Drivers/WindowsLib/WindowsDriver.cs index b8c85a989..09702a953 100644 --- a/Ginger/GingerCore/Drivers/WindowsLib/WindowsDriver.cs +++ b/Ginger/GingerCore/Drivers/WindowsLib/WindowsDriver.cs @@ -307,6 +307,26 @@ public override void RunAction(Act act) bList.Clear(); } } + else if (act.WindowsToCapture == Act.eWindowsToCapture.DesktopScreen) + { + UIAuto.AutomationElement current = mUIAutomationHelper.GetCurrentWindow() as UIAuto.AutomationElement; + if (current == null) + { + act.Error = "Windows application window was not found for the desktop screenshot."; + break; + } + System.Windows.Forms.Screen screen = System.Windows.Forms.Screen.FromHandle( + new IntPtr(current.Current.NativeWindowHandle)); + var shots = GingerCore.General.TakeDesktopScreenShot(true); + if (shots != null && shots.TryGetValue(screen.DeviceName, out string path)) + { + using (Bitmap fromFile = new Bitmap(path)) + using (Bitmap bitmap = new Bitmap(fromFile)) + { + act.AddScreenShot(bitmap); + } + } + } else if (mUIAutomationHelper.GetCurrentWindow() != null) { using (Bitmap bmp = mUIAutomationHelper.GetCurrentWindowBitmap()) diff --git a/Ginger/GingerCoreNET/Run/GingerExecutionEngine.cs b/Ginger/GingerCoreNET/Run/GingerExecutionEngine.cs index e6b7d847c..39c4a066d 100644 --- a/Ginger/GingerCoreNET/Run/GingerExecutionEngine.cs +++ b/Ginger/GingerCoreNET/Run/GingerExecutionEngine.cs @@ -2308,9 +2308,11 @@ private void ProcessScreenShot(Act act, GingerRunner.eActionExecutorType ActionE Agent screenshotAgent = CurrentBusinessFlow.CurrentActivity?.CurrentAgent as Agent; bool javaDesktopScreen = act.WindowsToCapture == Act.eWindowsToCapture.DesktopScreen && (screenshotAgent?.DriverType == Agent.eDriverType.JavaDriver - || screenshotAgent?.DriverType == Agent.eDriverType.PowerBuilder); + || screenshotAgent?.DriverType == Agent.eDriverType.PowerBuilder + || screenshotAgent?.DriverType == Agent.eDriverType.WindowsAutomation + || act.Platform == ePlatformType.Windows); - // Java and PowerBuilder desktop capture is the monitor that contains the application window. + // Java, PowerBuilder, Windows desktop capture is the monitor that contains the application window. // Other drivers keep the shared desktop capture. if (act.WindowsToCapture == Act.eWindowsToCapture.DesktopScreen && !javaDesktopScreen) { diff --git a/Ginger/GingerCoreNET/RunLib/Common/XPathHelper.cs b/Ginger/GingerCoreNET/RunLib/Common/XPathHelper.cs index 210fa2872..396b6830d 100644 --- a/Ginger/GingerCoreNET/RunLib/Common/XPathHelper.cs +++ b/Ginger/GingerCoreNET/RunLib/Common/XPathHelper.cs @@ -176,7 +176,7 @@ private string GetElemntNodeXpath(ElementInfo EI) } //Need to see if we have similar item with same property under same parent if yes need to add index - int? index = StrictXpathGeneration ? GetResolvablePropValIndex(EI, prop, rawVal) : GetPropValIndex(EI, prop, val); + int? index = StrictXpathGeneration ? GetResolvablePropValIndex(EI, prop, rawVal) : GetLegacyPropValIndex(EI, prop, val, rawVal); string SIndex = null; if (index is null or 0) { @@ -246,13 +246,32 @@ private string GetElemntNodeXpath(ElementInfo EI) return index; } + /// + /// The index the XPath has always been given, unless it lands on another element that + /// shares the property, as every repeated sibling after the first one did. + /// + /// + /// POM delta matches elements by XPath string, so an XPath that finds its element is + /// kept exactly as it was produced before. + /// + private int? GetLegacyPropValIndex(ElementInfo EI, string prop, string val, string rawVal) + { + int? legacyIndex = GetPropValIndex(EI, prop, val); + if (legacyIndex == null) + { + return null; + } + + return TryGetIndexAmongParentMatches(EI, prop, rawVal, out int? resolvableIndex) ? resolvableIndex : legacyIndex; + } + /// /// The element's position among its parent's children that match the property, as - /// FindNode resolves it: null for the first, otherwise the zero based position used - /// against FindAll. + /// FindNode resolves it. False when the parent's matches do not include the element. /// - private int? GetResolvablePropValIndex(ElementInfo EI, string prop, string val) + private bool TryGetIndexAmongParentMatches(ElementInfo EI, string prop, string val, out int? index) { + index = null; List conditions = [ new XpathPropertyCondition() { PropertyName = prop, Op = XpathPropertyCondition.XpathConditionOperator.Equel, Value = val }, @@ -268,14 +287,29 @@ private string GetElemntNodeXpath(ElementInfo EI) { if (Equals(matches[i].ElementObject, EI.ElementObject)) { - return i == 0 ? null : i; + index = i == 0 ? null : i; + return true; } } } } catch (Exception ex) { - Reporter.ToLog(eLogLevel.DEBUG, "Could not resolve the XPath index through the parent, counting the previous siblings instead", ex); + Reporter.ToLog(eLogLevel.DEBUG, "Could not resolve the XPath index through the parent", ex); + } + return false; + } + + /// + /// The element's position among its parent's children that match the property, as + /// FindNode resolves it: null for the first, otherwise the zero based position used + /// against FindAll. + /// + private int? GetResolvablePropValIndex(ElementInfo EI, string prop, string val) + { + if (TryGetIndexAmongParentMatches(EI, prop, val, out int? index)) + { + return index; } int previousMatches = 0; diff --git a/Ginger/GingerCoreTest/Misc/PbSpyLocatorTest.cs b/Ginger/GingerCoreTest/Misc/PbSpyLocatorTest.cs index 4df66d14b..7d7b48356 100644 --- a/Ginger/GingerCoreTest/Misc/PbSpyLocatorTest.cs +++ b/Ginger/GingerCoreTest/Misc/PbSpyLocatorTest.cs @@ -127,14 +127,54 @@ public void PowerBuilderXPath_FindsAnElementWhoseNameHasBrackets() } [TestMethod] - public void WindowsXPath_KeepsTheXPathItProducedBefore() + public void WindowsXPath_KeepsTheXPathItProducedBeforeWhereThatFindsTheElement() { UIAComWrapperHelper helper = CreateHelper(ePlatformType.Windows); - string secondButtonXPath = helper.GetElementAbsoluteXPath(ElementOf(mRepeatedButtons[1])); + // Windows POM delta matches elements by XPath string, so an XPath that finds its element must not move. + Assert.AreEqual("/" + RepeatedName, helper.GetElementAbsoluteXPath(ElementOf(mRepeatedButtons[0]))); + Assert.AreEqual("/" + BracketName, helper.GetElementAbsoluteXPath(ElementOf(mBracketButton))); + } + + [TestMethod] + public void WindowsXPath_FindsEachOfTheRepeatedlyNamedSiblings() + { + UIAComWrapperHelper helper = CreateHelper(ePlatformType.Windows); + List xpaths = []; + + foreach (Button button in mRepeatedButtons) + { + UIAuto.AutomationElement expected = ElementOf(button); + string xpath = helper.GetElementAbsoluteXPath(expected); + xpaths.Add(xpath); + + Assert.AreEqual(expected, helper.FindElementByLocator(eLocateBy.ByXPath, xpath), "XPath '" + xpath + "' landed on a different button than the one it was created for"); + } + Assert.AreEqual(xpaths.Count, xpaths.Distinct().Count(), "Each button needs an XPath of its own: " + string.Join(", ", xpaths)); + } + + [TestMethod] + public void WindowsXPath_GivesEachUnnamedTextBoxAnXPathOfItsOwn() + { + TextBox[] boxes = new TextBox[4]; + for (int i = 0; i < boxes.Length; i++) + { + boxes[i] = new TextBox { Text = "000" + i, Location = new Point(250 + (i * 80), 172), Width = 70 }; + mForm.Controls.Add(boxes[i]); + } + Application.DoEvents(); + UIAComWrapperHelper helper = CreateHelper(ePlatformType.Windows); + List xpaths = []; + + foreach (TextBox box in boxes) + { + UIAuto.AutomationElement expected = ElementOf(box); + string xpath = helper.GetElementAbsoluteXPath(expected); + xpaths.Add(xpath); - // Windows POM delta matches elements by XPath string, so its output must not move. - Assert.AreEqual("/" + RepeatedName, secondButtonXPath); + Assert.AreEqual(expected, helper.FindElementByLocator(eLocateBy.ByXPath, xpath), "XPath '" + xpath + "' landed on a different text box than the one it was created for"); + } + Assert.AreEqual(xpaths.Count, xpaths.Distinct().Count(), "Each text box needs an XPath of its own: " + string.Join(", ", xpaths)); } [TestMethod] diff --git a/Ginger/GingerRuntime/Dockerfile b/Ginger/GingerRuntime/Dockerfile index c4534890c..4c4524a39 100644 --- a/Ginger/GingerRuntime/Dockerfile +++ b/Ginger/GingerRuntime/Dockerfile @@ -86,7 +86,7 @@ RUN set -eu; \ chmod +x "$manager"; \ "$manager" --browser chrome; \ "$manager" --browser firefox --browser-version 134; \ - "$manager" --browser edge --browser-version 134 + "$manager" --browser edge # Specify the entry point ENTRYPOINT ["dotnet", "GingerRuntime.dll"]