Best Python code snippet using playwright-python
test_navigation.py
Source:test_navigation.py
...717 frame = page.frames[1]718 server.set_route("/empty.html", lambda _: None)719 with pytest.raises(Error) as exc_info:720 async with frame.expect_navigation():721 async def after_it():722 await server.wait_for_request("/empty.html")723 await page.eval_on_selector(724 "iframe", "frame => setTimeout(() => frame.remove(), 0)"725 )726 await asyncio.gather(727 page.eval_on_selector(728 "iframe",729 "frame => frame.contentWindow.location.href = '/empty.html'",730 ),731 after_it(),732 )733 assert "frame was detached" in exc_info.value.message734async def test_frame_wait_for_load_state_should_work(page, server):735 await page.goto(server.PREFIX + "/frames/one-frame.html")736 frame = page.frames[1]737 request_future = asyncio.Future()738 await page.route(739 server.PREFIX + "/one-style.css",740 lambda route, request: request_future.set_result(route),741 )742 await frame.goto(server.PREFIX + "/one-style.html", wait_until="domcontentloaded")743 request = await request_future744 load_task = asyncio.create_task(frame.wait_for_load_state())745 # give the promise a chance to resolve, even though it shouldn't...
Preprocess.py
Source:Preprocess.py
1import re2import codecs3import os4import string5class Preprocess:6 raw_doublequote = r"[ââ]"7 # strictly speaking, this where I plan to put code that normalizes punctuation8 # quotes in particular need a better solution9 def replace_doublequotes(self, text):10 text = re.sub(r'(?P<before_it>[\s\S])"(?P<after_it>[\s\S])', self.weird_double_quote_action, text)11 text = re.sub(r'(?P<before_it>"[\s\S])"(?P<after_it>[\s\S])', self.weird_double_quote_action, text)12 text = re.sub(r'"em\b', "'em", text)13 return re.sub(Preprocess.raw_doublequote, '"', text)14 def replace_singlequote(self, text):15 corrected = re.sub(r'(?P<before_it>[\s\S])[ââ`\'](?P<after_it>[\s\S])', self.single_quote_action, text)16 return corrected17 def odd_character_replacements(self, text, enc):18 text = re.sub(r'â¦', '...', text)19 text = re.sub(r'â', '-', text)20 return text21 def weird_double_quote_action(self, matchobj):22 replacement = '"'23 before = matchobj.group('before_it')24 after = matchobj.group('after_it')25 if before.lower().isalpha() and after.lower().isalpha():26 replacement = "'"27 return before + replacement + after28 def single_quote_action(self, matchobj):29 replacement = '"'30 before = matchobj.group('before_it')31 after = matchobj.group('after_it')32 if before.lower().isalpha() and after.lower().isalpha():33 replacement = "'"34 return before + replacement + after35byline = "by William Shakespeare"36target_folder = os.path.join("sources", "HorrorShow")37source_filename = os.path.join(target_folder, "Rocky_Horror.txt")38end_marker = "THE END"39sack = []40title = ""41# folder_list = os.listdir(target_folder)42# folder_list = [f for f in folder_list if f.endswith(".txt") and os.path.isfile(os.path.join(target_folder, f))]43# file_list = [os.path.join(target_folder, f) for f in folder_list]44file_list = [source_filename]45# file_list = file_list[4:5]46p = Preprocess()47for source_filename in file_list:48 file_size = min(32, os.path.getsize(source_filename))49 with open(source_filename, 'rb') as f_enc:50 raw = f_enc.read(file_size)51 if raw.startswith(codecs.BOM_UTF8):52 encoding = 'utf-8-sig'53 else:54 encoding = 'utf-8'55 full_doc = None56 with open(source_filename, 'r', encoding=encoding) as f_handle:57 print('reading doc', source_filename)58 full_doc = f_handle.read()59 print(type(full_doc))60 print(encoding)61 print('replacing text')62 # print(full_doc)63 full_doc = p.replace_singlequote(full_doc)64 full_doc = p.replace_doublequotes(full_doc)65 full_doc = p.odd_character_replacements(full_doc, encoding)66 with open(source_filename, 'w', encoding=encoding) as f_handle:67 f_handle.seek(0)68 print('rewriting doc', source_filename)69 f_handle.write(full_doc)...
file_monitor_pipe.py
Source:file_monitor_pipe.py
...33 def before(self, func):34 return Pipe(before_it(func, self))3536 def after(self, func):37 return Pipe(after_it(func, self))383940def repeat(value):41 value = value if callable else lambda: value42 while True:43 yield value()4445def before_it(func, iter1):46 for it in iter1:47 func()48 yield it4950def after_it(func, iter1):51 for it in iter1:52 yield it53 func()5455def ls_files(path):56 for entry in os.listdir(path):57 if os.path.isfile(os.path.join(path, entry)):58 yield entry5960def get_batch():61 return ls_files(STAGING_PATH)6263def after_batch():64 logging.info(f'Sleeping for {FILE_LIST_SLEEP_INTERVAL} seconds')
...
file_monitor.py
Source:file_monitor.py
...22 for it in iter1:23 func()24 yield it2526def after_it(func, iter1):27 for it in iter1:28 yield it29 func()3031def ls_files(path):32 for entry in os.listdir(path):33 if os.path.isfile(os.path.join(path, entry)):34 yield entry3536def get_batch():37 return ls_files(STAGING_PATH)3839def after_batch():40 logging.info(f'Sleeping for {FILE_LIST_SLEEP_INTERVAL} seconds')41 time.sleep(FILE_LIST_SLEEP_INTERVAL)4243def get_file_lines(file_name):44 with open(file_name) as file:45 for line in file:46 yield line4748def flat_map(func, it):49 return itertools.chain.from_iterable(map(func, it))5051p = re.compile('[^a-zA-Z]')525354batch_source = after_it(after_batch, repeater(get_batch))55file_names = flat_map(lambda x:x, batch_source)56file_names_with_path = map(lambda fn: os.path.join(STAGING_PATH, fn), file_names)57lines = flat_map(get_file_lines, file_names_with_path)58words = flat_map(lambda l: p.sub(' ', l).split(' '), lines)59words = filter(lambda w: len(w) > 0, words)60words = map(lambda w: w.lower().strip(), words)6162for entry in words:
...
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!!