forked from lleino/joplin-ui-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_general.py
214 lines (178 loc) · 7.79 KB
/
test_general.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"""General test cases."""
import enum
import itertools
import time
from parameterized import parameterized
import pyautogui
from selenium.webdriver.common.by import By
import base
import menu
class Zoom(enum.Enum):
IN = enum.auto()
OUT = enum.auto()
RESET = enum.auto()
ZOOM_MAP = {
"hotkey": {
Zoom.IN: lambda: pyautogui.hotkey("ctrl", "shift", "="),
Zoom.OUT: lambda: pyautogui.hotkey("ctrl", "-"),
Zoom.RESET: lambda: pyautogui.hotkey("ctrl", "0"),
},
"top_menu": {
Zoom.IN: lambda: menu.top(["View", "Zoom in"]),
Zoom.OUT: lambda: menu.top(["View", "Zoom out"]),
Zoom.RESET: lambda: menu.top(["View", "Actual size"]),
},
}
GOTO_ANYTHING_MAP = {
"hotkey": lambda: pyautogui.hotkey("ctrl", "p"),
"top_menu": lambda: menu.top(["Go", "Goto anything"], skip={"Goto anything": 1}),
}
# Order is mixed to don't select the same location twice.
FOCUS_MAP = {
"hotkey": {
"sidebar": lambda: pyautogui.hotkey("ctrl", "shift", "s"),
"note_list": lambda: pyautogui.hotkey("ctrl", "shift", "l"),
"note_title": lambda: pyautogui.hotkey("ctrl", "shift", "n"),
"note_body": lambda: pyautogui.hotkey("ctrl", "shift", "b"),
},
"top_menu": {
# Skip one entry at the focus selection, because "Forward" is not selectable.
"sidebar": lambda: menu.top(["Go", "Focus", "Sidebar"], skip={"Focus": 1}),
"note_list": lambda: menu.top(["Go", "Focus", "Note list"], skip={"Focus": 1}),
"note_title": lambda: menu.top(
["Go", "Focus", "Note title"], skip={"Focus": 1}
),
"note_body": lambda: menu.top(["Go", "Focus", "Note body"], skip={"Focus": 1}),
},
}
TOGGLE_MAP = {
"sidebar": {
"hotkey": lambda: pyautogui.press("f10"),
"top_menu": lambda: menu.top(["View", "Toggle sidebar"]),
},
"notelist": {
"hotkey": lambda: pyautogui.press("f11"),
"top_menu": lambda: menu.top(["View", "Toggle note list"]),
},
}
class Go(base.Test):
notebook = None
notebook_id = None
base_element_map = None
@classmethod
def setUpClass(cls):
super().setUpClass()
for key in GOTO_ANYTHING_MAP:
cls.api.add_note(title=key, content=key)
# Needed for "test_goto_anything()".
cls.api.add_notebook(title="abc")
def setUp(self):
super().setUp()
# Ensure the notebook is selected, in order to select the locations properly.
if self.__class__.notebook is None:
_, _, self.__class__.notebook, self.__class__.notebook_id = self.select_random_note()
self.__class__.base_element_map = {
"sidebar": self.sidebar,
"note_list": self.notelist,
"note_title": self.editor.find_element(By.CLASS_NAME, "title-input"),
"note_body": self.editor.find_element(
By.CLASS_NAME, "codeMirrorEditor"
),
}
@parameterized.expand(
itertools.product(FOCUS_MAP.keys(), FOCUS_MAP["hotkey"].keys())
)
def test_focus(self, way, location):
FOCUS_MAP[way][location]()
self.assert_contains(
self.base_element_map[location], self.driver.switch_to.active_element
)
def test_aa_go_back_forward(self):
# Run first to have a baseline for the coming tests.
# The baseline is: Forward - not selectable, backward - selectable.
# All notes should be in the current notebook.
notes = self.get_notes()
self.assertEqual(len(notes), 3)
def is_selected(element):
# Seleniums is_selected() is not usable in this case.
return "selected" in element.get_attribute("class")
# Click all three notes to have the selectable backward also when going back.
for note in notes:
note.click()
self.assertFalse(is_selected(notes[0]))
self.assertFalse(is_selected(notes[1]))
self.assertTrue(is_selected(notes[2]))
menu.top(["Go", "Back"])
self.assertFalse(is_selected(notes[0]))
self.assertTrue(is_selected(notes[1]))
self.assertFalse(is_selected(notes[2]))
menu.top(["Go", "Forward"])
self.assertFalse(is_selected(notes[0]))
self.assertFalse(is_selected(notes[1]))
self.assertTrue(is_selected(notes[2]))
@parameterized.expand(GOTO_ANYTHING_MAP.keys())
def test_goto_anything(self, way):
# TODO: Extend test for tags and notes (slow).
# Select another notebook to ensure goto is working correctly.
self.select_random_notebook(exclude=[self.notebook_id])
time.sleep(0.1) # TODO: Small delay, because else the menu doesn't open.
GOTO_ANYTHING_MAP[way]()
# Short waiting time to display the search results.
self.fill_modal_dialog(f"@{self.__class__.__name__}", wait_before_confirm=0.2)
self.assertIn("selected", self.notebook.get_attribute("class"))
class View(base.Test):
def test_app_title(self):
self.assertEqual(self.driver.title, "Joplin")
@staticmethod
def test_zz_application_layout():
# Execute last, since it makes the element references stale.
# TODO: Only a smoke test. The text is language specific.
menu.top(["View", "Change application layout"])
pyautogui.press("esc")
@parameterized.expand(
itertools.product(TOGGLE_MAP.keys(), TOGGLE_MAP["sidebar"].keys())
)
def test_toggle(self, location, way):
# "Toggle editor layout" is covered by "test_toggle_layout".
element_map = {"sidebar": self.sidebar, "notelist": self.notelist}
self.assertTrue(element_map[location].is_displayed())
TOGGLE_MAP[location][way]()
self.assertFalse(element_map[location].is_displayed())
TOGGLE_MAP[location][way]()
self.assertTrue(element_map[location].is_displayed())
@parameterized.expand(ZOOM_MAP.keys())
def test_zoom(self, way):
if way == "top_menu":
self.skipTest("TODO: Changing zoom by top menu is slow and error prone.")
def get_sizes():
return {
"sidebar": self.sidebar.size,
"notelist": self.notelist.size,
"editor": self.editor.size,
}
initial_size = get_sizes()
for _ in range(2):
ZOOM_MAP[way][Zoom.IN]()
# Actually only the editor is zooming in x and y direction.
zoom_in_size = get_sizes()
# fmt: off
self.assertLess(zoom_in_size["sidebar"]["height"], initial_size["sidebar"]["height"])
self.assertEqual(zoom_in_size["sidebar"]["width"], initial_size["sidebar"]["width"])
self.assertLess(zoom_in_size["notelist"]["height"], initial_size["notelist"]["height"])
self.assertEqual(zoom_in_size["notelist"]["width"], initial_size["notelist"]["width"])
self.assertLess(zoom_in_size["editor"]["height"], initial_size["editor"]["height"])
self.assertLess(zoom_in_size["editor"]["width"], initial_size["editor"]["width"])
# fmt: on
for _ in range(4):
ZOOM_MAP[way][Zoom.OUT]()
zoom_out_size = get_sizes()
# fmt: off
self.assertGreater(zoom_out_size["sidebar"]["height"], initial_size["sidebar"]["height"])
self.assertEqual(zoom_out_size["sidebar"]["width"], initial_size["sidebar"]["width"])
self.assertGreater(zoom_out_size["notelist"]["height"], initial_size["notelist"]["height"])
self.assertEqual(zoom_out_size["notelist"]["width"], initial_size["notelist"]["width"])
self.assertGreater(zoom_out_size["editor"]["height"], initial_size["editor"]["height"])
self.assertGreater(zoom_out_size["editor"]["width"], initial_size["editor"]["width"])
# fmt: on
ZOOM_MAP[way][Zoom.RESET]()
self.assertEqual(get_sizes(), initial_size)