03 / Web / 30 min READ + PRACTICE
Browser and API automation.
Automate repeatable checks against a local app with requests or a browser test tool.
Choose the right level
Use a simple HTTP client when testing an API response. Use browser automation when you must exercise rendering, JavaScript or keyboard interaction. Tests should assert outcomes, not merely click buttons.
Build a small check
Create a local status check that reports the response code and fails cleanly if the service is unavailable. Use a short timeout and a fixed loopback URL; this is a regression check, not an internet-wide crawler.
from urllib.request import urlopen
from urllib.error import URLError
try:
with urlopen('http://127.0.0.1:8000/', timeout=3) as response:
assert response.status == 200
print('Local app responded')
except URLError as exc:
print('Local app unavailable:', exc.reason) Make runs reproducible
Seed test data, limit request volume and clean up after every run. Do not hard-code a real password in scripts; use dummy test credentials from a local environment file excluded from version control.
Try it in your lab
Write a localhost response-code test for both an existing path and a missing path; assert the expected 200 and 404.
Make sure it clicks
When does a browser automation test teach you something an HTTP-only check cannot?
A prompt you can adapt
Add clear assertions and a timeout to my localhost-only web check; keep fixtures deterministic and redact any tokens from failures.How to set up OpenCode ↗