Skip to content

Instantly share code, notes, and snippets.

@dwalleck
Last active April 12, 2022 15:03
Show Gist options
  • Save dwalleck/f2480abe773b7d94fca97b9c1cd98bf7 to your computer and use it in GitHub Desktop.
Save dwalleck/f2480abe773b7d94fca97b9c1cd98bf7 to your computer and use it in GitHub Desktop.
APP_URL=http://localhost:{{port}}/YourService/
SUPERADMIN_USERNAME=superadmin
SUPERADMIN_SERVICE_KEY={{serviceKey}}
SUPERADMIN_API_KEY={{apiKey}}
SUPERADMIN_PASSWORD=someSafePassword
ADMIN_USERNAME=admin
So the general gist is this: the .env file is generated at run time because our system generates new admin credentials for a new deployment. That's the only reason there's sensitive fields in it. If you have sensitive data that needs to be in your per environment config, you probably want those in environment variables. All that said,
```
const fullEnvPath = getEnvPath('.')
dotenv.config({ path: fullEnvPath })
```
populates the environment variables with anything from the .env file. The instance of TestConfig below that pulls the environment variables into the object, setting some values based on whether or not we're in our CI environment. The testConfig can then either be imported by test files or provided as a custom fixture. This is **not** perfect, but this is what we started with. I hope this helps or gives some inspirations for improvements!
interface UserCredentials {
username: string
apiKey: string
password: string
}
interface TestConfig {
appUrl: string
superAdminCredentials: UserCredentials
playwrightDebugEnabled: string
recordVideo: string
resultsDirName: string
playwrightResultsDirPath: string
cucumberResultsDirPath: string
jira: JiraCredentials
}
interface ExternalSeriveCredentials {
username: string
apiKey: string
}
export const testConfig: TestConfig = {
appUrl: process.env.APP_URL || '',
playwrightDebugEnabled: process.env.PWDEBUG || '',
recordVideo: process.env.PWVIDEO || '',
resultsDirName: process.env.RESULT_DIR_NAME || '',
playwrightResultsDirPath:
process.env.CI === 'true'
? `./playwright-runner/results`
: `./playwright-runner/results/results-${process.env.RESULT_DIR_NAME}`,
cucumberResultsDirPath:
process.env.CI === 'true'
? `cucumber/results`
: `cucumber/results/${process.env.RESULT_DIR_NAME}`,
superAdminCredentials: {
username: process.env.SUPERADMIN_USERNAME || '',
apiKey: process.env.SUPERADMIN_API_KEY || '',
password: process.env.SUPERADMIN_PASSWORD || ''
},
externalServiceCredentials: {
username: process.env.EXTERNAL_SERVICE_USERNAME || '',
apiKey: process.env.EXTERNAL_API_TOKEN || ''
}
}
mport { getEnvPath } from './testConfigManagement'
import dotenv from 'dotenv'
const fullEnvPath = getEnvPath('.')
dotenv.config({ path: fullEnvPath })
export const getEnvPath = (parentDir: string) => {
const fullParentDir = fs.realpathSync(parentDir)
const fullEnvPath = `${fullParentDir}${path.sep}.env`
if (!fs.existsSync(fullEnvPath)) {
throw Error(
`Could not find a .env file at ${fullParentDir}.\nCheck the README to see how to set up your .env file for the Playwright tests.`
)
} else {
return fullEnvPath
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment