How to use test_add_init_script_throw_without_path_and_content 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_add_init_script.py

Source: test_add_init_script.py Github

copy
1# Copyright (c) Microsoft Corporation.
2#
3# Licensed under the Apache License, Version 2.0 (the "License")
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15from playwright.sync_api import Error, Page
16
17
18def test_add_init_script_evaluate_before_anything_else_on_the_page(page):
19    page.add_init_script("window.injected = 123")
20    page.goto("data:text/html,<script>window.result = window.injected</script>")
21    assert page.evaluate("window.result") == 123
22
23
24def test_add_init_script_work_with_a_path(page, assetdir):
25    page.add_init_script(path=assetdir / "injectedfile.js")
26    page.goto("data:text/html,<script>window.result = window.injected</script>")
27    assert page.evaluate("window.result") == 123
28
29
30def test_add_init_script_work_with_content(page: Page):
31    page.add_init_script("window.injected = 123")
32    page.goto("data:text/html,<script>window.result = window.injected</script>")
33    assert page.evaluate("window.result") == 123
34
35
36def test_add_init_script_throw_without_path_and_content(page):
37    error = None
38    try:
39        page.add_init_script({"foo": "bar"})
40    except Error as e:
41        error = e
42    assert error.message == "Either path or source parameter must be specified"
43
44
45def test_add_init_script_work_with_browser_context_scripts(page, context):
46    context.add_init_script("window.temp = 123")
47    page = context.new_page()
48    page.add_init_script("window.injected = window.temp")
49    page.goto("data:text/html,<script>window.result = window.injected</script>")
50    assert page.evaluate("window.result") == 123
51
52
53def test_add_init_script_work_with_browser_context_scripts_with_a_path(
54    page, context, assetdir
55):
56    context.add_init_script(path=assetdir / "injectedfile.js")
57    page = context.new_page()
58    page.goto("data:text/html,<script>window.result = window.injected</script>")
59    assert page.evaluate("window.result") == 123
60
61
62def test_add_init_script_work_with_browser_context_scripts_for_already_created_pages(
63    page, context
64):
65    context.add_init_script("window.temp = 123")
66    page.add_init_script("window.injected = window.temp")
67    page.goto("data:text/html,<script>window.result = window.injected</script>")
68    assert page.evaluate("window.result") == 123
69
70
71def test_add_init_script_support_multiple_scripts(page):
72    page.add_init_script("window.script1 = 1")
73    page.add_init_script("window.script2 = 2")
74    page.goto("data:text/html,<script>window.result = window.injected</script>")
75    assert page.evaluate("window.script1") == 1
76    assert page.evaluate("window.script2") == 2
77
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)