Created
December 22, 2023 01:12
-
-
Save pligor/b739d2d32a2ca601a4eb8f9df77f4224 to your computer and use it in GitHub Desktop.
Proof of Concept to execute Playwright code cell by cell in a Jupyter Notebook, which speeds up development of new and especially existing scenarios exponentially
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"%load_ext autoreload\n", | |
"%autoreload 2" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# %pip install -q playwright\n", | |
"# !playwright install" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# from playwright.sync_api import sync_playwright #NOT working in Jupyter notebooks" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import asyncio\n", | |
"from playwright.async_api import async_playwright" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Typical production code for playwright" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"async def test_in_gr_website():\n", | |
" async with async_playwright() as pp:\n", | |
" # Launch the browser\n", | |
" browser = await pp.chromium.launch(headless=False)\n", | |
" page = await browser.new_page()\n", | |
"\n", | |
" # Navigate to the website\n", | |
" await page.goto(\"https://www.in.gr/\")\n", | |
"\n", | |
" # Get the content of the body tag\n", | |
" body_content = await page.inner_text(\"body\")\n", | |
"\n", | |
" # Assert that the body is not empty\n", | |
" assert body_content, \"The body of the webpage is empty.\"\n", | |
"\n", | |
" # Close the browser\n", | |
" await browser.close()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Step by step, cell by cell, to develop with ease" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"<Response url='https://www.in.gr/' request=<Request url='https://www.in.gr/' method='GET'>>" | |
] | |
}, | |
"execution_count": 18, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"playwright = await async_playwright().start()\n", | |
"browser = await playwright.chromium.launch(headless=False)\n", | |
"page = await browser.new_page()\n", | |
"await page.goto(\"https://www.in.gr/\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"38001" | |
] | |
}, | |
"execution_count": 20, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"body_content = await page.inner_text(\"body\")\n", | |
"len(body_content)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# do an assertion\n", | |
"assert len(body_content) > 0, \"The body of the webpage is empty\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Close the browser and Playwright\n", | |
"await browser.close()\n", | |
"await playwright.stop()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "whatenv", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.10.13" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment