Exclude Turnstile from E2E tests
This tutorial explains how to handle Turnstile in your end-to-end (E2E) tests by using Turnstile's dedicated testing keys.
When running E2E tests, you often want to bypass or simplify the Turnstile verification process. Cloudflare provides official test credentials that always pass verification, making them perfect for testing environments:
- Test sitekey:
1x00000000000000000000AA
- Test secret key:
1x0000000000000000000000000000000AA
For more details, refer to the testing documentation.
The key to implementing test-environment detection is identifying test requests server-side. Here is a simple approach:
When rendering your page, inject the appropriate sitekey based on the environment:
Your template can then use the injected sitekey:
-
Environment detection
- Use multiple factors to identify test environments (IP, headers, etc.).
- Keep your test environment identifiers secure if you need to test from the public web.
-
Credential management
- Store production credentials securely (for example, in environment variables).
- Never commit credentials to version control.
- Use different credentials for each environment.
-
Deployment safety
- Add checks to prevent test credentials in production.
- Include credential validation in your CI/CD pipeline.
- Monitor for accidental test credential usage.
- Test credentials will always pass verification.
- They are perfect for automated testing environments.
- They help avoid rate limiting during testing.
- They make tests more predictable and faster.
For Cypress or similar E2E testing frameworks:
By using Turnstile's test credentials and proper environment detection, you can create reliable E2E tests while maintaining security in production. Remember to always keep test credentials separate from production and implement proper safeguards in your deployment process.