Best Python code snippet using playwright-python
test_page.py
Source:test_page.py
...683 await page.evaluate("window.Event = null")684 await page.select_option("select", "blue")685 assert await page.evaluate("result.onInput") == ["blue"]686 assert await page.evaluate("result.onChange") == ["blue"]687async def give_it_a_chance_to_fill(page):688 for i in range(5):689 await page.evaluate(690 "() => new Promise(f => requestAnimationFrame(() => requestAnimationFrame(f)))"691 )692async def test_fill_should_fill_textarea(page, server):693 await page.goto(server.PREFIX + "/input/textarea.html")694 await page.fill("textarea", "some value")695 assert await page.evaluate("result") == "some value"696@pytest.mark.skip_browser("webkit")697async def test_fill_should_fill_input(page, server):698 # Disabled as in upstream, we should validate time in the Playwright lib699 await page.goto(server.PREFIX + "/input/textarea.html")700 await page.fill("input", "some value")701 assert await page.evaluate("result") == "some value"702async def test_fill_should_throw_on_unsupported_inputs(page, server):703 await page.goto(server.PREFIX + "/input/textarea.html")704 for type in [705 "button",706 "checkbox",707 "file",708 "image",709 "radio",710 "range",711 "reset",712 "submit",713 ]:714 await page.eval_on_selector(715 "input", "(input, type) => input.setAttribute('type', type)", type716 )717 with pytest.raises(Error) as exc_info:718 await page.fill("input", "")719 assert f'input of type "{type}" cannot be filled' in exc_info.value.message720async def test_fill_should_fill_different_input_types(page, server):721 await page.goto(server.PREFIX + "/input/textarea.html")722 for type in ["password", "search", "tel", "text", "url"]:723 await page.eval_on_selector(724 "input", "(input, type) => input.setAttribute('type', type)", type725 )726 await page.fill("input", "text " + type)727 assert await page.evaluate("result") == "text " + type728async def test_fill_should_fill_date_input_after_clicking(page, server):729 await page.set_content("<input type=date>")730 await page.click("input")731 await page.fill("input", "2020-03-02")732 assert await page.eval_on_selector("input", "input => input.value") == "2020-03-02"733@pytest.mark.skip_browser("webkit")734async def test_fill_should_throw_on_incorrect_date(page, server):735 # Disabled as in upstream, we should validate time in the Playwright lib736 await page.set_content("<input type=date>")737 with pytest.raises(Error) as exc_info:738 await page.fill("input", "2020-13-05")739 assert "Malformed value" in exc_info.value.message740async def test_fill_should_fill_time_input(page, server):741 await page.set_content("<input type=time>")742 await page.fill("input", "13:15")743 assert await page.eval_on_selector("input", "input => input.value") == "13:15"744@pytest.mark.skip_browser("webkit")745async def test_fill_should_throw_on_incorrect_time(page, server):746 # Disabled as in upstream, we should validate time in the Playwright lib747 await page.set_content("<input type=time>")748 with pytest.raises(Error) as exc_info:749 await page.fill("input", "25:05")750 assert "Malformed value" in exc_info.value.message751async def test_fill_should_fill_datetime_local_input(page, server):752 await page.set_content("<input type=datetime-local>")753 await page.fill("input", "2020-03-02T05:15")754 assert (755 await page.eval_on_selector("input", "input => input.value")756 == "2020-03-02T05:15"757 )758@pytest.mark.only_browser("chromium")759async def test_fill_should_throw_on_incorrect_datetime_local(page):760 await page.set_content("<input type=datetime-local>")761 with pytest.raises(Error) as exc_info:762 await page.fill("input", "abc")763 assert "Malformed value" in exc_info.value.message764async def test_fill_should_fill_contenteditable(page, server):765 await page.goto(server.PREFIX + "/input/textarea.html")766 await page.fill("div[contenteditable]", "some value")767 assert (768 await page.eval_on_selector("div[contenteditable]", "div => div.textContent")769 == "some value"770 )771async def test_fill_should_fill_elements_with_existing_value_and_selection(772 page, server773):774 await page.goto(server.PREFIX + "/input/textarea.html")775 await page.eval_on_selector("input", "input => input.value = 'value one'")776 await page.fill("input", "another value")777 assert await page.evaluate("result") == "another value"778 await page.eval_on_selector(779 "input",780 """input => {781 input.selectionStart = 1782 input.selectionEnd = 2783 }""",784 )785 await page.fill("input", "maybe this one")786 assert await page.evaluate("result") == "maybe this one"787 await page.eval_on_selector(788 "div[contenteditable]",789 """div => {790 div.innerHTML = 'some text <span>some more text<span> and even more text'791 range = document.createRange()792 range.selectNodeContents(div.querySelector('span'))793 selection = window.getSelection()794 selection.removeAllRanges()795 selection.addRange(range)796 }""",797 )798 await page.fill("div[contenteditable]", "replace with this")799 assert (800 await page.eval_on_selector("div[contenteditable]", "div => div.textContent")801 == "replace with this"802 )803async def test_fill_should_throw_when_element_is_not_an_input_textarea_or_contenteditable(804 page, server805):806 await page.goto(server.PREFIX + "/input/textarea.html")807 with pytest.raises(Error) as exc_info:808 await page.fill("body", "")809 assert "Element is not an <input>" in exc_info.value.message810async def test_fill_should_throw_if_passed_a_non_string_value(page, server):811 await page.goto(server.PREFIX + "/input/textarea.html")812 with pytest.raises(Error) as exc_info:813 await page.fill("textarea", 123)814 assert "expected string, got number" in exc_info.value.message815async def test_fill_should_retry_on_disabled_element(page, server):816 await page.goto(server.PREFIX + "/input/textarea.html")817 await page.eval_on_selector("input", "i => i.disabled = true")818 done = []819 async def fill():820 await page.fill("input", "some value")821 done.append(True)822 promise = asyncio.create_task(fill())823 await give_it_a_chance_to_fill(page)824 assert done == []825 assert await page.evaluate("result") == ""826 await page.eval_on_selector("input", "i => i.disabled = false")827 await promise828 assert await page.evaluate("result") == "some value"829async def test_fill_should_retry_on_readonly_element(page, server):830 await page.goto(server.PREFIX + "/input/textarea.html")831 await page.eval_on_selector("textarea", "i => i.readOnly = true")832 done = []833 async def fill():834 await page.fill("textarea", "some value")835 done.append(True)836 promise = asyncio.create_task(fill())837 await give_it_a_chance_to_fill(page)838 assert done == []839 assert await page.evaluate("result") == ""840 await page.eval_on_selector("textarea", "i => i.readOnly = false")841 await promise842 assert await page.evaluate("result") == "some value"843async def test_fill_should_retry_on_invisible_element(page, server):844 await page.goto(server.PREFIX + "/input/textarea.html")845 await page.eval_on_selector("input", "i => i.style.display = 'none'")846 done = []847 async def fill():848 await page.fill("input", "some value")849 done.append(True)850 promise = asyncio.create_task(fill())851 await give_it_a_chance_to_fill(page)852 assert done == []853 assert await page.evaluate("result") == ""854 await page.eval_on_selector("input", "i => i.style.display = 'inline'")855 await promise856 assert await page.evaluate("result") == "some value"857async def test_fill_should_be_able_to_fill_the_body(page):858 await page.set_content('<body contentEditable="true"></body>')859 await page.fill("body", "some value")860 assert await page.evaluate("document.body.textContent") == "some value"861async def test_fill_should_fill_fixed_position_input(page):862 await page.set_content('<input style="position: fixed;" />')863 await page.fill("input", "some value")864 assert await page.evaluate("document.querySelector('input').value") == "some value"865async def test_fill_should_be_able_to_fill_when_focus_is_in_the_wrong_frame(page):...
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!!