Best Python code snippet using playwright-python
_assertions.py
Source:_assertions.py
...56 self._actual_page = page57 @property58 def _not(self) -> "PageAssertions":59 return PageAssertions(self._actual_page, not self._is_not)60 async def to_have_title(61 self, title_or_reg_exp: Union[Pattern, str], timeout: float = None62 ) -> None:63 expected_values = to_expected_text_values(64 [title_or_reg_exp], normalize_white_space=True65 )66 __tracebackhide__ = True67 await self._expect_impl(68 "to.have.title",69 FrameExpectOptions(expectedText=expected_values, timeout=timeout),70 title_or_reg_exp,71 "Page title expected to be",72 )73 async def not_to_have_title(74 self, title_or_reg_exp: Union[Pattern, str], timeout: float = None75 ) -> None:76 __tracebackhide__ = True77 await self._not.to_have_title(title_or_reg_exp, timeout)78 async def to_have_url(79 self, url_or_reg_exp: Union[str, Pattern], timeout: float = None80 ) -> None:81 __tracebackhide__ = True82 base_url = self._actual_page.context._options.get("baseURL")83 if isinstance(url_or_reg_exp, str) and base_url:84 url_or_reg_exp = urljoin(base_url, url_or_reg_exp)85 expected_text = to_expected_text_values([url_or_reg_exp])86 await self._expect_impl(87 "to.have.url",88 FrameExpectOptions(expectedText=expected_text, timeout=timeout),89 url_or_reg_exp,90 "Page URL expected to be",91 )...
base.py
Source:base.py
...58 expect(locator).to_have_attribute("aria-checked", "false")59class Path(Base):60 def home(self):61 self.page.goto('/', wait_until="networkidle")62 expect(self.page).to_have_title("Home | Colorful")63 expect(self.page).to_have_url("https://www.colorful.app/")64 def showcase(self):65 self.page.goto('/showcases/showcase', wait_until="networkidle")66 expect(self.page).to_have_title("colorful.app")67 expect(self.page).to_have_url("https://www.colorful.app/showcases/showcase")68 def packshot_generator(self):69 self.page.goto('https://packshot.colorful.app/', wait_until="networkidle")70 expect(self.page).to_have_title("Colorful - Create realistic packshots using a 3D model")71 expect(self.page).to_have_url("https://packshot.colorful.app/")72 def careers(self):73 self.page.goto('/careers', wait_until="networkidle")74 expect(self.page).to_have_title("Careers")75 expect(self.page).to_have_url("https://www.colorful.app/careers")76class Background(Base):77 @property78 def colorpicker(self):79 return self.page.locator(".chrome-picker ")80 @property81 def bg_button(self):82 locator = self.page.locator('.w-10') # rework83 return self.get_visible_element(locator)84 def open_colorpicker(self):85 if not self.colorpicker.is_visible():86 self.bg_button.click()87 self.colorpicker.wait_for(state="visible")88 def close_colorpicker(self):...
test_assertions.py
Source:test_assertions.py
...15from datetime import datetime16import pytest17from playwright.sync_api import Browser, Page, expect18from tests.server import Server19def test_assertions_page_to_have_title(page: Page, server: Server) -> None:20 page.goto(server.EMPTY_PAGE)21 page.set_content("<title>new title</title>")22 expect(page).to_have_title("new title")23 expect(page).to_have_title(re.compile("new title"))24 with pytest.raises(AssertionError):25 expect(page).to_have_title("not the current title", timeout=100)26 with pytest.raises(AssertionError):27 expect(page).to_have_title(re.compile("not the current title"), timeout=100)28 with pytest.raises(AssertionError):29 expect(page).not_to_have_title(re.compile("new title"), timeout=100)30 with pytest.raises(AssertionError):31 expect(page).not_to_have_title("new title", timeout=100)32 expect(page).not_to_have_title("great title", timeout=100)33 page.evaluate(34 """35 setTimeout(() => {36 document.title = 'great title';37 }, 2000);38 """39 )40 expect(page).to_have_title("great title")41 expect(page).to_have_title(re.compile("great title"))42def test_assertions_page_to_have_url(page: Page, server: Server) -> None:43 page.goto(server.EMPTY_PAGE)44 expect(page).to_have_url(server.EMPTY_PAGE)45 expect(page).to_have_url(re.compile(r".*/empty\.html"))46 with pytest.raises(AssertionError):47 expect(page).to_have_url("nooooo", timeout=100)48 with pytest.raises(AssertionError):49 expect(page).to_have_url(re.compile("not-the-url"), timeout=100)50 page.evaluate(51 """52 setTimeout(() => {53 window.location = window.location.origin + '/grid.html';54 }, 2000);55 """...
test_search.py
Source:test_search.py
...33 expect(result_page.search_input).to_have_value(phrase)34 # And the search result links pertain to the phrase35 assert result_page.result_link_titles_contain_phrase(phrase)36 # And the search result title contains the phrase...
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!!