forked from microsoft/playwright-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_browsercontext_storage_state.py
96 lines (85 loc) · 3.23 KB
/
test_browsercontext_storage_state.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
# 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 asyncio
import json
async def test_should_capture_local_storage(context, is_webkit, is_win):
page1 = await context.new_page()
await page1.route(
"**/*", lambda route: asyncio.create_task(route.fulfill(body="<html></html>"))
)
await page1.goto("https://www.example.com")
await page1.evaluate("localStorage['name1'] = 'value1'")
await page1.goto("https://www.domain.com")
await page1.evaluate("localStorage['name2'] = 'value2'")
state = await context.storage_state()
origins = state["origins"]
assert len(origins) == 2
assert origins[0] == {
"origin": "https://www.example.com",
"localStorage": [{"name": "name1", "value": "value1"}],
}
assert origins[1] == {
"origin": "https://www.domain.com",
"localStorage": [{"name": "name2", "value": "value2"}],
}
async def test_should_set_local_storage(browser, is_webkit, is_win):
context = await browser.new_context(
storage_state={
"origins": [
{
"origin": "https://www.example.com",
"localStorage": [{"name": "name1", "value": "value1"}],
}
]
}
)
page = await context.new_page()
await page.route(
"**/*", lambda route: asyncio.create_task(route.fulfill(body="<html></html>"))
)
await page.goto("https://www.example.com")
local_storage = await page.evaluate("window.localStorage")
assert local_storage == {"name1": "value1"}
await context.close()
async def test_should_round_trip_through_the_file(browser, context, tmpdir):
page1 = await context.new_page()
await page1.route(
"**/*",
lambda route: asyncio.create_task(route.fulfill(body="<html></html>")),
)
await page1.goto("https://www.example.com")
await page1.evaluate(
"""() => {
localStorage["name1"] = "value1"
document.cookie = "username=John Doe"
return document.cookie
}"""
)
path = tmpdir / "storage-state.json"
state = await context.storage_state(path=path)
with open(path, "r") as f:
written = json.load(f)
assert state == written
context2 = await browser.new_context(storage_state=path)
page2 = await context2.new_page()
await page2.route(
"**/*",
lambda route: asyncio.create_task(route.fulfill(body="<html></html>")),
)
await page2.goto("https://www.example.com")
local_storage = await page2.evaluate("window.localStorage")
assert local_storage == {"name1": "value1"}
cookie = await page2.evaluate("document.cookie")
assert cookie == "username=John Doe"
await context2.close()