-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] Make PDF testing idiomatic #7822
Comments
@mxschmitt Did you also take a look at the solution I proposed in case people are not able to change the source code of the application under test? #6342 (comment) This uses the download event for headless chrome and the page event for all other browsers that use the integrated PDF reader. |
@corradin yes I've seen that. Creative solution! Question from our side, what in your opinion gets mostly asserted / tested when working with PDF files? waitForResponse() seems working in all the cases but it seems important to assert the suggested file name? |
I'd love an option on context, like Thanks! |
@mxschmitt. Not sure, I would like to assert that the stream a user gets back is of type pdf. File name does not matter. In my case the file name is autogenerated so I could not do an exact match either way. |
Can we expect a simpler solution to PDF Viewer issue any time soon? |
Do you have a python version of the workaround? |
Here's a python version of the workaround: import re
from playwright.sync_api import Page, expect
def test_download_pdf(page: Page):
page.goto("https://www.example.com")
def handle_pdf(route):
response = page.context.request.get(route.request)
route.fulfill(
response,
headers={**response.headers, "Content-Disposition": "attachment"},
)
page.route(re.compile(r".*\.pdf"), lambda route: handle_pdf(route))
with page.expect_download() as download_info:
page.get_by_text("click_here_to_download_pdf").click() |
What plug-ins do I lack?
error 1 argument
is that true? |
The answer from @vincenzo-gasparo only works if the HTTP method is GET, also it always removes the filename, even if present. This solution should work for any use case: from playwright.sync_api import Route, Request # or from async_api
def override_content_disposition_handler(route: Route, request: Request) -> None:
response = route.fetch() # performs the request
overridden_headers = {
**response.headers,
"content-disposition": response.headers.get('content-disposition', 'attachment').replace('inline', 'attachment')
}
route.fulfill(response=response, headers=overridden_headers) If you need to modify request method, headers or payload use route.continue_(...). Also, there is no need to wrap the method in a lambda: page.route("**/*.pdf", handler=override_content_disposition_handler) |
@beegotsy
To automate the process of saving the PDF, I attempted to invoke the print function using evaluate on the popup page object, like so: This approach successfully triggers the print dialog, which includes an option to save the document as a PDF. However, this method requires manual intervention to input a filename and confirm the save action, which prevents the automation from completing without user input. |
Customers are confused, when a PDF results in a PDF viewer and when in a download event. We should explain how to workaround it in the relevant browsers.
To make it work out of the box the following changes are required:
Workaround for Chromium for now:
The text was updated successfully, but these errors were encountered: