Why Credentials Should Never Be Hardcoded in Test Scripts: Security Risks and Best Practices
imagine you are working on an automation testing project.
Your team asks you to automate the login functionality of an application.
You write:
await page.getByLabel('Email').fill('admin@gmail.com');
await page.getByLabel('Password').fill('Admin@12345');
The test runs successfully.
You upload your code to GitHub so your teammates can access it.
Everything looks fine until someone discovers that your application username and password are visible inside the repository.
Now, anyone with access to that code may be able to see the credentials.
This is one of the reasons why professional automation engineers avoid hardcoding credentials in test scripts.
Hardcoding credentials means directly writing usernames, passwords, API keys or authentication tokens inside source code instead of storing them in a separate and secure configuration system.
In this article, you will understand why hardcoding credentials is dangerous, how it affects automation projects and what professional testers should do instead.
What Does Hardcoding Credentials Mean?
Hardcoding means placing fixed values directly inside a program.
For example:
const username = 'admin@gmail.com';
const password = 'Admin@12345';
Here, the username and password are directly written in the source code.
Anyone who can read the file can potentially see these values.
This creates unnecessary security risks.
What Is the Difference Between Hardcoded Credentials and Environment Variables?
Consider these two examples.
Hardcoded credentials:
const password = 'Admin@12345';
Environment variable:
const password = process.env.TEST_PASSWORD;
In the first example, the actual password is stored directly inside the source code.
In the second example, the test reads the password from its execution environment.
The second approach separates application credentials from the automation logic.
However, remember that environment variables are not automatically encrypted or completely secure. They must be supplied and protected through appropriate systems.
Why Should You Never Hardcode Credentials in Test Scripts?
There are several important reasons.
1. Credentials Can Become Visible in Source Code
When you upload your Playwright project to GitHub, your source code becomes accessible to people who have repository access.
If the password is hardcoded, it may be visible in the test file.
For example:
test('Admin Login', async ({ page }) => {
await page.goto('/login');
await page.getByLabel('Email')
.fill('admin@gmail.com');
await page.getByLabel('Password')
.fill('Admin@12345');
});
A developer or tester reading the file can see the password.
This is particularly dangerous when a repository is public.
2. Exposed Credentials Can Be Misused
Imagine a company has an automation project with access to an internal application.
The project contains hardcoded administrator credentials.
If someone obtains those credentials, they may attempt to access the application outside the testing process.
Depending on the permissions, this could expose sensitive information or allow unwanted actions.
That is why credentials should be protected.
3. Changing Passwords Becomes Difficult
Imagine your manager asks you to change the test password.
If the password is hardcoded in 25 different test files, you may need to update every file.
This creates extra work.
It also increases the chance of forgetting one location.
With environment variables, you can change the configuration value without modifying every test script.
4. Multiple Environments Become Difficult to Manage
Most professional applications have different environments.
For example:
Development.
Testing.
Staging.
Production.
Each environment may use different accounts and passwords.
Hardcoding credentials makes switching between environments more complicated.
A better approach is to keep environment-specific configuration separate from the test logic.
5. Hardcoded Credentials Create Collaboration Problems
Imagine five automation testers working on the same Playwright project.
If all five testers use the same hardcoded password, changing that password requires code changes and coordination.
If credentials are managed separately, the team can use approved test accounts and controlled configuration.
This improves collaboration.
6. Credentials May Remain in Git History
This is an important security problem.
Suppose you accidentally commit a password to GitHub.
Later, you remove the password from your test file.
Does that mean the password is completely safe?
No.
The previous version may still contain the password in Git history.
Anyone with access to that history may be able to find the old value.
Important: If a real credential is exposed, deleting it from the current file is not enough. Immediately rotate or revoke the credential and review the repository history and access.
7. Credentials May Appear in Logs and Reports
Hardcoded passwords are not the only risk.
Credentials can also appear in:
Console logs.
Screenshots.
Playwright trace files.
HTML reports.
Debugging output.
CI/CD execution logs.
For example, if a test captures a screenshot immediately after entering a password, the screenshot may expose information depending on how the application displays the field.
Playwright traces can also contain sensitive application details.
Always review artifacts before sharing them.
Real-World Example: What Happens When a Tester Hardcodes a Password?
Consider an online shopping application.
An automation tester creates a Playwright login test.
The tester writes:
test('Login Test', async ({ page }) => {
await page.goto('/login');
await page.getByLabel('Email')
.fill('admin@shopping.com');
await page.getByLabel('Password')
.fill('Shopping@123');
await page.getByRole('button', {
name: 'Login'
}).click();
});
The test works.
The tester commits the code to a shared repository.
Later, another developer downloads the project.
The developer can now see the credentials.
The problem is not that Playwright failed.
The problem is that the project was not designed with proper credential protection.
A successful automation test does not automatically mean the automation framework is secure.
How to Avoid Hardcoding Credentials in Playwright
The solution is to separate credentials from the test code.
Method 1: Use Environment Variables
Instead of writing:
const password = 'Admin@12345';
Use:
const password = process.env.TEST_PASSWORD;
Now, the test reads the password from its environment.
Method 2: Use a .env File for Local Development
For local practice, you can use a .env file.
Example:
TEST_USERNAME=tester@example.com
TEST_PASSWORD=example-password
Load the file using dotenv.
Make sure the file is included in .gitignore.
Do not upload real credentials to your repository.
Method 3: Use GitHub Actions Secrets
When Playwright tests run in GitHub Actions, store sensitive credentials in GitHub Actions Secrets.
For example:
- name: Run Playwright Tests
run: npx playwright test
env:
TEST_USERNAME: ${{ secrets.TEST_USERNAME }}
TEST_PASSWORD: ${{ secrets.TEST_PASSWORD }}
Your Playwright test can access the credentials using:
process.env.TEST_USERNAME
process.env.TEST_PASSWORD
This prevents the actual credential values from being written directly into your workflow.
Method 4: Use Dedicated Test Accounts
Do not use real customer accounts for regular automation testing.
Create separate test accounts.
For example:
test-user-01
test-user-02
admin-test-account
Give each account only the permissions required for its testing purpose.
This helps reduce the impact of accidental actions.
Method 5: Validate Credentials Before Using Them
Always check whether the required credentials are available.
Example:
const username = process.env.TEST_USERNAME;
const password = process.env.TEST_PASSWORD;
if (!username || !password) {
throw new Error(
'Required test credentials are missing'
);
}
This prevents your test from silently continuing with missing values.
Hardcoding Credentials vs Secure Credential Management
Common Security Mistakes Automation Testers Should Avoid
Mistake 1: Uploading .env Files to GitHub
A .env file may contain private credentials.
Add it to .gitignore.
Example:
.env
.env.*
!.env.example
Mistake 2: Using Administrator Credentials for Every Test
Not every test needs administrator access.
Use the minimum permissions required.
Mistake 3: Sharing Passwords Through Chat Messages
Avoid sharing sensitive passwords through ordinary messages, emails or public collaboration channels.
Use an approved password manager or secret-sharing system.
Mistake 4: Printing Passwords During Debugging
Avoid:
console.log(password);
This may expose credentials in terminal output or CI logs.
Mistake 5: Assuming GitHub Automatically Protects Everything
GitHub Secrets helps protect stored values, but it does not make every workflow automatically secure.
Review workflow permissions, third-party actions, pull request access and log output.
Mistake 6: Ignoring Playwright Trace Files
Trace files can contain sensitive information.
Do not upload or share them without checking their contents and access permissions.
What Should You Do If You Accidentally Commit a Password?
This is an important situation for every automation tester to understand.
Follow these steps:
Step 1: Change or revoke the exposed password immediately.
Do not wait until someone misuses it.
Step 2: Review where the credential was exposed.
Check the repository, commit history, logs and other shared locations.
Step 3: Remove the exposed credential from the source code.
Replace it with an environment variable or secure secret-management method.
Step 4: Review Git history.
If necessary, remove the exposed value from repository history using an appropriate history-rewriting process.
Step 5: Check access permissions.
Review who could access the repository and associated artifacts.
Step 6: Prevent the same problem in the future.
Use secret scanning, code reviews and repository protection rules.
Remember that removing a password from Git history does not make an already exposed password safe. Credential rotation is essential.
Best Practices for Professional Playwright Automation Projects
Follow these security practices:
Never store actual passwords directly in test files.
Use environment variables for configuration.
Store CI/CD secrets in approved secret-management systems.
Keep .env files out of Git repositories.
Use dedicated automation accounts.
Give accounts minimum required permissions.
Rotate credentials when exposure is suspected.
Avoid printing passwords and tokens in logs.
Protect screenshots, traces and test reports.
Review security practices regularly.
Frequently Asked Questions
1. Why should credentials never be hardcoded in test scripts?
Hardcoded credentials can become visible to developers, testers and anyone who can access the source code. This creates unnecessary security risks and makes credential management difficult.
2. What is hardcoding a password?
Hardcoding a password means writing the actual password directly inside a source-code file instead of obtaining it from a secure configuration source.
3. Is hardcoding API keys dangerous?
Yes. API keys may provide access to external services and resources. If exposed, they may be misused depending on their permissions.
4. How can I protect passwords in Playwright?
Use environment variables, dedicated test accounts and secure CI/CD secret-management systems. Avoid storing actual passwords in test scripts.
5. Is a .env file completely secure?
No. A .env file is a convenient local configuration file, not an encrypted secret vault. Protect it using file permissions, .gitignore and appropriate storage practices.
6. What happens if I accidentally push a password to GitHub?
The credential should be treated as exposed. Rotate or revoke it immediately, review repository history and remove the exposed value from the repository where appropriate.
7. Can Playwright use GitHub Actions Secrets?
Yes. GitHub Actions can provide secrets as environment variables when running Playwright tests.
8. Should automation testers use real customer credentials?
No. Use dedicated test accounts with controlled permissions whenever possible.
9. Can Playwright screenshots expose passwords?
They can expose sensitive information depending on what the application displays. Review screenshots and traces before sharing them.
10. Are environment variables safer than hardcoded credentials?
They provide better separation between code and configuration, but their security depends on how they are stored, provided, accessed and logged.
Conclusion
Hardcoding credentials may look like a simple way to write automation tests, but it creates unnecessary security and maintenance problems.
Professional Playwright automation frameworks should separate test logic from sensitive configuration.
Use environment variables, secure CI/CD secrets, dedicated test accounts and proper access controls.
Remember:
Your automation framework should test application security and functionality without creating new security problems of its own.
If you want to become a professional automation tester, understanding secure coding practices is just as important as learning how to write Playwright test cases.
Explore Playwright Masters to learn practical Playwright automation concepts and build real-world testing skills.

Comments
Post a Comment