How to use test_strict_mode method in Playwright Python

Best Python code snippet using playwright-python

Run Playwright Python automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

test_sets_hashes.py

Source: test_sets_hashes.py Github

copy
1import related
2from .models import Person, RoleModels
3
4
5def test_set_construction():
6    scientists = [Person(first_name="Grace", last_name="Hopper"),
7                  Person(first_name="Katherine", last_name="Johnson"),
8                  Person(first_name="Katherine", last_name="Johnson")]
9
10    assert len(scientists) == 3
11
12    role_models = RoleModels(scientists=scientists)
13    assert len(role_models.scientists) == 2
14
15    assert related.to_yaml(role_models).strip() in (SET_ORDER_1, SET_ORDER_2)
16
17
18SET_ORDER_1 = """
19scientists:
20- first_name: Grace
21  last_name: Hopper
22- first_name: Katherine
23  last_name: Johnson
24""".strip()
25
26
27SET_ORDER_2 = """
28scientists:
29- first_name: Katherine
30  last_name: Johnson
31- first_name: Grace
32  last_name: Hopper
33""".strip()
34
35
36def test_strict_mode():
37    p = related.to_model(Person, dict(first_name="Grace", last_name="Hopper"))
38    assert p.first_name == "Grace"
39
40    try:
41        d = dict(first_name="Grace", last_name="Hopper", extra="Failure!")
42        related.to_model(Person, d)
43        assert False, "Did not fail."
44
45    except ValueError as e:
46        assert e, "Error as expected."
47
Full Screen

test_utilities.py

Source: test_utilities.py Github

copy
1from tests.utilities.utilities import compare_lists
2
3
4class TestNormalizeAndCompareLists(object):
5    def test_lists_empty_string(self):
6        assert compare_lists(["", "", ""], ["", "", ""])
7
8    def test_lists_not_same_size(self):
9        assert not compare_lists(["", ""], ["", "", ""])
10
11    def test_lists_empty(self):
12        assert compare_lists([], [])
13
14    def test_list_empty_and_other_not(self):
15        assert not compare_lists([], ["foo"])
16        assert not compare_lists(["foo"], [])
17
18    def test_list_tabs(self):
19        assert compare_lists(["\t", ""], ["\t", ""])
20
21    def test_list_mixed_order(self):
22        assert compare_lists(["", "\t"], ["\t", ""])
23        assert compare_lists(["", "foo"], ["foo", ""])
24        assert compare_lists(["foo", ""], ["foo", ""])
25        assert not compare_lists(["bar", ""], ["foo", ""])
26        assert not compare_lists(["", "bar"], ["foo", ""])
27
28    def test_list_mix_tabs_spaces_letters(self):
29        assert not compare_lists(["foo \t", "bar"], ["\t", ""])
30        assert compare_lists(["foo   dssdsd\t", "bar"], ["\tfoo dssdsd", "\tbar"])
31
32    def test_strict_mode(self):
33        assert not compare_lists(["foo \t", "bar"], ["foo", "bar"], True)
34        assert compare_lists(["foo", "bar"], ["foo", "bar"], True)
35
Full Screen

test_imports.py

Source: test_imports.py Github

copy
1import re
2import unittest
3import warnings
4from importlib import import_module
5
6from import_guard import ForbiddenImportError, guard
7
8
9class TestForbiddenImportError(unittest.TestCase):
10    def test_strict_mode(self):
11        guard.set_deny_rules({"csv": "re"})
12        guard.enable(strict=True)
13
14        with self.assertRaises(ForbiddenImportError):
15            import_module("csv")
16
17
18class TestProject(unittest.TestCase):
19    def test_test_proj(self):
20        # rules are defined inside test_proj
21        expected = {
22            # <module> forbidden in <caller>
23            ("csv", "test_proj.api"),
24            ("bisect", "test_proj.api"),
25            ("json", "test_proj.logging"),
26            ("test_proj.business_logic", "test_proj.core"),
27            ("test_proj.logging", "test_proj.api"),
28        }
29
30        # capture all warnings, extract affected modules
31        # and compare to expected
32        with warnings.catch_warnings(record=True) as w:
33            warnings.simplefilter("always")
34            import_module("test_proj").run()
35
36        pattern = re.compile(r"Importing `([\w\.]+)` from `([\w\.]+)`")
37        actual = {pattern.findall(str(x.message))[0] for x in w}
38
39        self.assertEqual(actual, expected)
40
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Python Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Python on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)