How to prepare a website for automated tests
Updated:
A website is ready for automated tests when elements can be identified unambiguously, input data can be prepared repeatedly, and a scenario can be completed safely. Being able to click through the steps manually is not enough. Development and testing teams need to know the conditions for the run, the expected outcome, and how to restore state after a failure. Start with one important scenario and plan changes around the obstacles it reveals.
First, choose the process the test should verify
An illustrative example is an order in an online store. Manually, you select an available product, sign in, and confirm payment. An automated test may run alongside other tests, the product may no longer be in stock, and the payment gateway may require another verification step. Without preparation, you cannot tell whether the order, data, or environment failed.
Write down the scenario’s starting point, steps, and specific outcome. For an order, this could mean displaying its number and checking its saved state. Agree on a failure path too, such as a declined payment. Record the prerequisites: user role, product, interface language, and payment service configuration. This produces a list of necessary changes instead of a vague request to “prepare the whole website.”
How to find elements without depending on appearance
A selector determines how a test finds an element. A long chain of CSS classes or an exact position in the HTML ties the test to a structure that even a visual change can alter. Prefer locators based on a button’s role and accessible name, or a field’s label. The Playwright documentation also explains how to narrow the lookup to a particular form or product when elements repeat.
A label must actually be associated with the field, rather than simply displayed beside it. The W3C guide to labeling form controls shows the relationship using a label whose for matches the field’s id. Good HTML helps assistive-technology users as well as tests. Successfully finding an element by role does not replace an accessibility audit.
If a suitable semantic locator is not enough, agree on a test identifier, such as data-testid="checkout-summary". It should identify a stable function of the element. For a product list, narrow the search to the correct row instead of relying on the first button. A test identifier does not fix a missing label or keyboard operation. When text, language, or an identifier changes, decide who updates the relevant tests.
Individual Playwright lookups might look like this. These are illustrative snippets for an application with English labels, rather than a complete runnable test.
page.getByLabel('Email', { exact: true });
page.getByRole('button', { name: 'Place order', exact: true });
page.getByTestId('checkout-summary');
Wait for the expected application state
A visible button may not yet be ready to click, and a completed click does not necessarily mean an order has been saved. Playwright checks interaction conditions and retries web assertions until the expected state appears or the timeout expires. Wait for something like an order confirmation, rather than just the end of an animation.
A fixed pause of a few seconds may be too short in a slow environment and unnecessary in a fast one. Developers should expose distinguishable loading, success, and error states. The tester can then identify what the test is waiting for and what did not happen. On failure, inspect the action record, page state, and relevant request. For a more detailed procedure, see the article on causes of intermittent test failures.
Where the scenario can run safely
The test environment should allow an order to be created and canceled without affecting customer data or real payments. It need not copy all of production, but the team must know which differences limit the test result. Record the application version, enabled features, language, time zone, and integration settings. Avoid uncontrolled changes to them during the run.
Before starting, check the availability of the application and the services the scenario needs. An available sign-in page does not confirm that the database or payment sandbox works. Agree on who manages the environment and how they announce deployments or outages. When infrastructure fails, it should be possible to distinguish a test that could not run from an application defect. The article on test environments and their differences discusses environment selection.
Prepare data so tests do not interfere with each other
A test should not rely on a product that happens to be available or an account someone is currently editing. Prepare the data through an API, a verified script, or another controlled method. Playwright supports preparing server state through an API, but the application must provide the actual interface and permissions. Data setup should not bypass rules needed for a valid scenario.
For an order, specify an available product, price, cart state, and user. If tests change the same inventory or account, a new browser window alone will not separate them. Reserve separate accounts and data for parallel runs, or coordinate changes to shared state. Read-only scenarios can share immutable data if running concurrently does not affect them.
Agree on data cleanup after failed runs too:
- Mark created records with a run identifier so they can be found.
- Clean up only data belonging to the particular test or reserved environment.
- Allow for interruptions before cleanup and prepare a way to clean up abandoned records.
- Verify that a new run does not depend on the previous test succeeding.
Resetting the entire database may suit a dedicated environment, but in a shared one it would affect other tests. The article on test data for application journeys covers preparation and isolation in more detail.
Sign-in and sessions need their own rules
Distinguish a sign-in test from a test that just needs an authenticated user. In the latter case, you can use prepared authenticated state. Playwright describes how to save and reuse it. This approach does not check the sign-in form, which needs a separate scenario.
Agree on how to renew expired sessions and provide accounts for different permissions. An administrator account should not replace an ordinary customer when the test checks the customer’s order. For parallel tests that modify server data, use accounts that do not change each other’s state.
An authenticated-state file may contain cookies that can be used to take over the session. Keep it out of Git and restrict access as you would for a password. For two-factor authentication, agree on a controlled test procedure with the application owner. Selenium lists options for a test environment. Any exception must be protected, separated from production, and reviewed regularly.
Payments, messages, and CAPTCHA: what simulation verifies
For external services, separate your application’s behavior from the real connection to the provider. A simulated response helps you repeatedly check success, errors, or unavailability. Playwright can simulate requests made by the page, but a fully simulated response does not call the actual API. A passing test then does not confirm that the integration works.
Playwright network interception concerns browser traffic. If the application server calls the payment service directly, it needs a separate test mode or simulation on its side. Before the pilot, confirm with the developer where the request originates and which call is being replaced. Otherwise, a test may display a simulated result while the server still uses the real service.
For selected integration scenarios, use the provider’s sandbox and documented test data. For example, Stripe lets you simulate payments without moving money, including declined payments. For email and SMS, agree on a test channel, a way to identify the message belonging to a particular run, and a delivery timeout. The integration owner needs to know the chosen mode’s limitations.
CAPTCHA is deliberately designed to prevent automation. Selenium recommends test configuration or a controlled exception. Such an exception must be limited to a protected test environment. Using it verifies the subsequent process, rather than the effectiveness of CAPTCHA itself.
The first pilot reveals which changes should come first
Before expanding the suite, we recommend completing one representative scenario:
- The developer and tester identify elements, expected states, and missing interfaces for data preparation together.
- The environment administrator confirms configuration, availability, and safe separation from production.
- The integration owner chooses a sandbox or simulation and identifies what remains uncovered.
- The tester runs the scenario from a clean state, repeats it, and checks a relevant failure path too.
- The team checks the run alongside another test and recovery after a deliberate interruption in the test environment.
Group failures by cause: element lookup, an incomplete state, data collision, session, integration, or an environment outage. Assign each obstacle an owner and a way to verify the fix. Repeated passing results alone do not prove that the test checks the right thing.
How to tell whether the website is ready
The pilot should progress from prepared data to a verifiable result, and the next run should work without manual intervention. On failure, it should be possible to trace the specific step and restore state.
For a successful-order scenario in the test environment, try simulating a declined payment instead of a successful response. The successful-completion assertion should then fail. A separate declined-payment test, by contrast, passes if the application displays the expected error.
This preparation can reduce failures caused by test code and repeated maintenance. It does not require test identifiers across the entire site or a perfect copy of production. Start with the next important scenario, resolve its specific obstacles, and carry the same rules over to subsequent tests.