def test_assertions_page_to_have_url_with_base_url(browser: Browser, server: Server) -> None: page = browser.new_page(base_url=server.PREFIX) page.goto("/empty.html") expect(page).to_have_url("/empty.html") expect(page).to_have_url(re.compile(r".*/empty\.html")) page.close()
def test_response_security_details( browser: Browser, https_server: Server, browser_name, is_win, is_linux ): if browser_name == "webkit" and is_linux: pytest.skip("https://github.com/microsoft/playwright/issues/6759") page = browser.new_page(ignore_https_errors=True) response = page.goto(https_server.EMPTY_PAGE) response.finished() security_details = response.security_details() assert security_details if browser_name == "webkit" and is_win: assert security_details == { "subjectName": "puppeteer-tests", "validFrom": 1550084863, "validTo": -1, } elif browser_name == "webkit": assert security_details == { "protocol": "TLS 1.3", "subjectName": "puppeteer-tests", "validFrom": 1550084863, "validTo": 33086084863, } else: assert security_details == { "issuer": "puppeteer-tests", "protocol": "TLS 1.3", "subjectName": "puppeteer-tests", "validFrom": 1550084863, "validTo": 33086084863, } page.close()
def reporting_fixture(browser: Browser, request): p: Page = browser.new_page() # p: Page = browser.new_page(record_video_dir="video/") p.context.clear_cookies() yield p screenshot = p.screenshot(path=f"screenshots/{request.node.name}.png", full_page=True) # video = p.video.path() p.close() allure.attach(screenshot, name=f"{request.node.name}", attachment_type=allure.attachment_type.PNG)
def test_record_video_to_path(browser: Browser, tmpdir: Path, server: Server) -> None: page = browser.new_page(record_video_dir=tmpdir) page.goto(server.PREFIX + "/grid.html") video = page.video assert video path = video.path() assert str(tmpdir) in str(path) page.wait_for_timeout(1000) page.context.close() assert os.path.exists(path)
def test_should_expose_video_path(browser: Browser, tmpdir: Path, server: Server) -> None: page = browser.new_page(record_video_dir=tmpdir, record_video_size={ "width": 100, "height": 200 }) page.goto(server.PREFIX + "/grid.html") video = page.video assert video path = video.path() assert repr(page.video) == f"<Video page={page}>" assert str(tmpdir) in str(path) page.wait_for_timeout(1000) page.context.close()
def test_should_work_for_ssl( browser: Browser, https_server: Server, is_mac: bool, is_webkit: bool ) -> None: if is_webkit and is_mac: pytest.skip() page = browser.new_page(ignore_https_errors=True) with page.expect_event("requestfinished") as request_info: page.goto(https_server.EMPTY_PAGE) request = request_info.value timing = request.timing verify_connections_timing_consistency(timing) assert timing["requestStart"] >= timing["connectEnd"] assert timing["responseStart"] >= timing["requestStart"] assert timing["responseEnd"] >= timing["responseStart"] assert timing["responseEnd"] < 10000 page.close()
def get_shutterstock_image_by_id(id: str, output_dir: str, browser: Browser = None): page = browser.new_page() page.goto("https://www.shutterstock.com/") search_input = page.locator("input[type=\"search\"]") search_input.fill(str(id)) search_button = page.locator("button[aria-label=\"Search\"]") search_button.click() div_a_wrapper = page.locator( "div[data-automation=\"AssetGrids_MosaicAssetGrid_div\"]").first src = div_a_wrapper.locator('img').first.get_attribute('src') if not os.path.isdir(output_dir): os.mkdir(output_dir) file_location = os.path.join(output_dir, os.path.basename(src)) urllib.request.urlretrieve(src, file_location) page.close() return ImageInfo(id, file_location)
def logout_fixture(browser: Browser, request): # p: Page = browser.new_page() p: Page = browser.new_page(record_video_dir="video/") p.context.clear_cookies() yield p # Logout # main_page = MainPage(base_url, p) # if p.inner_text(main_page.account_button(username).selector) is not None or "": # settings_page = main_page.open_settings() # settings_page.logout() screenshot = p.screenshot(path=f"screenshots/{request.node.name}.png", full_page=True) video = p.video.path() p.close() allure.attach(screenshot, name=f"{request.node.name}", attachment_type=allure.attachment_type.PNG) allure.attach.file(f'./{video}', attachment_type=allure.attachment_type.WEBM)
def get_shutterstock_image_by_image(image_location: str, output_dir: str, browser: Browser = None): page = browser.new_page() page.goto("https://www.shutterstock.com/") listbox = page.locator("[aria-haspopup=\"listbox\"][aria-label=\"Image\"]") listbox.click() listbox_item = page.locator("[data-value=\"reverseImageSearch\"]") listbox_item.click() file_input = page.locator("input[type=\"file\"]") file_input.set_input_files(image_location) imageLink = page.locator("a[data-track-label=\"gridItem\"]").first image_info = imageLink.get_attribute('data-track-value') id = json.loads(image_info)['id'] src = imageLink.locator('img').first.get_attribute('src') if not os.path.isdir(output_dir): os.mkdir(output_dir) file_location = os.path.join(output_dir, os.path.basename(src)) urllib.request.urlretrieve(src, file_location) page.close() return ImageInfo(id, file_location)
def test_should_work_for_ssl(browser: Browser, https_server: Server, is_mac: bool, is_webkit: bool) -> None: if is_webkit and is_mac: pytest.skip() page = browser.new_page(ignore_https_errors=True) with page.expect_event("requestfinished") as request_info: page.goto(https_server.EMPTY_PAGE) request = request_info.value timing = request.timing if not (is_webkit and is_mac): assert timing["domainLookupStart"] >= -1 assert timing["domainLookupEnd"] >= timing["domainLookupStart"] assert timing["connectStart"] >= timing["domainLookupEnd"] assert timing["secureConnectionStart"] > timing["connectStart"] assert timing["connectEnd"] > timing["secureConnectionStart"] assert timing["requestStart"] >= timing["connectEnd"] assert timing["responseStart"] > timing["requestStart"] assert timing["responseEnd"] >= timing["responseStart"] assert timing["responseEnd"] < 10000 page.close()
def test_sync_download(browser: Browser, server): server.set_route( "/downloadWithFilename", lambda request: ( request.setHeader("Content-Type", "application/octet-stream"), request.setHeader("Content-Disposition", "attachment; filename=file.txt"), request.write(b"Hello world"), request.finish(), ), ) page = browser.new_page(accept_downloads=True) page.set_content(f'<a href="{server.PREFIX}/downloadWithFilename">download</a>') with page.expect_event("download") as download: page.click("a") assert download.value assert download.value.suggested_filename == "file.txt" path = download.value.path() assert os.path.isfile(path) with open(path, "r") as fd: assert fd.read() == "Hello world" page.close()