Best Python code snippet using playwright-python
test_v1.py
Source:test_v1.py
...14 if not isfile(path):15 raise FileNotFoundError()16 with open(path) as file:17 return file.read()18async def async_readfile(path: str):19 if not isfile(path):20 raise FileNotFoundError()21 async with async_open(path) as file:22 return await file.read()23pt_test_file_path = "tests/data/pt.json"24en_test_file_path = "tests/data/en.json"25class TestLanguageImportationExportation:26 # Sync27 def test_sync_import_invalid_language_dict(self):28 with pytest.raises(globalprogramlib.utils.errors.InvalidLanguageData):29 assert Language().from_dict({}) is None30 def test_sync_import_invalid_language_json(self):31 with pytest.raises(globalprogramlib.utils.errors.InvalidLanguageData):32 assert (33 Language().from_json(sync_readfile("tests/data/invalid/file.txt"))34 is None35 )36 def test_sync_import_invalid_language_file(self):37 with pytest.raises(globalprogramlib.utils.errors.InvalidLanguageData):38 assert Language().from_file("tests/data/invalid/file.txt") is None39 def test_sync_import_incompatible_language_dict(self):40 with pytest.raises(globalprogramlib.utils.errors.IncompatibleLanguageVersion):41 assert (42 Language().from_dict(loads(sync_readfile("tests/data/invalid/it.json")))43 is None44 )45 def test_sync_import_incompatible_language_json(self):46 with pytest.raises(globalprogramlib.utils.errors.IncompatibleLanguageVersion):47 assert (48 Language().from_json(sync_readfile("tests/data/invalid/it.json"))49 is None50 )51 def test_sync_import_incompatible_language_file(self):52 with pytest.raises(globalprogramlib.utils.errors.IncompatibleLanguageVersion):53 assert Language().from_file("tests/data/invalid/it.json") is None54 def test_sync_import_valid_language_dict(self, sync_pt_language: Language):55 assert (56 Language().from_dict(loads(sync_readfile(pt_test_file_path)))57 == sync_pt_language58 )59 def test_sync_import_valid_language_json(self, sync_pt_language: Language):60 assert (61 Language().from_json(sync_readfile(pt_test_file_path)) == sync_pt_language62 )63 def test_sync_import_valid_language_file(self, sync_pt_language: Language):64 file_language = Language().from_file(pt_test_file_path)65 assert file_language == sync_pt_language66 def test_sync_export_valid_language_dict(self, sync_pt_language: Language):67 assert loads(sync_readfile(pt_test_file_path)) == sync_pt_language.to_dict68 def test_sync_export_valid_language_json(self, sync_pt_language: Language):69 assert loads(sync_readfile(pt_test_file_path)) == loads(70 sync_pt_language.to_json71 )72 def test_sync_export_valid_language_file(self, sync_pt_language: Language):73 c = sync_pt_language.to_file("tests/pt_test.json")74 if c is None:75 raise FileNotFoundError(76 """Test failed because file can't be exported to tests directory77Please check permissions or verify the working state of method «to_file()» in the Language object"""78 )79 d = Language().from_file(c)80 remove(c)81 assert sync_pt_language == d82 # Async83 @pytest.mark.asyncio84 async def test_async_import_invalid_language_dict(self):85 with pytest.raises(globalprogramlib.utils.errors.InvalidLanguageData):86 assert await (await Language()).async_from_dict({}) is None87 @pytest.mark.asyncio88 async def test_async_import_invalid_language_json(self):89 with pytest.raises(globalprogramlib.utils.errors.InvalidLanguageData):90 assert (91 await (await Language()).async_from_json(92 await async_readfile("tests/data/invalid/file.txt")93 )94 is None95 )96 @pytest.mark.asyncio97 async def test_async_import_invalid_language_file(self):98 with pytest.raises(globalprogramlib.utils.errors.InvalidLanguageData):99 assert (100 await (await Language()).async_from_file("tests/data/invalid/file.txt")101 is None102 )103 @pytest.mark.asyncio104 async def test_async_import_incompatible_language_dict(self):105 with pytest.raises(globalprogramlib.utils.errors.IncompatibleLanguageVersion):106 assert (107 await (await Language()).async_from_dict(108 loads(await async_readfile("tests/data/invalid/it.json"))109 )110 is None111 )112 @pytest.mark.asyncio113 async def test_async_import_incompatible_language_json(self):114 with pytest.raises(globalprogramlib.utils.errors.IncompatibleLanguageVersion):115 assert (116 await (await Language()).async_from_json(117 await async_readfile("tests/data/invalid/it.json")118 )119 is None120 )121 @pytest.mark.asyncio122 async def test_async_import_incompatible_language_file(self):123 with pytest.raises(globalprogramlib.utils.errors.IncompatibleLanguageVersion):124 assert (125 await (await Language()).async_from_file("tests/data/invalid/it.json")126 is None127 )128 @pytest.mark.asyncio129 async def test_async_import_valid_language_dict(self, async_pt_language: Language):130 assert (131 await (await Language()).async_from_dict(132 loads(await async_readfile(pt_test_file_path))133 )134 == async_pt_language135 )136 @pytest.mark.asyncio137 async def test_async_import_valid_language_json(self, async_pt_language: Language):138 assert (139 await (await Language()).async_from_json(140 await async_readfile(pt_test_file_path)141 )142 == async_pt_language143 )144 @pytest.mark.asyncio145 async def test_async_import_valid_language_file(self, async_pt_language: Language):146 file_language = await (await Language()).async_from_file(pt_test_file_path)147 assert file_language == async_pt_language148 @pytest.mark.asyncio149 async def test_async_export_valid_language_dict(self, async_pt_language: Language):150 assert (151 loads(await async_readfile(pt_test_file_path))152 == await async_pt_language.async_to_dict153 )154 @pytest.mark.asyncio155 async def test_async_export_valid_language_json(self, async_pt_language: Language):156 assert loads(await async_readfile(pt_test_file_path)) == loads(157 await async_pt_language.async_to_json158 )159 @pytest.mark.asyncio160 async def test_async_export_valid_language_file(self, async_pt_language: Language):161 c = await async_pt_language.async_to_file("tests/pt_test.json")162 if c is None:163 raise FileNotFoundError(164 """Test failed because file can't be exported to tests directory165Please check permissions or verify the working state of method «to_file()» in the Language object"""166 )167 d = await (await Language()).async_from_file(c)168 remove(c)169 assert async_pt_language == d170class TestLanguageAttributes:...
_frame.py
Source:_frame.py
...372 ) -> ElementHandle:373 params = locals_to_params(locals())374 if path:375 params["content"] = (376 (await async_readfile(path)).decode()377 + "\n//# sourceURL="378 + str(Path(path))379 )380 del params["path"]381 return from_channel(await self._channel.send("addScriptTag", params))382 async def add_style_tag(383 self, url: str = None, path: Union[str, Path] = None, content: str = None384 ) -> ElementHandle:385 params = locals_to_params(locals())386 if path:387 params["content"] = (388 (await async_readfile(path)).decode()389 + "\n/*# sourceURL="390 + str(Path(path))391 + "*/"392 )393 del params["path"]394 return from_channel(await self._channel.send("addStyleTag", params))395 async def click(396 self,397 selector: str,398 modifiers: List[KeyboardModifier] = None,399 position: Position = None,400 delay: float = None,401 button: MouseButton = None,402 clickCount: int = None,...
_fetch.py
Source:_fetch.py
...62 if "storageState" in params:63 storage_state = params["storageState"]64 if not isinstance(storage_state, dict) and storage_state:65 params["storageState"] = json.loads(66 (await async_readfile(storage_state)).decode()67 )68 if "extraHTTPHeaders" in params:69 params["extraHTTPHeaders"] = serialize_headers(params["extraHTTPHeaders"])70 context = cast(71 APIRequestContext,72 from_channel(await self.playwright._channel.send("newRequest", params)),73 )74 context._tracing._local_utils = self.playwright._utils75 return context76class APIRequestContext(ChannelOwner):77 def __init__(78 self, parent: ChannelOwner, type: str, guid: str, initializer: Dict79 ) -> None:80 super().__init__(parent, type, guid, initializer)...
_browser.py
Source:_browser.py
...202 if "storageState" in params:203 storageState = params["storageState"]204 if not isinstance(storageState, dict):205 params["storageState"] = json.loads(206 (await async_readfile(storageState)).decode()...
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!