Best Python code snippet using playwright-python
t_kermit.py
Source:t_kermit.py
...322 @patch('os.getcwd', new=Mock(return_value='/a/dir'))323 @patch('glob.glob', new=Mock(return_value=['aglobfile.txt']))324 @patch('os.path.isfile', new=Mock(return_value=True))325 @patch('os.remove')326 def test_should_delete_file(self, mock_remove):327 sert(mock_remove).not_called()328 k.local_delete('afile.txt')329 sert(mock_remove).called_once_with('/a/dir/aglobfile.txt')330 @patch('os.getcwd', new=Mock(return_value='/a/dir'))331 @patch('glob.glob', new=Mock(return_value=['aglobdir']))332 @patch('os.path.isfile', new=Mock(return_value=False))333 @patch('os.path.isdir', new=Mock(return_value=True))334 @patch('os.rmdir')335 def test_should_delete_dir(self, mock_rmdir):336 sert(mock_rmdir).not_called()337 k.local_delete('dirname')338 sert(mock_rmdir).called_once_with('/a/dir/aglobdir')339 @patch('os.getcwd', new=Mock(return_value='/a/dir'))340 @patch('glob.glob', new=Mock(return_value=['aglobfile.txt']))341 @patch('os.path.isfile', new=Mock(return_value=True))342 @patch('os.remove', new=Mock(side_effect=OSError('failed')))343 @patch('src.log.i')344 def test_should_raise_on_delete_file(self, mock_i):345 k.local_delete('afile.txt')346 sert(mock_i).called_once_with('Delete /a/dir/aglobfile.txt failed.')347 @patch('os.getcwd', new=Mock(return_value='/a/dir'))348 @patch('glob.glob', new=Mock(return_value=['aglobdir']))349 @patch('os.path.isfile', new=Mock(return_value=False))350 @patch('os.path.isdir', new=Mock(return_value=True))351 @patch('os.rmdir', new=Mock(side_effect=OSError('failed')))352 @patch('src.log.i')353 def test_should_raise_on_delete_dir(self, mock_i):354 k.local_delete('dirname')355 sert(mock_i).called_once_with('Delete /a/dir/aglobdir failed.')356 @patch('os.getcwd', new=Mock(return_value='/a/dir'))357 @patch('glob.glob', new=Mock(return_value=['aglobdir']))358 @patch('os.path.isfile', new=Mock(return_value=False))359 @patch('os.path.isdir', new=Mock(return_value=False))360 @patch('src.log.i')361 def test_should_raise_on_delete_dir(self, mock_i):362 k.local_delete('not_file_or_dir')363 sert(mock_i).called_once_with('/a/dir/aglobdir is not a file or dir. Ignored.')364def mtime(yr=1970, mon=1, day=1, hr=0, min=0):365 # Compute adjustment to params in order to handle UTC offset and Daylight Saving.366 dtdt = datetime.datetime367 utc_epoch = dtdt.fromtimestamp(0)368 local_epoch = dtdt(*[int(v) for v in utc_epoch.strftime('%Y %m %d %H %M').split()])369 offset = (utc_epoch - local_epoch).total_seconds()370 tm = dtdt(yr, mon, day, hr, min)371 return (tm - utc_epoch).total_seconds() + offset372def call_args_to_ary(call_args):373 ary = []374 for call in call_args:375 tup = call.args376 sert(len(tup)).to_equal(1)377 parts = tup[0].split()378 sert(len(parts)).to_equal(4)379 nam = parts[3]380 is_dir = False381 if nam.startswith('[') and nam.endswith(']'):382 nam = nam[1:-1]383 is_dir = True384 ary.append(TAttrBag(date=parts[0]+'_'+parts[1], size=parts[2], name=nam, isdir=is_dir))385 return ary386class TestLocalDirectory(Base):387 def setUp(self):388 super(TestLocalDirectory, self).setUp()389 self.stat_call_count = 0390 self.fake_files = [391 TAttrBag(name='ac', isdir=False, stat=TAttrBag(st_size=400, st_mtime=mtime(1982, 1, 1))),392 TAttrBag(name='ab', isdir=True, stat=TAttrBag(st_size=400, st_mtime=mtime(1982, 1, 1))),393 TAttrBag(name='aa', isdir=False, stat=TAttrBag(st_size=200, st_mtime=mtime(1986, 1, 1))),394 ]395 patch_getcwd = patch('os.getcwd')396 patch_isdir = patch('os.path.isdir')397 patch_listdir = patch('os.listdir')398 patch_stat = patch('os.stat')399 patch_glob = patch('glob.glob')400 self.addCleanup(patch_getcwd.stop)401 self.addCleanup(patch_isdir.stop)402 self.addCleanup(patch_listdir.stop)403 self.addCleanup(patch_stat.stop)404 self.addCleanup(patch_glob.stop)405 self.mock_getcwd = patch_getcwd.start()406 self.mock_isdir = patch_isdir.start()407 self.mock_listdir = patch_listdir.start()408 self.mock_stat = patch_stat.start()409 self.mock_glob = patch_glob.start()410 # For some reason, tho mocked 'os.stat' is not unpatched after the test411 # completes. This results in the mock being called, rather than the412 # real os.stat() function. callable_stat() works around this issue.413 def callable_stat(fname):414 if self.stat_call_count < len(self.fake_files):415 self.stat_call_count += 1416 return self.fake_files[self.stat_call_count - 1].stat417 return DEFAULT418 self.mock_getcwd.return_value = '/adir'419 self.mock_stat.side_effect = callable_stat # Work-around420 self.mock_listdir.return_value = [f.name for f in self.fake_files]421 self.mock_isdir.side_effect = [False] + [f.isdir for f in self.fake_files]422 self.mock_glob.side_effect = [['aa1'], ['bb1'], ['cc1']]423 @patch('src.log.i')424 def test_should_sort_by_date_size_name_asc(self, mock_i):425 k.config['order-by'].value = 0 # dsn426 k.config['oslope'].value = 1 # asc427 k.local_directory()428 sert(mock_i).called_n_times(3)429 ary = call_args_to_ary(src.log.i.call_args_list)430 prev = ary[0]431 name_checked = False432 for cur in ary[1:]:433 sert(cur.date >= prev.date).is_true()434 if (cur.date == prev.date):435 sert(cur.size >= prev.size).is_true()436 if (cur.size == prev.size):437 sert(cur.name > prev.name).is_true()438 name_checked = True439 prev = cur440 sert(name_checked).is_true()441 @patch('src.log.i')442 def test_should_sort_by_date_size_name_desc(self, mock_i):443 k.config['order-by'].value = 0 # dsn444 k.config['oslope'].value = -1 # desc445 k.local_directory()446 sert(mock_i).called_n_times(3)447 ary = call_args_to_ary(src.log.i.call_args_list)448 prev = ary[0]449 name_checked = False450 for cur in ary[1:]:451 sert(cur.date <= prev.date).is_true()452 if (cur.date == prev.date):453 sert(cur.size <= prev.size).is_true()454 if (cur.size == prev.size):455 sert(cur.name < prev.name).is_true()456 name_checked = True457 prev = cur458 sert(name_checked).is_true()459 @patch('src.log.i')460 def test_should_sort_by_date_name_asc(self, mock_i):461 k.config['order-by'].value = 2 # dn462 k.config['oslope'].value = 1 # asc463 k.local_directory()464 sert(mock_i).called_n_times(3)465 ary = call_args_to_ary(src.log.i.call_args_list)466 prev = ary[0]467 name_checked = False468 for cur in ary[1:]:469 sert(cur.date >= prev.date).is_true()470 if (cur.date == prev.date):471 sert(cur.name > prev.name).is_true()472 name_checked = True473 prev = cur474 sert(name_checked).is_true()475 @patch('src.log.i')476 def test_should_sort_by_date_name_desc(self, mock_i):477 k.config['order-by'].value = 2 # dn478 k.config['oslope'].value = -1 # desc479 k.local_directory()480 sert(mock_i).called_n_times(3)481 ary = call_args_to_ary(src.log.i.call_args_list)482 prev = ary[0]483 name_checked = False484 for cur in ary[1:]:485 sert(cur.date <= prev.date).is_true()486 if (cur.date == prev.date):487 sert(cur.name < prev.name).is_true()488 name_checked = True489 prev = cur490 sert(name_checked).is_true()491 @patch('src.log.i')492 def test_should_sort_by_size_name_asc(self, mock_i):493 k.config['order-by'].value = 3 # sn494 k.config['oslope'].value = 1 # asc495 k.local_directory()496 sert(mock_i).called_n_times(3)497 ary = call_args_to_ary(src.log.i.call_args_list)498 prev = ary[0]499 name_checked = False500 for cur in ary[1:]:501 sert(cur.size >= prev.size).is_true()502 if (cur.size == prev.size):503 sert(cur.name > prev.name).is_true()504 name_checked = True505 prev = cur506 sert(name_checked).is_true()507 @patch('src.log.i')508 def test_should_sort_by_size_name_desc(self, mock_i):509 k.config['order-by'].value = 3 # sn510 k.config['oslope'].value = -1 # desc511 k.local_directory()512 sert(mock_i).called_n_times(3)513 ary = call_args_to_ary(src.log.i.call_args_list)514 prev = ary[0]515 name_checked = False516 for cur in ary[1:]:517 sert(cur.size <= prev.size).is_true()518 if (cur.size == prev.size):519 sert(cur.name < prev.name).is_true()520 name_checked = True521 prev = cur522 sert(name_checked).is_true()523 @patch('src.log.i')524 def test_should_sort_by_size_date_name_asc(self, mock_i):525 k.config['order-by'].value = 1 # sdn526 k.config['oslope'].value = 1 # asc527 k.local_directory()528 sert(mock_i).called_n_times(3)529 ary = call_args_to_ary(src.log.i.call_args_list)530 prev = ary[0]531 name_checked = False532 for cur in ary[1:]:533 sert(cur.size >= prev.size).is_true()534 if (cur.size == prev.size):535 sert(cur.date >= prev.date).is_true()536 if (cur.date == prev.date):537 sert(cur.name > prev.name).is_true()538 name_checked = True539 prev = cur540 sert(name_checked).is_true()541 @patch('src.log.i')542 def test_should_sort_by_size_date_name_desc(self, mock_i):543 k.config['order-by'].value = 1 # sdn544 k.config['oslope'].value = -1 # desc545 k.local_directory()546 sert(mock_i).called_n_times(3)547 ary = call_args_to_ary(src.log.i.call_args_list)548 prev = ary[0]549 name_checked = False550 for cur in ary[1:]:551 sert(cur.size <= prev.size).is_true()552 if (cur.size == prev.size):553 sert(cur.date <= prev.date).is_true()554 if (cur.date == prev.date):555 sert(cur.name < prev.name).is_true()556 name_checked = True557 prev = cur558 sert(name_checked).is_true()559 @patch('src.log.i')560 def test_should_sort_by_name_asc(self, mock_i):561 k.config['order-by'].value = 4 # n562 k.config['oslope'].value = 1 # asc563 k.local_directory()564 sert(mock_i).called_n_times(3)565 ary = call_args_to_ary(src.log.i.call_args_list)566 prev = ary[0]567 name_checked = False568 for cur in ary[1:]:569 sert(cur.isdir <= prev.isdir).is_true()570 if (cur.isdir == prev.isdir):571 sert(cur.name > prev.name).is_true()572 name_checked = True573 prev = cur574 sert(name_checked).is_true()575 @patch('src.log.i')576 def test_should_sort_by_name_desc(self, mock_i):577 k.config['order-by'].value = 4 # n578 k.config['oslope'].value = -1 # desc579 k.local_directory()580 sert(mock_i).called_n_times(3)581 ary = call_args_to_ary(src.log.i.call_args_list)582 prev = ary[0]583 name_checked = False584 for cur in ary[1:]:585 sert(cur.isdir >= prev.isdir).is_true()586 if (cur.isdir == prev.isdir):587 sert(cur.name < prev.name).is_true()588 name_checked = True589 prev = cur590 sert(name_checked).is_true()591 @patch('src.log.i')592 def test_should_list_directory(self, mock_i):593 adir = '/a/dir/name'594 self.mock_isdir.side_effect = [True] + [f.isdir for f in self.fake_files]595 k.config['order-by'].value = 4 # n596 k.config['oslope'].value = 1 # asc597 k.local_directory(adir)598 sert(self.mock_listdir).called_once_with(adir)599 sert(mock_i).called_n_times(3)600 ary = call_args_to_ary(src.log.i.call_args_list)601 prev = ary[0]602 name_checked = False603 for cur in ary[1:]:604 sert(cur.isdir <= prev.isdir).is_true()605 if (cur.isdir == prev.isdir):606 sert(cur.name > prev.name).is_true()607 name_checked = True608 prev = cur609 sert(name_checked).is_true()610 @patch('src.log.i')611 def test_should_glob_filespec(self, mock_i):612 filespec = 'aa bb cc'613 k.config['order-by'].value = 4 # n614 k.config['oslope'].value = 1 # asc615 k.local_directory(filespec)616 sert(self.mock_glob).called_n_times(3)617 sert(self.mock_glob).nth_call_called_with(1, 'aa')618 sert(self.mock_glob).nth_call_called_with(2, 'bb')619 sert(self.mock_glob).nth_call_called_with(3, 'cc')620 ary = call_args_to_ary(mock_i.call_args_list)621 prev = ary[0]622 name_checked = False623 for cur in ary[1:]:624 sert(cur.isdir <= prev.isdir).is_true()625 if (cur.isdir == prev.isdir):626 sert(cur.name > prev.name).is_true()627 name_checked = True628 prev = cur629 sert(name_checked).is_true()630 @patch('src.util.key_for_val', new=Mock(return_value='z'))631 @patch('src.log.e')632 def test_should_handle_bad_sort_char(self, mock_e):633 k.local_directory()634 sert(mock_e).called_once_with('Bad field char {}'.format('z'))635class TestLocalPath(Base):636 @patch('os.getcwd')637 @patch('src.log.i')638 def test_should_delete_file(self, mock_i, mock_getcwd):639 mock_getcwd.return_value = 'curdir'640 k.local_path()641 sert(mock_i).called_once_with('curdir')642class TestLocalPush(Base):643 @patch('os.system')644 @patch('os.name', new_callable=Mock(return_value='posix'))645 def test_should_call_bash(self, mock_name, mock_system):646 sert(mock_system).not_called()647 k.local_push()648 sert(mock_system).called_once_with('bash')649 @patch('os.system')650 @patch('os.name', new_callable=Mock(return_value='nt'))651 def test_should_call_commandcom(self, mock_name, mock_system):652 sert(mock_system).not_called()...
test_download.py
Source:test_download.py
...246 download = await download_info.value247 path = await download.path()248 assert os.path.exists(path)249 await page.close()250async def test_should_delete_file(browser, server):251 page = await browser.new_page(accept_downloads=True)252 await page.set_content(f'<a href="{server.PREFIX}/download">download</a>')253 async with page.expect_download() as download_info:254 await page.click("a")255 download = await download_info.value256 path = await download.path()257 assert os.path.exists(path)258 await download.delete()259 assert os.path.exists(path) is False260 await page.close()261async def test_should_delete_downloads_on_context_destruction(browser, server):262 page = await browser.new_page(accept_downloads=True)263 await page.set_content(f'<a href="{server.PREFIX}/download">download</a>')264 async with page.expect_download() as download_info:...
test_backend.py
Source:test_backend.py
...20 ) -> None:21 await fs.save(f"nested/{trollface_file.filename}", trollface_file)22 assert await fs.exists(f"nested/{trollface_file.filename}") is True23 @pytest.mark.asyncio24 async def test_should_delete_file(25 self, fs: StorageBackend, trollface_file: File26 ) -> None:27 await fs.save(trollface_file.filename, trollface_file)28 await fs.delete(trollface_file.filename)29 assert await fs.exists(trollface_file.filename) is False30 @pytest.mark.asyncio31 async def test_should_read_file(32 self, fs: StorageBackend, trollface_file: File33 ) -> None:34 await fs.save(trollface_file.filename, trollface_file)35 await trollface_file.seek(0)36 assert await fs.read(trollface_file.filename) == await trollface_file.read()37class TestFilesystemStorage(BaseStorageTest):38 @pytest.fixture...
test_delete_file.py
Source:test_delete_file.py
1from test import cassette2from test.resources.documents import *3def test_should_delete_file():4 session = get_user_session()5 delete_all_documents()6 with cassette('fixtures/resources/files/delete_file/delete_file.yaml'):7 doc = create_document(session)8 file = doc.attach_file('fixtures/resources/files/basket.txt')9 file.delete()...
What is `AttributeError: 'ScrapyPlaywrightDownloadHandler' object has no attribute 'browser' in scrapy-playwright?
Scrapy-Playwright Integration in Windows environment
Playwright not loading all thumbnails on page
Playwright with python - Download file from CloudFlare
What's the best way to setup playwright in Apache Airflow in Docker?
Problem with selecting a specific web element with Playwright in Python
How to type F5 to refresh a page using Playwright Python
How to get outer html from python playwright locator object?
Python playwright - existing browser and variable in function
Playwright with python - Download file from CloudFlare
It means that your object of ScrapyPlaywrightDownloadHandler doesn't have an attribute named browser.
Check out the latest blogs from LambdaTest on this topic:
A good User Interface (UI) is essential to the quality of software or application. A well-designed, sleek, and modern UI goes a long way towards providing a high-quality product for your customers − something that will turn them on.
A productive workspace is crucial in crafting code rather than just finding the right IDE. After several generations of IDEs and code editors, Visual Studio Code is considered one of the best web development IDEs used by developers.
Selenium is still the most influential and well-developed framework for web automation testing. Being one of the best automation frameworks with constantly evolving features, it is poised to lead the industry in all aspects as compared to other trending frameworks like Cypress, Puppeteer, PlayWright, etc. Furthermore, using Selenium gives you the flexibility to use different programming languages like C#, Ruby, Perl, Java, Python, etc., and also accommodate different operating systems and web browsers for Selenium automation testing.
Websites and web apps are growing in number day by day, and so are the expectations of people for a pleasant web experience. Even though the World Wide Web (WWW) was invented only in 1989 (32 years back), this technology has revolutionized the world we know back then. The best part is that it has made life easier for us. You no longer have to stand in long queues to pay your bills. You can get that done within a few minutes by visiting their website, web app, or mobile app.
This article is a part of our Content Hub. For more in-depth resources, check out our content hub on Selenium Locators Tutorial.
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!!