|
| 1 | +# Use Playwright Codegen to Automatically Record UI Interactions |
| 2 | + |
| 3 | +Playwright has a neat feature of recording user interaction and automatically |
| 4 | +generate source code for you. This would save you a lot of time and automate many |
| 5 | +of your day to day work if the tasks can be done by a browser. |
| 6 | + |
| 7 | +The following example will show you how to record an UI operation that we visit |
| 8 | +the playwright.dev docs and search for this codegen feature and then move on to |
| 9 | +the next page: |
| 10 | + |
| 11 | +First, create a venv: |
| 12 | + |
| 13 | +```sh |
| 14 | +python3 -m venv venv |
| 15 | +``` |
| 16 | + |
| 17 | +Then, install the package |
| 18 | + |
| 19 | +```sh |
| 20 | +pip install pytest-playwright |
| 21 | +``` |
| 22 | + |
| 23 | +And run the codegen command: |
| 24 | + |
| 25 | +```sh |
| 26 | +playwright codegen |
| 27 | +``` |
| 28 | + |
| 29 | +> Note that you might need to run `playwright install` command if you haven't installed any chromium browser on your machine before |
| 30 | +
|
| 31 | +After running the codegen, you should now have two browsers opened, one is the main chromium browser and the other is a UI for recorded actions. |
| 32 | + |
| 33 | +Here is a piece of code that's generated by the codegen after running thru the actions that we mentioned in the above: |
| 34 | + |
| 35 | +```py |
| 36 | +import time |
| 37 | + |
| 38 | +from playwright.sync_api import Playwright, expect, sync_playwright |
| 39 | + |
| 40 | + |
| 41 | +def run(playwright: Playwright) -> None: |
| 42 | + browser = playwright.chromium.launch(headless=False) |
| 43 | + context = browser.new_context() |
| 44 | + page = context.new_page() |
| 45 | + page.goto("https://playwright.dev/") |
| 46 | + page.get_by_role("button", name="Search").click() |
| 47 | + page.get_by_placeholder("Search docs").fill("codegen") |
| 48 | + page.locator("#docsearch-item-0").get_by_role("link", |
| 49 | + name="Running Codegen Test generator").click() |
| 50 | + page.get_by_role( |
| 51 | + "heading", name="Running CodegenDirect link to Running Codegen").click() |
| 52 | + |
| 53 | + page.mouse.wheel(0, 4000) |
| 54 | + |
| 55 | + # These sleeps help to slow down the UI interactions so you can see what's actually done during the run |
| 56 | + time.sleep(2) |
| 57 | + page.locator(".pagination-nav__link--next").click() |
| 58 | + time.sleep(3) |
| 59 | + |
| 60 | + # --------------------- |
| 61 | + context.close() |
| 62 | + browser.close() |
| 63 | + |
| 64 | + |
| 65 | +with sync_playwright() as playwright: |
| 66 | + run(playwright) |
| 67 | + |
| 68 | +``` |
| 69 | + |
| 70 | +References: |
| 71 | + |
| 72 | +- [THIS is Playwrights BEST Feature for Web Automation |
| 73 | + ](https://www.youtube.com/watch?v=oPjfDkge8Vc&t=14s&ab_channel=JohnWatsonRooney) - Awesome video tutorial by John Rooney on how to work with codegen |
| 74 | +- [Running Codegen](https://playwright.dev/docs/codegen-intro#running-codegen) - Playwright docs for how to run codegen |
0 commit comments