Automating Playwright steps using ChatGPT(OpenAI) or DeepSeek in Python
Project description
🚀 Auto Playwright Python
Automating Playwright steps using ChatGPT(OpenAI) or DeepSeek in Python
This library is inspired by Luc Gagan's auto-playwright node.js library.
Setup
Install the library using pip:
pip install autoplaywright-python
Supported Browsers
Autoplaywright supports all browsers supported by Playwright, including:
- Chromium
- Firefox
- WebKit
Usage
Below are examples demonstrating how to use the library:
▶️ Async Example
import asyncio
from playwright.async_api import async_playwright
from pydantic import BaseModel
from autoplaywright.async_api.openai_client import AutoPlaywrightOpenAI
# Create an instance of the AI client using the GPT-4o-mini model
# Remember to replace 'OPENAI_API_KEY_HERE' with you your actual openai api key
ai = AutoPlaywrightOpenAI(model="gpt-4o-mini", api_key="OPENAI_API_KEY_HERE")
# Define a Pydantic model to store the price of the plan
class Plan(BaseModel):
price: float
# The main function that performs the sequence of operations in the browser
async def main():
# Launch Playwright in an asynchronous context
async with async_playwright() as playwright:
chromium = playwright.chromium
# Launch the Chromium browser in non-headless mode (i.e., the browser UI is visible)
browser = await chromium.launch(headless=False)
page = await browser.new_page()
# Open the test e-commerce website
await page.goto("https://webscraper.io/test-sites/e-commerce/allinone")
# Click the "Pricing" link in the header of the page using the AI command
# The AI model performs the task "Click on pricing" on the page
await ai.auto("Click on pricing", page)
# Select the most expensive plan and return its price as a Pydantic model (Plan)
# The AI model analyzes the page and extracts the relevant price
plan = await ai.auto("Return price of most expensive plan", page, Plan)
# Print the price of the most expensive plan to the console
print(plan.price)
# Close the page
await page.close()
# Run the main function asynchronously
asyncio.run(main())
▶️ Sync Example
from typing import List
from playwright.sync_api import sync_playwright
from pydantic import BaseModel
from autoplaywright.sync_api.openai_client import AutoPlaywrightOpenAI
# Create an instance of the AI client using the GPT-4o-mini model
# Remember to replace 'OPENAI_API_KEY_HERE' with you your actual openai api key
ai = AutoPlaywrightOpenAI(model="gpt-4o-mini", api_key="OPENAI_API_KEY_HERE")
# Define a Pydantic model for a person structure
class Person(BaseModel):
first_name: str
last_name: str
username: str
# Define a Pydantic model to store the people from the table
class Table(BaseModel):
people: List[Person]
# The main function that performs the sequence of operations in the browser
def main():
# Launch Playwright in an asynchronous context
with sync_playwright() as playwright:
chromium = playwright.chromium
# Launch the Chromium browser in non-headless mode (i.e., the browser UI is visible)
browser = chromium.launch(headless=False)
page = browser.new_page()
# Open the test e-commerce website
page.goto("https://webscraper.io/test-sites/tables")
# Select people from the table and return data as a Pydantic model (Table)
# The AI model analyzes the page and extracts the relevant price
table = ai.auto("Return people from the table", page, Table)
# People in the table
print(table.people)
# Close the page
page.close()
# Run the main function synchronously
main()
Supported actions
Action
ai.auto("Click on pricing button", page)
Query / Assert
# Define a Pydantic model for a person structure
class Person(BaseModel):
first_name: str
last_name: str
username: str
# Define a Pydantic model to store the people from the table
class Table(BaseModel):
people: List[Person]
# Select people from the table and return data as a Pydantic model (Table)
# The AI model analyzes the page and extracts the relevant price
table = ai.auto("Return people from the table", page, Table)
# People in the table
print(table.people)
Supported Playwright Actions
Here is a list of supported actions:
- add_init_script
- bring_to_front
- click
- close
- dblclick
- dispatch_event
- evaluate
- fill
- focus
- go_back
- go_forward
- goto
- hover
- press
- reload
- screenshot
- set_content
- set_viewport_size
- tap
- type
- uncheck
- wait_for_event
- wait_for_load_state
- wait_for_selector
- wait_for_timeout
- wait_for_url
Why use Auto Playwright Python?
| Aspect | Conventional Approach | Testing with Auto Playwright |
|---|---|---|
| Coupling with Markup | Strongly linked to the application's markup. | Eliminates the use of selectors; actions are determined by the AI assistant at runtime. |
| Speed of Implementation | Slower implementation due to the need for precise code translation for each action. | Rapid test creation using simple, plain text instructions for actions and assertions. |
| Handling Complex Scenarios | Automating complex scenarios is challenging and prone to frequent failures. | Facilitates testing of complex scenarios by focusing on the intended test outcomes. |
| Test Writing Timing | Can only write tests after the complete development of the functionality. | Enables a Test-Driven Development (TDD) approach, allowing test writing concurrent with or before functionality development. |
Implementation
The library optimizes AI query costs by shortening the HTML code before sending the query, reducing the token count and lowering costs associated with OpenAI and DeepSeek models.
Links
- Documentation (soon)
- GitHub
Made with ❤️ by Nohet.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file autoplaywright_python-1.0.3.tar.gz.
File metadata
- Download URL: autoplaywright_python-1.0.3.tar.gz
- Upload date:
- Size: 10.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
12e152d4048bbfd6a9961c41d8c28700e2eff49f6a4b2f10fa480d7458e27966
|
|
| MD5 |
d3e6d1f00f76d8f4dbd00b8aa73db538
|
|
| BLAKE2b-256 |
873d573f57065c2fe99fcde00650c1ab6ad6552d927fddad613e99dbfd4ac388
|
File details
Details for the file autoplaywright_python-1.0.3-py3-none-any.whl.
File metadata
- Download URL: autoplaywright_python-1.0.3-py3-none-any.whl
- Upload date:
- Size: 14.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
981f24b365a343c33077182952ae642e333057f506e48691a0895591fc0600a7
|
|
| MD5 |
8e191e2fb66255c887b9bef9d2d209b1
|
|
| BLAKE2b-256 |
94abc68ac404b3c2c53420e87c265e746ca2049dd6395b7350bac283de0a4523
|