Overview
Lifecycle files are YAML files placed in your repository’slifecycle/
directory. TestDriver automatically executes these files during the appropriate phases:
lifecycle/provision.yaml
: Executed when a new sandbox is createdlifecycle/prerun.yaml
: Executed before tests runlifecycle/postrun.yaml
: Executed after tests complete
File Structure
All lifecycle files follow the standard TestDriver YAML format:Provision Scripts
Thelifecycle/provision.yaml
file is executed when a new sandbox is created. This is ideal for installing dependencies, setting up the environment, or preparing the system for testing.
Example
lifecycle/provision.yaml
Common provision tasks:
- Installing software dependencies
- Setting up browser extensions
- Configuring system settings
- Downloading test data or assets
Prerun Scripts
Thelifecycle/prerun.yaml
file is executed before each test run irrespective of whether its a new sandbox or an already running one. This is useful for preparing the immediate test environment.
Example
lifecycle/prerun.yaml
Common prerun tasks:
- Opening applications or browsers
- Navigating to starting pages
- Clearing application state
- Starting monitoring tools
- Setting environment variables
Postrun Scripts
Thelifecycle/postrun.yaml
file is executed after tests complete. This is useful for cleanup tasks, generating reports, or capturing final state information.
Example
lifecycle/postrun.yaml
Common postrun tasks:
- Generating test reports
- Capturing screenshots or logs
- Cleaning up temporary files
- Stopping background processes
- Uploading artifacts
Environment Variables
Lifecycle files have access to TestDriver environment variables:${TD_WEBSITE}
: The target website URL${TD_THIS_FILE}
: Current test file name${TD_API_KEY}
: Your TestDriver API key- Custom variables defined in your workflow
Using variables in lifecycle files:
Best Practices
Keep lifecycle files focused
Each lifecycle file should have a specific purpose:- Provision: System-level setup
- Prerun: Test preparation
- Postrun: Cleanup and reporting
Use appropriate commands
- Use
exec
withlang: pwsh
for system commands - Use TestDriver commands (
wait-for-text
,hover-text
, etc.) for UI interactions - Include appropriate timeouts for reliability
Error handling
Include checks to verify operations completed successfully:Performance considerations
- Keep provision scripts efficient to minimize sandbox setup time
- Cache dependencies when possible
- Use lightweight operations in prerun/postrun scripts
File Placement
Place lifecycle files in your repository’s root directory:Execution Order
When TestDriver runs, lifecycle files execute in this order:- Provision (once per sandbox creation)
- Prerun (before each test)
- Your test files
- Postrun (after each test)
Troubleshooting
Lifecycle files not executing
- Verify files are in the
testdriver/lifecycle/
directory - Check YAML syntax is valid
- Ensure version number is specified
- Verify file permissions in your repository
Commands failing
- Check PowerShell syntax for
exec
commands - Verify file paths exist on the target system
- Add error handling and logging
- Use appropriate timeouts for operations