Skip to content

Commit

Permalink
Added tests for opening desktops on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
l8556 committed Dec 10, 2024
1 parent b360bb0 commit 0b5ea2e
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 5 deletions.
1 change: 1 addition & 0 deletions frameworks/host_control/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from .FileUtils import FileUtils
from .host_info import HostInfo
from .windows import Window
7 changes: 7 additions & 0 deletions frameworks/host_control/windows/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
from frameworks.host_control import HostInfo

if HostInfo().os == 'windows':
from .windows_window import WindowsWindow as Window
else:
from .linux_window import LinuxWindow as Window
19 changes: 19 additions & 0 deletions frameworks/host_control/windows/linux_window.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from typing import Optional

from .window import Window


class LinuxWindow(Window):

@staticmethod
def get_hwnd(class_name: str, text: str) -> Optional[int]:
pass

@staticmethod
def get_child_window_hwnd(window_hwnd: int, child_window_title: str, child_window_text: str) -> Optional[int]:
pass

@staticmethod
def click_on_button(button_hwnd: int):
pass
18 changes: 18 additions & 0 deletions frameworks/host_control/windows/window.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
from abc import ABC, abstractmethod
from typing import Optional


class Window(ABC):

@staticmethod
@abstractmethod
def get_hwnd(class_name: str, text: str) -> Optional[int]: ...

@staticmethod
@abstractmethod
def get_child_window_hwnd(window_hwnd: int, child_window_title: str, child_window_text: str) -> Optional[int]: ...

@staticmethod
@abstractmethod
def click_on_button(button_hwnd: int): ...
48 changes: 48 additions & 0 deletions frameworks/host_control/windows/windows_window.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
from typing import Optional

from .window import Window

try:
import win32gui
import win32con
except ImportError:
pass


class WindowsWindow(Window):

@staticmethod
def get_hwnd(class_name: str, text: str) -> Optional[int]:

def enum_windows_callback(hwnd: int):
if win32gui.IsWindowVisible(hwnd):
if (
class_name.strip() == win32gui.GetClassName(hwnd).strip()
and text.strip() == win32gui.GetWindowText(hwnd).strip()
):
return hwnd

win32gui.EnumWindows(enum_windows_callback)
return None

@staticmethod
def get_child_window_hwnd(window_hwnd: int, child_window_title: str, child_window_text: str) -> Optional[int]:

def find_button(hwnd):
cls_name, text = win32gui.GetClassName(hwnd), win32gui.GetWindowText(hwnd)
if cls_name in child_window_title and child_window_text in text:
return hwnd

win32gui.EnumChildWindows(window_hwnd, find_button)
return None

@staticmethod
def click_on_button(button_hwnd: int):
try:
win32gui.SendMessage(button_hwnd, win32con.BM_CLICK, 0, 0)
except Exception as ex:
print(ex)



10 changes: 6 additions & 4 deletions frameworks/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ class TestData:
project_dir: str = getcwd()
tmp_dir: str = join(project_dir, 'tmp')
reports_dir: str = join(project_dir, 'reports')
img_template: str = join(project_dir, 'tests', 'assets', 'image_template')
bad_files_dir: str = join(project_dir, 'tests', 'assets', 'bad_files')
good_files_dir: str = join(project_dir, 'tests', 'assets', 'good_files')
lic_file_path: str = join(project_dir, 'tests', 'assets', 'test_lic.lickey')
test_assets: str = join(project_dir, 'tests', 'assets')
img_template: str = join(test_assets, 'image_template')
bad_files_dir: str = join(test_assets, 'bad_files')
good_files_dir: str = join(test_assets, 'good_files')
lic_file_path: str = join(test_assets, 'test_lic.lickey')
config: json = FileUtils.read_json(join(project_dir, 'config.json'))
cache_dir: str = config.get("cache_dir", None)
warning_window_info: str = join(test_assets, "warning_window_info.json")
8 changes: 8 additions & 0 deletions tests/assets/warning_window_info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Default_app_window": {
"class_name": "#32770",
"window_text": "ONLYOFFICE Desktop Editors",
"button_class_name": "Button",
"button_text": "No"
}
}
19 changes: 18 additions & 1 deletion tests/desktop_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from frameworks.desktop import DesktopEditor, DesktopData, DesktopException, UrlException, PackageException
from frameworks.test_data import TestData
from frameworks.host_control import FileUtils, HostInfo
from frameworks.host_control import FileUtils, HostInfo, Window
from frameworks.image_handler import Image
from tests.tools.desktop_report import DesktopReport

Expand All @@ -18,6 +18,8 @@ class TestException(Exception): ...


class DesktopTests:
warning_window_info = FileUtils.read_json(TestData.warning_window_info)

def __init__(
self,
version: str,
Expand Down Expand Up @@ -101,6 +103,7 @@ def check_correct_version(self):
raise TestException(f"[red]|ERROR| The version is not correct: {version}")

def check_error_on_screen(self):
self._close_warning_window()
for error_img in FileUtils.get_paths(join(self.img_dir, 'errors')):
if Image.is_present(error_img):
Image.make_screenshot(join(self.report.dir, f'{self.version}_{self.host_name}_error_screen.png'))
Expand Down Expand Up @@ -160,3 +163,17 @@ def _create_desktop(version: str, custom_config: str, license_file_path: str):
def _report_path(self):
title = self.config.get('title', 'Undefined_title')
return join(TestData.reports_dir, title, self.version, f"{self.version}_{title}_report.csv")

def _close_warning_window(self) -> None:
if HostInfo().os != 'windows':
return

window = Window()
for info in self.warning_window_info.values():
window_hwnd = window.get_hwnd(info.get('class_name', ''), info.get('window_text', ''))
if not window_hwnd:
continue

button_hwnd = window.get_child_window_hwnd(window_hwnd, info.get('Button', ''), info.get('button_text', ''))
if button_hwnd:
window.click_on_button(button_hwnd)

0 comments on commit 0b5ea2e

Please sign in to comment.