-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathtest_locate_with_relations.py
More file actions
340 lines (309 loc) · 13.3 KB
/
Copy pathtest_locate_with_relations.py
File metadata and controls
340 lines (309 loc) · 13.3 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
"""Tests for Agent.locate() with different locator types and models"""
import pathlib
import pytest
from PIL import Image as PILImage
from askui import ComputerAgent
from askui.locators import Element, Image, Prompt, Text
from askui.locators.locators import AiElement
from askui.models.exceptions import ElementNotFoundError
class TestAgentLocateWithRelations:
"""Test class for Agent.locate() method with relations."""
def test_locate_with_above_relation(
self,
vision_agent: ComputerAgent,
github_login_screenshot: PILImage.Image,
) -> None:
"""Test locating elements using above_of relation."""
locator = Text("Forgot password?").above_of(Element("textfield"))
x, y = vision_agent.locate(locator, github_login_screenshot)
assert 450 <= x <= 570
assert 190 <= y <= 260
def test_locate_with_below_relation(
self,
vision_agent: ComputerAgent,
github_login_screenshot: PILImage.Image,
) -> None:
"""Test locating elements using below_of relation."""
locator = Text("Forgot password?").below_of(Element("textfield"))
x, y = vision_agent.locate(locator, github_login_screenshot)
assert 450 <= x <= 570
assert 190 <= y <= 260
def test_locate_with_right_relation(
self,
vision_agent: ComputerAgent,
github_login_screenshot: PILImage.Image,
) -> None:
"""Test locating elements using right_of relation."""
locator = Text("Forgot password?").right_of(Text("Password"))
x, y = vision_agent.locate(locator, github_login_screenshot)
assert 450 <= x <= 570
assert 190 <= y <= 260
def test_locate_with_left_relation(
self,
vision_agent: ComputerAgent,
github_login_screenshot: PILImage.Image,
) -> None:
"""Test locating elements using left_of relation."""
locator = Text("Password").left_of(Text("Forgot password?"))
x, y = vision_agent.locate(locator, github_login_screenshot)
assert 350 <= x <= 450
assert 190 <= y <= 260
def test_locate_with_containing_relation(
self,
vision_agent: ComputerAgent,
github_login_screenshot: PILImage.Image,
) -> None:
"""Test locating elements using containing relation."""
locator = Element("textfield").containing(Text("github.com/login"))
x, y = vision_agent.locate(locator, github_login_screenshot)
assert 50 <= x <= 860
assert 0 <= y <= 80
def test_locate_with_inside_relation(
self,
vision_agent: ComputerAgent,
github_login_screenshot: PILImage.Image,
) -> None:
"""Test locating elements using inside_of relation."""
locator = Text("github.com/login").inside_of(Element("textfield"))
x, y = vision_agent.locate(locator, github_login_screenshot)
assert 70 <= x <= 200
assert 10 <= y <= 75
def test_locate_with_nearest_to_relation(
self,
vision_agent: ComputerAgent,
github_login_screenshot: PILImage.Image,
) -> None:
"""Test locating elements using nearest_to relation."""
locator = Element("textfield").nearest_to(Text("Password"))
x, y = vision_agent.locate(locator, github_login_screenshot)
assert 350 <= x <= 570
assert 210 <= y <= 280
@pytest.mark.skip("Skipping tests for now because it is failing for unknown reason")
def test_locate_with_and_relation(
self,
vision_agent: ComputerAgent,
github_login_screenshot: PILImage.Image,
) -> None:
"""Test locating elements using and_ relation."""
locator = Text("Forgot password?").and_(Element("text"))
x, y = vision_agent.locate(locator, github_login_screenshot)
assert 450 <= x <= 570
assert 190 <= y <= 260
def test_locate_with_or_relation(
self,
vision_agent: ComputerAgent,
github_login_screenshot: PILImage.Image,
) -> None:
"""Test locating elements using or_ relation."""
locator = Element("textfield").nearest_to(
Text("Password").or_(Text("Username or email address"))
)
x, y = vision_agent.locate(locator, github_login_screenshot)
assert 350 <= x <= 570
assert 160 <= y <= 280
def test_locate_with_relation_index(
self,
vision_agent: ComputerAgent,
github_login_screenshot: PILImage.Image,
) -> None:
"""Test locating elements using relation with index."""
locator = Element("textfield").below_of(
Text("Username or email address"), index=0
)
x, y = vision_agent.locate(locator, github_login_screenshot)
assert 350 <= x <= 570
assert 160 <= y <= 230
def test_locate_with_relation_index_greater_0(
self,
vision_agent: ComputerAgent,
github_login_screenshot: PILImage.Image,
) -> None:
"""Test locating elements using relation with index."""
locator = Element("textfield").below_of(Element("textfield"), index=1)
x, y = vision_agent.locate(locator, github_login_screenshot)
assert 350 <= x <= 570
assert 210 <= y <= 280
@pytest.mark.skip("Skipping tests for now because it is failing for unknown reason")
def test_locate_with_relation_index_greater_1(
self,
vision_agent: ComputerAgent,
github_login_screenshot: PILImage.Image,
) -> None:
"""Test locating elements using relation with index."""
locator = Text("Sign in").below_of(Text(), index=4, reference_point="any")
x, y = vision_agent.locate(locator, github_login_screenshot)
assert 420 <= x <= 500
assert 250 <= y <= 310
def test_locate_with_relation_reference_point_center(
self,
vision_agent: ComputerAgent,
github_login_screenshot: PILImage.Image,
) -> None:
"""Test locating elements using relation with center reference point."""
locator = Text("Forgot password?").right_of(
Text("Password"), reference_point="center"
)
x, y = vision_agent.locate(locator, github_login_screenshot)
assert 450 <= x <= 570
assert 190 <= y <= 260
def test_locate_with_relation_reference_point_center_raises_when_element_cannot_be_located(
self,
vision_agent: ComputerAgent,
github_login_screenshot: PILImage.Image,
) -> None:
"""Test locating elements using relation with center reference point."""
locator = Text("Sign in").below_of(Text("Password"), reference_point="center")
with pytest.raises(ElementNotFoundError):
vision_agent.locate(locator, github_login_screenshot)
def test_locate_with_relation_reference_point_boundary(
self,
vision_agent: ComputerAgent,
github_login_screenshot: PILImage.Image,
) -> None:
"""Test locating elements using relation with boundary reference point."""
locator = Text("Forgot password?").right_of(
Text("Password"), reference_point="boundary"
)
x, y = vision_agent.locate(locator, github_login_screenshot)
assert 450 <= x <= 570
assert 190 <= y <= 260
def test_locate_with_relation_reference_point_boundary_raises_when_element_cannot_be_located(
self,
vision_agent: ComputerAgent,
github_login_screenshot: PILImage.Image,
) -> None:
"""Test locating elements using relation with boundary reference point."""
locator = Text("Sign in").below_of(Text("Password"), reference_point="boundary")
with pytest.raises(ElementNotFoundError):
vision_agent.locate(locator, github_login_screenshot)
def test_locate_with_relation_reference_point_any(
self,
vision_agent: ComputerAgent,
github_login_screenshot: PILImage.Image,
) -> None:
"""Test locating elements using relation with any reference point."""
locator = Text("Sign in").below_of(Text("Password"), reference_point="any")
x, y = vision_agent.locate(locator, github_login_screenshot)
assert 420 <= x <= 500
assert 250 <= y <= 310
def test_locate_with_multiple_relations_with_same_locator_raises(
self,
vision_agent: ComputerAgent,
github_login_screenshot: PILImage.Image,
) -> None:
"""Test locating elements using multiple relations with same locator which is not supported by AskUI."""
locator = (
Text("Forgot password?")
.below_of(Element("textfield"))
.below_of(Element("textfield"))
)
with pytest.raises(NotImplementedError):
vision_agent.locate(locator, github_login_screenshot)
def test_locate_with_chained_relations(
self,
vision_agent: ComputerAgent,
github_login_screenshot: PILImage.Image,
) -> None:
"""Test locating elements using chained relations."""
locator = Text("Sign in").below_of(
Text("Password").below_of(Text("Username or email address")),
reference_point="any",
)
x, y = vision_agent.locate(locator, github_login_screenshot)
assert 420 <= x <= 500
assert 250 <= y <= 310
def test_locate_with_relation_different_locator_types(
self,
vision_agent: ComputerAgent,
github_login_screenshot: PILImage.Image,
) -> None:
"""Test locating elements using relation with different locator types."""
locator = Text("Sign in").below_of(
Element("textfield").below_of(Text("Username or email address")),
reference_point="center",
)
x, y = vision_agent.locate(locator, github_login_screenshot)
assert 420 <= x <= 500
assert 250 <= y <= 310
def test_locate_with_description_and_relation(
self,
vision_agent: ComputerAgent,
github_login_screenshot: PILImage.Image,
) -> None:
"""Test locating elements using description with relation."""
locator = Prompt("Sign in button").below_of(Prompt("Password field"))
x, y = vision_agent.locate(locator, github_login_screenshot)
assert 350 <= x <= 570
assert 240 <= y <= 320
@pytest.mark.skip("Skipping tests for now because it is failing for unknown reason")
def test_locate_with_description_and_complex_relation(
self,
vision_agent: ComputerAgent,
github_login_screenshot: PILImage.Image,
) -> None:
"""Test locating elements using description with relation."""
locator = Prompt("Sign in button").below_of(
Element("textfield").below_of(Text("Password"))
)
x, y = vision_agent.locate(locator, github_login_screenshot)
assert 350 <= x <= 570
assert 240 <= y <= 320
def test_locate_with_image_and_relation(
self,
vision_agent: ComputerAgent,
github_login_screenshot: PILImage.Image,
path_fixtures: pathlib.Path,
) -> None:
"""Test locating elements using image locator with relation."""
image_path = path_fixtures / "images" / "github_com__signin__button.png"
image = PILImage.open(image_path)
locator = Image(image=image).containing(Text("Sign in"))
x, y = vision_agent.locate(locator, github_login_screenshot)
assert 350 <= x <= 570
assert 240 <= y <= 320
def test_locate_with_image_in_relation_to_other_image(
self,
vision_agent: ComputerAgent,
github_login_screenshot: PILImage.Image,
path_fixtures: pathlib.Path,
) -> None:
"""Test locating elements using image locator with relation."""
github_icon_image_path = path_fixtures / "images" / "github_com__icon.png"
signin_button_image_path = (
path_fixtures / "images" / "github_com__signin__button.png"
)
github_icon_image = PILImage.open(github_icon_image_path)
signin_button_image = PILImage.open(signin_button_image_path)
github_icon = Image(image=github_icon_image)
signin_button = Image(image=signin_button_image).below_of(github_icon)
x, y = vision_agent.locate(signin_button, github_login_screenshot)
assert 350 <= x <= 570
assert 240 <= y <= 320
def test_locate_with_image_and_complex_relation(
self,
vision_agent: ComputerAgent,
github_login_screenshot: PILImage.Image,
path_fixtures: pathlib.Path,
) -> None:
"""Test locating elements using image locator with complex relation."""
image_path = path_fixtures / "images" / "github_com__signin__button.png"
image = PILImage.open(image_path)
locator = Image(image=image).below_of(
Element("textfield").below_of(Text("Password"))
)
x, y = vision_agent.locate(locator, github_login_screenshot)
assert 350 <= x <= 570
assert 240 <= y <= 320
def test_locate_with_ai_element_locator_relation(
self,
vision_agent: ComputerAgent,
github_login_screenshot: PILImage.Image,
) -> None:
"""Test locating elements using an AI element locator with relation."""
icon_locator = AiElement("github_com__icon")
signin_locator = AiElement("github_com__signin__button")
x, y = vision_agent.locate(
signin_locator.below_of(icon_locator), github_login_screenshot
)
assert 350 <= x <= 570
assert 240 <= y <= 320