|
| 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 | + |
| 15 | +import os |
| 16 | +from pathlib import Path |
| 17 | +from typing import Dict, Optional |
| 18 | + |
| 19 | +import pytest |
| 20 | + |
| 21 | +from playwright.sync_api import BrowserType, Error |
| 22 | + |
| 23 | + |
| 24 | +@pytest.mark.skip_browser("firefox") |
| 25 | +def test_browser_type_launch_should_throw_if_page_argument_is_passed( |
| 26 | + browser_type: BrowserType, launch_arguments: Dict |
| 27 | +) -> None: |
| 28 | + with pytest.raises(Error) as exc: |
| 29 | + browser_type.launch(**launch_arguments, args=["http://example.com"]) |
| 30 | + assert "can not specify page" in exc.value.message |
| 31 | + |
| 32 | + |
| 33 | +def test_browser_type_launch_should_reject_if_launched_browser_fails_immediately( |
| 34 | + browser_type: BrowserType, launch_arguments: Dict, assetdir: Path |
| 35 | +) -> None: |
| 36 | + with pytest.raises(Error): |
| 37 | + browser_type.launch( |
| 38 | + **launch_arguments, |
| 39 | + executable_path=assetdir / "dummy_bad_browser_executable.js", |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +def test_browser_type_launch_should_reject_if_executable_path_is_invalid( |
| 44 | + browser_type: BrowserType, launch_arguments: Dict |
| 45 | +) -> None: |
| 46 | + with pytest.raises(Error) as exc: |
| 47 | + browser_type.launch(**launch_arguments, executable_path="random-invalid-path") |
| 48 | + assert "executable doesn't exist" in exc.value.message |
| 49 | + |
| 50 | + |
| 51 | +def test_browser_type_executable_path_should_work( |
| 52 | + browser_type: BrowserType, browser_channel: str |
| 53 | +) -> None: |
| 54 | + if browser_channel: |
| 55 | + return |
| 56 | + executable_path = browser_type.executable_path |
| 57 | + assert os.path.exists(executable_path) |
| 58 | + assert os.path.realpath(executable_path) == os.path.realpath(executable_path) |
| 59 | + |
| 60 | + |
| 61 | +def test_browser_type_name_should_work( |
| 62 | + browser_type: BrowserType, is_webkit: bool, is_firefox: bool, is_chromium: bool |
| 63 | +) -> None: |
| 64 | + if is_webkit: |
| 65 | + assert browser_type.name == "webkit" |
| 66 | + elif is_firefox: |
| 67 | + assert browser_type.name == "firefox" |
| 68 | + elif is_chromium: |
| 69 | + assert browser_type.name == "chromium" |
| 70 | + else: |
| 71 | + raise ValueError("Unknown browser") |
| 72 | + |
| 73 | + |
| 74 | +def test_browser_close_should_fire_close_event_for_all_contexts( |
| 75 | + browser_type: BrowserType, launch_arguments: Dict |
| 76 | +) -> None: |
| 77 | + browser = browser_type.launch(**launch_arguments) |
| 78 | + context = browser.new_context() |
| 79 | + closed = [] |
| 80 | + context.on("close", lambda _: closed.append(True)) |
| 81 | + browser.close() |
| 82 | + assert closed == [True] |
| 83 | + |
| 84 | + |
| 85 | +def test_browser_close_should_be_callable_twice( |
| 86 | + browser_type: BrowserType, launch_arguments: Dict |
| 87 | +) -> None: |
| 88 | + browser = browser_type.launch(**launch_arguments) |
| 89 | + browser.close() |
| 90 | + browser.close() |
| 91 | + |
| 92 | + |
| 93 | +@pytest.mark.only_browser("chromium") |
| 94 | +def test_browser_launch_should_return_background_pages( |
| 95 | + browser_type: BrowserType, |
| 96 | + tmpdir: Path, |
| 97 | + browser_channel: Optional[str], |
| 98 | + assetdir: Path, |
| 99 | + launch_arguments: Dict, |
| 100 | +) -> None: |
| 101 | + if browser_channel: |
| 102 | + pytest.skip() |
| 103 | + |
| 104 | + extension_path = str(assetdir / "simple-extension") |
| 105 | + context = browser_type.launch_persistent_context( |
| 106 | + str(tmpdir), |
| 107 | + **{ |
| 108 | + **launch_arguments, |
| 109 | + "headless": False, |
| 110 | + "args": [ |
| 111 | + f"--disable-extensions-except={extension_path}", |
| 112 | + f"--load-extension={extension_path}", |
| 113 | + ], |
| 114 | + }, |
| 115 | + ) |
| 116 | + background_page = None |
| 117 | + if len(context.background_pages): |
| 118 | + background_page = context.background_pages[0] |
| 119 | + else: |
| 120 | + background_page = context.wait_for_event("backgroundpage") |
| 121 | + assert background_page |
| 122 | + assert background_page in context.background_pages |
| 123 | + assert background_page not in context.pages |
| 124 | + context.close() |
| 125 | + assert len(context.background_pages) == 0 |
| 126 | + assert len(context.pages) == 0 |
0 commit comments