Build a web page scraping and testing service using GraphQL and Playwright.
In my previous article, I walked through with you on developing serverless APIs to test web pages under different resolutions using
Puppeteer. In this article let’s use
Playwright, which is a similar library to perform web browser automation.
Playwright is a library available in Node.js, Python, and Java to automate
WebKit with a single API. It is built to enable cross-browser web automation that is ever-green, capable, reliable, and fast.
Playwright and browser binaries for
Playwright requires Python 3.7+.
$ pip install playwright
$ playwright install
Install Python Libraries
pip install -r requirements.txt to install the libraries.
GraphQL API to Capture Web Page with Specific ViewPort
Below is the FastAPI source code which
- exposes a GraphQL query endpoint that accepts URL, width, and height parameters.
Playwrightto capture the web page with the preferred width and height.
- returns a base64-encoded PNG image string.