From 938a56dfb42bb269e8414a7ed1c5c74393727e8d Mon Sep 17 00:00:00 2001 From: buddsean Date: Thu, 29 Apr 2021 15:28:41 +1000 Subject: [PATCH 1/6] focus chrome before running tests and log if this fails --- tests/system/libraries/ChromeLib.py | 46 ++++++++++++--- .../system/libraries/SystemTestSpy/windows.py | 58 +++++++++++++++++++ tests/system/robot/chromeTests.robot | 1 - 3 files changed, 95 insertions(+), 10 deletions(-) create mode 100644 tests/system/libraries/SystemTestSpy/windows.py diff --git a/tests/system/libraries/ChromeLib.py b/tests/system/libraries/ChromeLib.py index d99c3fe37e4..529d29c2d3c 100644 --- a/tests/system/libraries/ChromeLib.py +++ b/tests/system/libraries/ChromeLib.py @@ -1,5 +1,5 @@ # A part of NonVisual Desktop Access (NVDA) -# Copyright (C) 2020 NV Access Limited +# Copyright (C) 2020-2021 NV Access Limited # This file may be used under the terms of the GNU General Public License, version 2 or later. # For more details see: https://www.gnu.org/licenses/gpl-2.0.html @@ -12,8 +12,15 @@ import tempfile as _tempfile from typing import Optional as _Optional from SystemTestSpy import ( + _blockUntilConditionMet, _getLib, ) +from SystemTestSpy.windows import ( + GetForegroundWindowTitle, + GetActiveWindowTitles, + SetForegroundWindow, +) +import re from robot.libraries.BuiltIn import BuiltIn # Imported for type information @@ -116,6 +123,34 @@ def _waitForStartMarker(self, spy, lastSpeechIndex): " See NVDA log for full speech." ) + def _focusChrome(self): + """ Ensure chrome started and is focused. + Different versions of chrome have variations in how the title is presented. + This may mean that there is a separator between document name and application name. + E.G. "htmlTest Google Chrome", "html – Google Chrome" or perhaps no applcation name at all. + Rather than try to get this right, just use the doc title. + If this continues to be unreliable we could use solenium or similar to start chrome and inform us + when it is ready. + """ + startsWithTestCaseTitle = re.compile(f"^{self._testCaseTitle}") + success, _success = _blockUntilConditionMet( + getValue=lambda: SetForegroundWindow(startsWithTestCaseTitle), + giveUpAfterSeconds=3, + intervalBetweenSeconds=0.5 + ) + if success: + return + windowInformation = "" + try: + windowInformation = f"Foreground Window: {GetForegroundWindowTitle()}.\n" + windowInformation += f"Open Windows: {GetActiveWindowTitles()}" + except OSError as e: + builtIn.log(f"Couldn't retrieve active window information.\nException: {e}") + raise AssertionError( + "Unable to focus Chrome.\n" + f"{windowInformation}" + ) + def prepareChrome(self, testCase: str) -> None: """ Starts Chrome opening a file containing the HTML sample @@ -127,14 +162,7 @@ def prepareChrome(self, testCase: str) -> None: spy.wait_for_speech_to_finish() lastSpeechIndex = spy.get_last_speech_index() self.start_chrome(path) - # Ensure chrome started - # Different versions of chrome have variations in how the title is presented - # This may mean that there is a separator between document name and - # application name. E.G. "htmlTest Google Chrome", "html – Google Chrome" or perhaps no applcation - # name at all. - # Rather than try to get this right, just wait for the doc title. - # If this continues to be unreliable we could use solenium or similar to start chrome and inform us when - # it is ready. + self._focusChrome() applicationTitle = f"{self._testCaseTitle}" appTitleIndex = spy.wait_for_specific_speech(applicationTitle, afterIndex=lastSpeechIndex) self._waitForStartMarker(spy, appTitleIndex) diff --git a/tests/system/libraries/SystemTestSpy/windows.py b/tests/system/libraries/SystemTestSpy/windows.py new file mode 100644 index 00000000000..a344a4f0271 --- /dev/null +++ b/tests/system/libraries/SystemTestSpy/windows.py @@ -0,0 +1,58 @@ +# A part of NonVisual Desktop Access (NVDA) +# Copyright (C) 2021 NV Access Limited +# This file may be used under the terms of the GNU General Public License, version 2 or later. +# For more details see: https://www.gnu.org/licenses/gpl-2.0.html + +"""This module provides functions to interact with the Windows system. +""" + +from ctypes.wintypes import HWND, LPARAM +from ctypes import c_bool, c_int, create_unicode_buffer, POINTER, WINFUNCTYPE, windll, WinError +import re +from typing import List, Tuple + + +def _GetWindowTitle(hwnd: HWND) -> str: + length = windll.user32.GetWindowTextLengthW(hwnd) + if not length: + return '' + buff = create_unicode_buffer(length + 1) + if not windll.user32.GetWindowTextW(hwnd, buff, length + 1): + raise WinError() + return str(buff.value) + + +def _GetActiveWindows() -> List[Tuple[HWND, str]]: + windows: List[Tuple[HWND, str]] = [] + EnumWindowsProc = WINFUNCTYPE(c_bool, POINTER(c_int), POINTER(c_int)) + + def _append_title(hwnd: HWND, _lParam: LPARAM) -> bool: + if windll.user32.IsWindowVisible(hwnd): + title = _GetWindowTitle(hwnd) + if title: + windows.append((hwnd, title)) + return True + + if not windll.user32.EnumWindows(EnumWindowsProc(_append_title), 0): + raise WinError() + return windows + + +def SetForegroundWindow(targetTitle: re.Pattern) -> bool: + if re.match(targetTitle, GetForegroundWindowTitle()): + return True + windows = _GetActiveWindows() + for hwnd, title in windows: + if re.match(targetTitle, title): + return windll.user32.SetForegroundWindow(hwnd) + return False + + +def GetActiveWindowTitles() -> List[str]: + windows = _GetActiveWindows() + return [w[1] for w in windows] + + +def GetForegroundWindowTitle() -> str: + hwnd = windll.user32.GetForegroundWindow() + return _GetWindowTitle(hwnd) diff --git a/tests/system/robot/chromeTests.robot b/tests/system/robot/chromeTests.robot index 7a2e7b03610..b84747cdba6 100644 --- a/tests/system/robot/chromeTests.robot +++ b/tests/system/robot/chromeTests.robot @@ -24,7 +24,6 @@ default teardown default setup start NVDA standard-dontShowWelcomeDialog.ini - Sleep 5s *** Test Cases *** From b1e59aef920c582396d5ceb5b87b13efccc8333f Mon Sep 17 00:00:00 2001 From: buddsean Date: Fri, 30 Apr 2021 09:53:55 +1000 Subject: [PATCH 2/6] fix typos --- tests/system/libraries/ChromeLib.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/system/libraries/ChromeLib.py b/tests/system/libraries/ChromeLib.py index 529d29c2d3c..6be81b58b34 100644 --- a/tests/system/libraries/ChromeLib.py +++ b/tests/system/libraries/ChromeLib.py @@ -127,9 +127,9 @@ def _focusChrome(self): """ Ensure chrome started and is focused. Different versions of chrome have variations in how the title is presented. This may mean that there is a separator between document name and application name. - E.G. "htmlTest Google Chrome", "html – Google Chrome" or perhaps no applcation name at all. + E.G. "htmlTest Google Chrome", "html – Google Chrome" or perhaps no application name at all. Rather than try to get this right, just use the doc title. - If this continues to be unreliable we could use solenium or similar to start chrome and inform us + If this continues to be unreliable we could use Selenium or similar to start chrome and inform us when it is ready. """ startsWithTestCaseTitle = re.compile(f"^{self._testCaseTitle}") From 9bd1fbd18813922f977a80a4ff8f789628f9c53f Mon Sep 17 00:00:00 2001 From: buddsean Date: Fri, 30 Apr 2021 11:53:21 +1000 Subject: [PATCH 3/6] make focus window title unique --- tests/system/libraries/ChromeLib.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/tests/system/libraries/ChromeLib.py b/tests/system/libraries/ChromeLib.py index 6be81b58b34..208bfd5187c 100644 --- a/tests/system/libraries/ChromeLib.py +++ b/tests/system/libraries/ChromeLib.py @@ -71,6 +71,14 @@ def start_chrome(self, filePath): _afterMarker = "After Test Case Marker" _loadCompleteString = "Test page load complete" + @staticmethod + def getUniqueTestCaseTitle(testCase: str) -> str: + return f"{ChromeLib._testCaseTitle} ({hash(testCase)})" + + @staticmethod + def getUniqueTestCaseTitleRegex(testCase: str) -> re.Pattern: + return re.compile(f"{ChromeLib._testCaseTitle} \\({hash(testCase)}\\)") + @staticmethod def _writeTestFile(testCase) -> str: """ @@ -82,7 +90,7 @@ def _writeTestFile(testCase) -> str: filePath = ChromeLib._getTestCasePath("test.html") fileContents = (f""" - {ChromeLib._testCaseTitle} + {ChromeLib.getUniqueTestCaseTitle(testCase)}

{ChromeLib._beforeMarker}

@@ -123,7 +131,7 @@ def _waitForStartMarker(self, spy, lastSpeechIndex): " See NVDA log for full speech." ) - def _focusChrome(self): + def _focusChrome(self, startsWithTestCaseTitle: re.Pattern): """ Ensure chrome started and is focused. Different versions of chrome have variations in how the title is presented. This may mean that there is a separator between document name and application name. @@ -132,7 +140,6 @@ def _focusChrome(self): If this continues to be unreliable we could use Selenium or similar to start chrome and inform us when it is ready. """ - startsWithTestCaseTitle = re.compile(f"^{self._testCaseTitle}") success, _success = _blockUntilConditionMet( getValue=lambda: SetForegroundWindow(startsWithTestCaseTitle), giveUpAfterSeconds=3, @@ -162,8 +169,8 @@ def prepareChrome(self, testCase: str) -> None: spy.wait_for_speech_to_finish() lastSpeechIndex = spy.get_last_speech_index() self.start_chrome(path) - self._focusChrome() - applicationTitle = f"{self._testCaseTitle}" + self._focusChrome(ChromeLib.getUniqueTestCaseTitleRegex(testCase)) + applicationTitle = ChromeLib.getUniqueTestCaseTitle(testCase) appTitleIndex = spy.wait_for_specific_speech(applicationTitle, afterIndex=lastSpeechIndex) self._waitForStartMarker(spy, appTitleIndex) # Move to the loading status line, and wait fore it to become complete From 29c287cf70904016314677a131c0f2ae158f43fd Mon Sep 17 00:00:00 2001 From: buddsean Date: Fri, 30 Apr 2021 12:25:19 +1000 Subject: [PATCH 4/6] fix regex --- tests/system/libraries/ChromeLib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/system/libraries/ChromeLib.py b/tests/system/libraries/ChromeLib.py index 208bfd5187c..e064dab40f9 100644 --- a/tests/system/libraries/ChromeLib.py +++ b/tests/system/libraries/ChromeLib.py @@ -77,7 +77,7 @@ def getUniqueTestCaseTitle(testCase: str) -> str: @staticmethod def getUniqueTestCaseTitleRegex(testCase: str) -> re.Pattern: - return re.compile(f"{ChromeLib._testCaseTitle} \\({hash(testCase)}\\)") + return re.compile(f"^{ChromeLib._testCaseTitle} \\({hash(testCase)}\\)") @staticmethod def _writeTestFile(testCase) -> str: From 38df3b15b759f18f86f8a34fd1ab0a1caf1c216b Mon Sep 17 00:00:00 2001 From: buddsean Date: Fri, 30 Apr 2021 13:41:16 +1000 Subject: [PATCH 5/6] fix matching for focus --- tests/system/libraries/ChromeLib.py | 8 ++-- .../system/libraries/SystemTestSpy/windows.py | 41 ++++++++++++------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/tests/system/libraries/ChromeLib.py b/tests/system/libraries/ChromeLib.py index e064dab40f9..74ed48ecdb7 100644 --- a/tests/system/libraries/ChromeLib.py +++ b/tests/system/libraries/ChromeLib.py @@ -17,7 +17,7 @@ ) from SystemTestSpy.windows import ( GetForegroundWindowTitle, - GetActiveWindowTitles, + GetVisibleWindowTitles, SetForegroundWindow, ) import re @@ -73,11 +73,11 @@ def start_chrome(self, filePath): @staticmethod def getUniqueTestCaseTitle(testCase: str) -> str: - return f"{ChromeLib._testCaseTitle} ({hash(testCase)})" + return f"{ChromeLib._testCaseTitle} ({abs(hash(testCase))})" @staticmethod def getUniqueTestCaseTitleRegex(testCase: str) -> re.Pattern: - return re.compile(f"^{ChromeLib._testCaseTitle} \\({hash(testCase)}\\)") + return re.compile(f"^{ChromeLib._testCaseTitle} \\({abs(hash(testCase))}\\)") @staticmethod def _writeTestFile(testCase) -> str: @@ -150,7 +150,7 @@ def _focusChrome(self, startsWithTestCaseTitle: re.Pattern): windowInformation = "" try: windowInformation = f"Foreground Window: {GetForegroundWindowTitle()}.\n" - windowInformation += f"Open Windows: {GetActiveWindowTitles()}" + windowInformation += f"Open Windows: {GetVisibleWindowTitles()}" except OSError as e: builtIn.log(f"Couldn't retrieve active window information.\nException: {e}") raise AssertionError( diff --git a/tests/system/libraries/SystemTestSpy/windows.py b/tests/system/libraries/SystemTestSpy/windows.py index a344a4f0271..1617ff4d57f 100644 --- a/tests/system/libraries/SystemTestSpy/windows.py +++ b/tests/system/libraries/SystemTestSpy/windows.py @@ -9,7 +9,12 @@ from ctypes.wintypes import HWND, LPARAM from ctypes import c_bool, c_int, create_unicode_buffer, POINTER, WINFUNCTYPE, windll, WinError import re -from typing import List, Tuple +from typing import Callable, List, NamedTuple + + +class Window(NamedTuple): + hwnd: HWND + title: str def _GetWindowTitle(hwnd: HWND) -> str: @@ -22,15 +27,16 @@ def _GetWindowTitle(hwnd: HWND) -> str: return str(buff.value) -def _GetActiveWindows() -> List[Tuple[HWND, str]]: - windows: List[Tuple[HWND, str]] = [] +def _GetWindows( + filterUsingWindow: Callable[[Window], bool] = lambda _: True, +) -> List[Window]: + windows: List[Window] = [] EnumWindowsProc = WINFUNCTYPE(c_bool, POINTER(c_int), POINTER(c_int)) def _append_title(hwnd: HWND, _lParam: LPARAM) -> bool: - if windll.user32.IsWindowVisible(hwnd): - title = _GetWindowTitle(hwnd) - if title: - windows.append((hwnd, title)) + window = Window(hwnd, _GetWindowTitle(hwnd)) + if filterUsingWindow(window): + windows.append(window) return True if not windll.user32.EnumWindows(EnumWindowsProc(_append_title), 0): @@ -38,19 +44,26 @@ def _append_title(hwnd: HWND, _lParam: LPARAM) -> bool: return windows +def _GetVisibleWindows() -> List[Window]: + return _GetWindows( + filterUsingWindow=lambda window: windll.user32.IsWindowVisible(window.hwnd) and bool(window.title) + ) + + def SetForegroundWindow(targetTitle: re.Pattern) -> bool: if re.match(targetTitle, GetForegroundWindowTitle()): return True - windows = _GetActiveWindows() - for hwnd, title in windows: - if re.match(targetTitle, title): - return windll.user32.SetForegroundWindow(hwnd) + windows = _GetWindows( + filterUsingWindow=lambda window: re.match(targetTitle, window.title) + ) + for window in windows: + return windll.user32.SetForegroundWindow(window.hwnd) return False -def GetActiveWindowTitles() -> List[str]: - windows = _GetActiveWindows() - return [w[1] for w in windows] +def GetVisibleWindowTitles() -> List[str]: + windows = _GetVisibleWindows() + return [w.title for w in windows] def GetForegroundWindowTitle() -> str: From 7de1a8ebd67cf97211a619c50937f0a09e0976e9 Mon Sep 17 00:00:00 2001 From: buddsean Date: Fri, 30 Apr 2021 13:52:49 +1000 Subject: [PATCH 6/6] fix lint --- tests/system/libraries/SystemTestSpy/windows.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/system/libraries/SystemTestSpy/windows.py b/tests/system/libraries/SystemTestSpy/windows.py index 1617ff4d57f..6742bad58eb 100644 --- a/tests/system/libraries/SystemTestSpy/windows.py +++ b/tests/system/libraries/SystemTestSpy/windows.py @@ -13,8 +13,8 @@ class Window(NamedTuple): - hwnd: HWND - title: str + hwnd: HWND + title: str def _GetWindowTitle(hwnd: HWND) -> str: