How to use on_request 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_session.py

Source: test_session.py Github

copy
1import unittest
2import time
3import memcache
4import subprocess
5
6from py4web import request, response, Session, DAL
7from py4web.utils.dbstore import DBStore
8
9
10class TestSession(unittest.TestCase):
11    def test_session(self):
12        request.app_name = "myapp"
13        session = Session(secret="a", expiration=10)
14        session.on_request()
15        session["key"] = "value"
16        session.on_success()
17        cookie_name = session.local.session_cookie_name
18
19        a, b = str(response._cookies)[len("Set-Cookie: ") :].split(";")[0].split("=", 1)
20        request.cookies[a] = b
21        request.cookies = response._cookies
22        session.local.data.clear()
23
24        session = Session(secret="b", expiration=10)
25        session.on_request()
26        self.assertEqual(session.get("key"), None)
27
28        session = Session(secret="a", expiration=10)
29        session.on_request()
30        self.assertEqual(session.get("key"), "value")
31
32    def test_session_in_db(self):
33        request.app_name = "myapp"
34        db = DAL("sqlite:memory")
35        session = Session(secret="a", expiration=10, storage=DBStore(db))
36        request.cookies.clear()
37        session.on_request()
38        session["key"] = "value"
39        session.on_success()
40        cookie_name = session.local.session_cookie_name
41
42        a, b = str(response._cookies)[len("Set-Cookie: ") :].split(";")[0].split("=", 1)
43        request.cookies[a] = b
44        request.cookies = response._cookies
45        session.local.data.clear()
46
47        session = Session(expiration=10, storage=DBStore(db))
48        session.on_request()
49        self.assertEqual(session.get("key"), "value")
50
51        request.cookies[a] = "wrong_cookie"
52        session = Session(expiration=10, storage=DBStore(db))
53        session.on_request()
54        self.assertEqual(session.get("key"), None)
55
56    def test_session_in_memcache(self):
57        memcache_process = None
58        try:
59            memcache_process = subprocess.Popen(["memcached", "-p", "11211"])
60            time.sleep(1)
61            request.app_name = "myapp"
62            conn = memcache.Client(["127.0.0.1:11211"], debug=0)
63            session = Session(secret="a", expiration=10, storage=conn)
64            request.cookies.clear()
65            session.on_request()
66            session["key"] = "value"
67            session.on_success()
68            cookie_name = session.local.session_cookie_name
69
70            a, b = (
71                str(response._cookies)[len("Set-Cookie: ") :]
72                .split(";")[0]
73                .split("=", 1)
74            )
75            request.cookies[a] = b
76            request.cookies = response._cookies
77            session.local.data.clear()
78
79            conn = memcache.Client(["127.0.0.1:11211"], debug=0)
80            session = Session(expiration=10, storage=conn)
81            session.on_request()
82            self.assertEqual(session.get("key"), "value")
83
84            request.cookies[a] = "wrong_cookie"
85            conn = memcache.Client(["127.0.0.1:11211"], debug=0)
86            session = Session(expiration=10, storage=conn)
87            session.on_request()
88            self.assertEqual(session.get("key"), None)
89        finally:
90            if memcache_process is None:
91                print("memcached not availabl, test skipped")
92            elif memcache_process:
93                memcache_process.kill()
94
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)