forked from microsoft/playwright-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_assertions.py
799 lines (733 loc) · 24.1 KB
/
_assertions.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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import collections.abc
from typing import Any, List, Optional, Pattern, Sequence, Union
from urllib.parse import urljoin
from playwright._impl._api_structures import ExpectedTextValue, FrameExpectOptions
from playwright._impl._connection import format_call_log
from playwright._impl._errors import Error
from playwright._impl._fetch import APIResponse
from playwright._impl._helper import is_textual_mime_type
from playwright._impl._locator import Locator
from playwright._impl._page import Page
from playwright._impl._str_utils import escape_regex_flags
class AssertionsBase:
def __init__(
self,
locator: Locator,
timeout: float = None,
is_not: bool = False,
message: Optional[str] = None,
) -> None:
self._actual_locator = locator
self._loop = locator._loop
self._dispatcher_fiber = locator._dispatcher_fiber
self._timeout = timeout
self._is_not = is_not
self._custom_message = message
async def _expect_impl(
self,
expression: str,
expect_options: FrameExpectOptions,
expected: Any,
message: str,
) -> None:
__tracebackhide__ = True
expect_options["isNot"] = self._is_not
if expect_options.get("timeout") is None:
expect_options["timeout"] = self._timeout or 5_000
if expect_options["isNot"]:
message = message.replace("expected to", "expected not to")
if "useInnerText" in expect_options and expect_options["useInnerText"] is None:
del expect_options["useInnerText"]
result = await self._actual_locator._expect(expression, expect_options)
if result["matches"] == self._is_not:
actual = result.get("received")
if self._custom_message:
out_message = self._custom_message
if expected is not None:
out_message += f"\nExpected value: '{expected or '<None>'}'"
else:
out_message = (
f"{message} '{expected}'" if expected is not None else f"{message}"
)
raise AssertionError(
f"{out_message}\nActual value: {actual} {format_call_log(result.get('log'))}"
)
class PageAssertions(AssertionsBase):
def __init__(
self,
page: Page,
timeout: float = None,
is_not: bool = False,
message: Optional[str] = None,
) -> None:
super().__init__(page.locator(":root"), timeout, is_not, message)
self._actual_page = page
@property
def _not(self) -> "PageAssertions":
return PageAssertions(
self._actual_page, self._timeout, not self._is_not, self._custom_message
)
async def to_have_title(
self, titleOrRegExp: Union[Pattern[str], str], timeout: float = None
) -> None:
expected_values = to_expected_text_values(
[titleOrRegExp], normalize_white_space=True
)
__tracebackhide__ = True
await self._expect_impl(
"to.have.title",
FrameExpectOptions(expectedText=expected_values, timeout=timeout),
titleOrRegExp,
"Page title expected to be",
)
async def not_to_have_title(
self, titleOrRegExp: Union[Pattern[str], str], timeout: float = None
) -> None:
__tracebackhide__ = True
await self._not.to_have_title(titleOrRegExp, timeout)
async def to_have_url(
self, urlOrRegExp: Union[str, Pattern[str]], timeout: float = None
) -> None:
__tracebackhide__ = True
base_url = self._actual_page.context._options.get("baseURL")
if isinstance(urlOrRegExp, str) and base_url:
urlOrRegExp = urljoin(base_url, urlOrRegExp)
expected_text = to_expected_text_values([urlOrRegExp])
await self._expect_impl(
"to.have.url",
FrameExpectOptions(expectedText=expected_text, timeout=timeout),
urlOrRegExp,
"Page URL expected to be",
)
async def not_to_have_url(
self, urlOrRegExp: Union[Pattern[str], str], timeout: float = None
) -> None:
__tracebackhide__ = True
await self._not.to_have_url(urlOrRegExp, timeout)
class LocatorAssertions(AssertionsBase):
def __init__(
self,
locator: Locator,
timeout: float = None,
is_not: bool = False,
message: Optional[str] = None,
) -> None:
super().__init__(locator, timeout, is_not, message)
self._actual_locator = locator
@property
def _not(self) -> "LocatorAssertions":
return LocatorAssertions(
self._actual_locator, self._timeout, not self._is_not, self._custom_message
)
async def to_contain_text(
self,
expected: Union[
Sequence[str],
Sequence[Pattern[str]],
Sequence[Union[Pattern[str], str]],
Pattern[str],
str,
],
useInnerText: bool = None,
timeout: float = None,
ignoreCase: bool = None,
) -> None:
__tracebackhide__ = True
if isinstance(expected, collections.abc.Sequence) and not isinstance(
expected, str
):
expected_text = to_expected_text_values(
expected,
match_substring=True,
normalize_white_space=True,
ignoreCase=ignoreCase,
)
await self._expect_impl(
"to.contain.text.array",
FrameExpectOptions(
expectedText=expected_text,
useInnerText=useInnerText,
timeout=timeout,
),
expected,
"Locator expected to contain text",
)
else:
expected_text = to_expected_text_values(
[expected],
match_substring=True,
normalize_white_space=True,
ignoreCase=ignoreCase,
)
await self._expect_impl(
"to.have.text",
FrameExpectOptions(
expectedText=expected_text,
useInnerText=useInnerText,
timeout=timeout,
),
expected,
"Locator expected to contain text",
)
async def not_to_contain_text(
self,
expected: Union[
Sequence[str],
Sequence[Pattern[str]],
Sequence[Union[Pattern[str], str]],
Pattern[str],
str,
],
useInnerText: bool = None,
timeout: float = None,
ignoreCase: bool = None,
) -> None:
__tracebackhide__ = True
await self._not.to_contain_text(expected, useInnerText, timeout, ignoreCase)
async def to_have_attribute(
self,
name: str,
value: Union[str, Pattern[str]],
ignoreCase: bool = None,
timeout: float = None,
) -> None:
__tracebackhide__ = True
expected_text = to_expected_text_values([value], ignoreCase=ignoreCase)
await self._expect_impl(
"to.have.attribute.value",
FrameExpectOptions(
expressionArg=name, expectedText=expected_text, timeout=timeout
),
value,
"Locator expected to have attribute",
)
async def not_to_have_attribute(
self,
name: str,
value: Union[str, Pattern[str]],
ignoreCase: bool = None,
timeout: float = None,
) -> None:
__tracebackhide__ = True
await self._not.to_have_attribute(
name, value, ignoreCase=ignoreCase, timeout=timeout
)
async def to_have_class(
self,
expected: Union[
Sequence[str],
Sequence[Pattern[str]],
Sequence[Union[Pattern[str], str]],
Pattern[str],
str,
],
timeout: float = None,
) -> None:
__tracebackhide__ = True
if isinstance(expected, collections.abc.Sequence) and not isinstance(
expected, str
):
expected_text = to_expected_text_values(expected)
await self._expect_impl(
"to.have.class.array",
FrameExpectOptions(expectedText=expected_text, timeout=timeout),
expected,
"Locator expected to have class",
)
else:
expected_text = to_expected_text_values([expected])
await self._expect_impl(
"to.have.class",
FrameExpectOptions(expectedText=expected_text, timeout=timeout),
expected,
"Locator expected to have class",
)
async def not_to_have_class(
self,
expected: Union[
Sequence[str],
Sequence[Pattern[str]],
Sequence[Union[Pattern[str], str]],
Pattern[str],
str,
],
timeout: float = None,
) -> None:
__tracebackhide__ = True
await self._not.to_have_class(expected, timeout)
async def to_have_count(
self,
count: int,
timeout: float = None,
) -> None:
__tracebackhide__ = True
await self._expect_impl(
"to.have.count",
FrameExpectOptions(expectedNumber=count, timeout=timeout),
count,
"Locator expected to have count",
)
async def not_to_have_count(
self,
count: int,
timeout: float = None,
) -> None:
__tracebackhide__ = True
await self._not.to_have_count(count, timeout)
async def to_have_css(
self,
name: str,
value: Union[str, Pattern[str]],
timeout: float = None,
) -> None:
__tracebackhide__ = True
expected_text = to_expected_text_values([value])
await self._expect_impl(
"to.have.css",
FrameExpectOptions(
expressionArg=name, expectedText=expected_text, timeout=timeout
),
value,
"Locator expected to have CSS",
)
async def not_to_have_css(
self,
name: str,
value: Union[str, Pattern[str]],
timeout: float = None,
) -> None:
__tracebackhide__ = True
await self._not.to_have_css(name, value, timeout)
async def to_have_id(
self,
id: Union[str, Pattern[str]],
timeout: float = None,
) -> None:
__tracebackhide__ = True
expected_text = to_expected_text_values([id])
await self._expect_impl(
"to.have.id",
FrameExpectOptions(expectedText=expected_text, timeout=timeout),
id,
"Locator expected to have ID",
)
async def not_to_have_id(
self,
id: Union[str, Pattern[str]],
timeout: float = None,
) -> None:
__tracebackhide__ = True
await self._not.to_have_id(id, timeout)
async def to_have_js_property(
self,
name: str,
value: Any,
timeout: float = None,
) -> None:
__tracebackhide__ = True
await self._expect_impl(
"to.have.property",
FrameExpectOptions(
expressionArg=name, expectedValue=value, timeout=timeout
),
value,
"Locator expected to have JS Property",
)
async def not_to_have_js_property(
self,
name: str,
value: Any,
timeout: float = None,
) -> None:
__tracebackhide__ = True
await self._not.to_have_js_property(name, value, timeout)
async def to_have_value(
self,
value: Union[str, Pattern[str]],
timeout: float = None,
) -> None:
__tracebackhide__ = True
expected_text = to_expected_text_values([value])
await self._expect_impl(
"to.have.value",
FrameExpectOptions(expectedText=expected_text, timeout=timeout),
value,
"Locator expected to have Value",
)
async def not_to_have_value(
self,
value: Union[str, Pattern[str]],
timeout: float = None,
) -> None:
__tracebackhide__ = True
await self._not.to_have_value(value, timeout)
async def to_have_values(
self,
values: Union[
Sequence[str], Sequence[Pattern[str]], Sequence[Union[Pattern[str], str]]
],
timeout: float = None,
) -> None:
__tracebackhide__ = True
expected_text = to_expected_text_values(values)
await self._expect_impl(
"to.have.values",
FrameExpectOptions(expectedText=expected_text, timeout=timeout),
values,
"Locator expected to have Values",
)
async def not_to_have_values(
self,
values: Union[
Sequence[str], Sequence[Pattern[str]], Sequence[Union[Pattern[str], str]]
],
timeout: float = None,
) -> None:
__tracebackhide__ = True
await self._not.to_have_values(values, timeout)
async def to_have_text(
self,
expected: Union[
Sequence[str],
Sequence[Pattern[str]],
Sequence[Union[Pattern[str], str]],
Pattern[str],
str,
],
useInnerText: bool = None,
timeout: float = None,
ignoreCase: bool = None,
) -> None:
__tracebackhide__ = True
if isinstance(expected, collections.abc.Sequence) and not isinstance(
expected, str
):
expected_text = to_expected_text_values(
expected,
normalize_white_space=True,
ignoreCase=ignoreCase,
)
await self._expect_impl(
"to.have.text.array",
FrameExpectOptions(
expectedText=expected_text,
useInnerText=useInnerText,
timeout=timeout,
),
expected,
"Locator expected to have text",
)
else:
expected_text = to_expected_text_values(
[expected], normalize_white_space=True, ignoreCase=ignoreCase
)
await self._expect_impl(
"to.have.text",
FrameExpectOptions(
expectedText=expected_text,
useInnerText=useInnerText,
timeout=timeout,
),
expected,
"Locator expected to have text",
)
async def not_to_have_text(
self,
expected: Union[
Sequence[str],
Sequence[Pattern[str]],
Sequence[Union[Pattern[str], str]],
Pattern[str],
str,
],
useInnerText: bool = None,
timeout: float = None,
ignoreCase: bool = None,
) -> None:
__tracebackhide__ = True
await self._not.to_have_text(expected, useInnerText, timeout, ignoreCase)
async def to_be_attached(
self,
attached: bool = None,
timeout: float = None,
) -> None:
__tracebackhide__ = True
await self._expect_impl(
"to.be.attached"
if (attached is None or attached is True)
else "to.be.detached",
FrameExpectOptions(timeout=timeout),
None,
"Locator expected to be attached",
)
async def to_be_checked(
self,
timeout: float = None,
checked: bool = None,
) -> None:
__tracebackhide__ = True
await self._expect_impl(
"to.be.checked"
if checked is None or checked is True
else "to.be.unchecked",
FrameExpectOptions(timeout=timeout),
None,
"Locator expected to be checked",
)
async def not_to_be_attached(
self,
attached: bool = None,
timeout: float = None,
) -> None:
__tracebackhide__ = True
await self._not.to_be_attached(attached=attached, timeout=timeout)
async def not_to_be_checked(
self,
timeout: float = None,
) -> None:
__tracebackhide__ = True
await self._not.to_be_checked(timeout)
async def to_be_disabled(
self,
timeout: float = None,
) -> None:
__tracebackhide__ = True
await self._expect_impl(
"to.be.disabled",
FrameExpectOptions(timeout=timeout),
None,
"Locator expected to be disabled",
)
async def not_to_be_disabled(
self,
timeout: float = None,
) -> None:
__tracebackhide__ = True
await self._not.to_be_disabled(timeout)
async def to_be_editable(
self,
editable: bool = None,
timeout: float = None,
) -> None:
__tracebackhide__ = True
if editable is None:
editable = True
await self._expect_impl(
"to.be.editable" if editable else "to.be.readonly",
FrameExpectOptions(timeout=timeout),
None,
"Locator expected to be editable",
)
async def not_to_be_editable(
self,
editable: bool = None,
timeout: float = None,
) -> None:
__tracebackhide__ = True
await self._not.to_be_editable(editable, timeout)
async def to_be_empty(
self,
timeout: float = None,
) -> None:
__tracebackhide__ = True
await self._expect_impl(
"to.be.empty",
FrameExpectOptions(timeout=timeout),
None,
"Locator expected to be empty",
)
async def not_to_be_empty(
self,
timeout: float = None,
) -> None:
__tracebackhide__ = True
await self._not.to_be_empty(timeout)
async def to_be_enabled(
self,
enabled: bool = None,
timeout: float = None,
) -> None:
__tracebackhide__ = True
if enabled is None:
enabled = True
await self._expect_impl(
"to.be.enabled" if enabled else "to.be.disabled",
FrameExpectOptions(timeout=timeout),
None,
"Locator expected to be enabled",
)
async def not_to_be_enabled(
self,
enabled: bool = None,
timeout: float = None,
) -> None:
__tracebackhide__ = True
await self._not.to_be_enabled(enabled, timeout)
async def to_be_hidden(
self,
timeout: float = None,
) -> None:
__tracebackhide__ = True
await self._expect_impl(
"to.be.hidden",
FrameExpectOptions(timeout=timeout),
None,
"Locator expected to be hidden",
)
async def not_to_be_hidden(
self,
timeout: float = None,
) -> None:
__tracebackhide__ = True
await self._not.to_be_hidden(timeout)
async def to_be_visible(
self,
visible: bool = None,
timeout: float = None,
) -> None:
__tracebackhide__ = True
if visible is None:
visible = True
await self._expect_impl(
"to.be.visible" if visible else "to.be.hidden",
FrameExpectOptions(timeout=timeout),
None,
"Locator expected to be visible",
)
async def not_to_be_visible(
self,
visible: bool = None,
timeout: float = None,
) -> None:
__tracebackhide__ = True
await self._not.to_be_visible(visible, timeout)
async def to_be_focused(
self,
timeout: float = None,
) -> None:
__tracebackhide__ = True
await self._expect_impl(
"to.be.focused",
FrameExpectOptions(timeout=timeout),
None,
"Locator expected to be focused",
)
async def not_to_be_focused(
self,
timeout: float = None,
) -> None:
__tracebackhide__ = True
await self._not.to_be_focused(timeout)
async def to_be_in_viewport(
self,
ratio: float = None,
timeout: float = None,
) -> None:
__tracebackhide__ = True
await self._expect_impl(
"to.be.in.viewport",
FrameExpectOptions(timeout=timeout, expectedNumber=ratio),
None,
"Locator expected to be in viewport",
)
async def not_to_be_in_viewport(
self, ratio: float = None, timeout: float = None
) -> None:
__tracebackhide__ = True
await self._not.to_be_in_viewport(ratio=ratio, timeout=timeout)
class APIResponseAssertions:
def __init__(
self,
response: APIResponse,
timeout: float = None,
is_not: bool = False,
message: Optional[str] = None,
) -> None:
self._loop = response._loop
self._dispatcher_fiber = response._dispatcher_fiber
self._timeout = timeout
self._is_not = is_not
self._actual = response
self._custom_message = message
@property
def _not(self) -> "APIResponseAssertions":
return APIResponseAssertions(
self._actual, self._timeout, not self._is_not, self._custom_message
)
async def to_be_ok(
self,
) -> None:
__tracebackhide__ = True
if self._is_not is not self._actual.ok:
return
message = f"Response status expected to be within [200..299] range, was '{self._actual.status}'"
if self._is_not:
message = message.replace("expected to", "expected not to")
out_message = self._custom_message or message
out_message += format_call_log(await self._actual._fetch_log())
content_type = self._actual.headers.get("content-type")
is_text_encoding = content_type and is_textual_mime_type(content_type)
text = await self._actual.text() if is_text_encoding else None
if text is not None:
out_message += f"\n Response Text:\n{text[:1000]}"
raise AssertionError(out_message)
async def not_to_be_ok(self) -> None:
__tracebackhide__ = True
await self._not.to_be_ok()
def expected_regex(
pattern: Pattern[str],
match_substring: bool,
normalize_white_space: bool,
ignoreCase: Optional[bool] = None,
) -> ExpectedTextValue:
expected = ExpectedTextValue(
regexSource=pattern.pattern,
regexFlags=escape_regex_flags(pattern),
matchSubstring=match_substring,
normalizeWhiteSpace=normalize_white_space,
ignoreCase=ignoreCase,
)
if expected["ignoreCase"] is None:
del expected["ignoreCase"]
return expected
def to_expected_text_values(
items: Union[
Sequence[Pattern[str]], Sequence[str], Sequence[Union[str, Pattern[str]]]
],
match_substring: bool = False,
normalize_white_space: bool = False,
ignoreCase: Optional[bool] = None,
) -> Sequence[ExpectedTextValue]:
out: List[ExpectedTextValue] = []
assert isinstance(items, list)
for item in items:
if isinstance(item, str):
o = ExpectedTextValue(
string=item,
matchSubstring=match_substring,
normalizeWhiteSpace=normalize_white_space,
ignoreCase=ignoreCase,
)
if o["ignoreCase"] is None:
del o["ignoreCase"]
out.append(o)
elif isinstance(item, Pattern):
out.append(
expected_regex(item, match_substring, normalize_white_space, ignoreCase)
)
else:
raise Error("value must be a string or regular expression")
return out