Environment Variables | Bondar Academy
Course: Playwright API Testing with TypeScript
Module: Test Management
Instructor: Artem Bondar
Lesson Summary
In this lesson, we discuss environment variables and how to manage them effectively in your project. Environment variables allow you to configure different settings for various environments, such as development , QA , and production . Key Concepts Environment Variable Configuration: Use a variable like testenv to switch between environments. Security for Credentials: It's crucial to keep sensitive information, like usernames and passwords, secure, especially in production. Options for Managing Environment Variables Create a .env file to store variables locally. Pass variables via the command line during test execution, which is useful for CICD processes. Setting Up the .env File To create a .env file: touch .env Ensure to add it to your .gitignore to prevent accidental exposure: .env Reading Variables in TypeScript Install the .env package to read the variables: npm install dotenv --save-dev Define your variables in the .env file: [email protected] prod_password=prod_pass_123 Access these variables in your TypeScript code using process.env . Command Line Usage To pass environment variables via the command line: [email protected] prod_password=prod_pass_123 npx playwright test This method overrides values in the .env file, making it suitable for secure execution in CICD pipelines. This lesson covers how to manage and utilize environment variables effectively in your framework. See you in the next lesson!
Video Transcript
Hey guys, welcome back. And in this lesson, we're gonna talk about environment variables, how to use them and organize in your project. All right, let's get into it. So this is our API test.config.ts. And previously, when we were configuring this file, we already talked about environment variables. So we created this testenv environment variable. And when we pass the different values to this environment variable, such as dev, QA, or prod, we can switch the environment that we want to execute. And also, we can just simply change this value directly in the configuration file to switch the environment, for example, to QA or to prod. But what if you want to have the variables that you want to securely save for your project, for example, credentials? Look, we have a production environment here. And for username and password, we don't have anything else right there. And usually, credentials for the low environment is not a big deal. But for the production, well, it'd have to be kept secure. How would you manage those? And there are two options for that, to create .env file and keep this file only locally for your projects, keeping the environment variables only on your computer. And the second option is to pass those environment variables every time through the command line during the test execution. So using the command line is convenient when you run tests in CICD. So this is how you pass environment variables. But when you run the test locally, it's convenient to have .env file and store those credentials. So let's configure this stuff. So first of all, let's create a new file in the root of our project. Create new file, and I call it .env, like this. Also, this file absolutely have to be added to git ignore file, to make sure that you will not commit this file accidentally to GitHub repository, and all your credentials will be exposed. So make sure that you will add this file over here. So I type .env into .gitignore file, that's it. And now you can see that this file is kind of grayed out a little bit. It means that it's successfully excluded from the source control. So it will not be added to the git repository. And then we can use it. Then how to read the .env actually in the TS file? For that, we're gonna go to Playwright Configuration file, and we copy these commented lines that are not used, but we kept them for the reference. Going back over here, and I paste those lines right here and on command. So this is the package called .env that will enable you working with the .env file, reading this file from this configuration file. So let's install this package first. For that, I will open this URL in the browser. So this is just more details on the GitHub about this package, and we need to install it. So scrolling a little bit down, and this is the command I need to execute. Ctrl-C, going back to the project, new terminal, paste it in new terminal, and I type it save dev like this. And installing the package. All right, this step is completed. The package was installed, and we have everything is fine. The next step, we need to create environment variables inside of .env file that we want to use. Let's say that we create two environment variables. For example, prod underscore username equals two. And I created a prod account in advance, which is prod at test.com. And the second will be prod underscore password. Password equals two, and the value prod pass 123. This is the kind of alternative credentials for our Conduit application. And then after that, we need to set this value over here. Instead of empty string, we need to call process.env. And the value that we set right here, that prod username, like this. Prod username, and the second one is prod password. Process.env.prodpassword. All right, you see, now we have a little complaint from the TypeScript that is string undefined is not assignable to type string. There are two ways how you can fix it. One way is just set that you add this as a string, and you will fix this compiling error as a string. That's the option number one. The option number two, if you want that credentials or environment variables would set absolutely for your project, you can add something like this. You can add the conditions that if process environment variable does not exist, or this process environment variable does not exist. Like this, and I put the exclamation sign, like this. Then we want to throw the error with the error message. Missing required environment variables, like this. So in this case, if I remove this, you see we don't have any compilation issues. Because the framework right now requires us for whoever you're gonna use this framework to add environment variables to the project. So it's up to you to decide whatever restrictions level you need to your project, either do it as a string or add a mandatory field like this, that hey, this is a required. For me, I will simplify, I will comment this out. I don't want this to throw additional errors. And yeah, I believe this is it. So now we can just change this from dev to prod, like this. When this file will be initialized, it's gonna call this section and the production variables should be taken from this file. So let's run this test. For example, get articles, and test failed. Something went wrong. Email or password is invalid. Okay, password is incorrect. Not port, it's prod password. Okay, prod pass, one, two, three. All right, let's try it one more time. So running the test. Okay, now test pass successfully. So as you saw, the credentials that we saved into environment variables were used successfully because it failed, because the password was incorrect initially. Also, when you run your test from the command line, for example, in CICD, you can pass this username and password from the command line. How would you do that? So you open the terminal and just do it like this. And this syntax, by the way, is for Mac or Linux systems. For the Windows PowerShell and Windows Command Prompt, it will be slightly different syntax. So you just type the credentials like this, and then type the command that you want to execute, npx play write test. And then the test that we want to run is the smoke test.spec.ts. And if I remove or just modify, make it invalid, the test's still gonna work, so run the test. And you see, all tests were executed. So when you pass the credentials from the command line, when you pass the environment variables from the command line, whatever value you pass from the command line overrides any value saved in the .env file, or if you even did not provide any values in .env file, those values will be overrided. And this syntax is useful for execution in the CICD pipelines, because very often you need to keep those credentials safely in some vault or storage keys or something like this. And then you pass those values in the command line during the execution, and that way, your framework can be executed in CICD. Well, that's pretty much it, guys. This is how you handle and manage environment variables for your framework. This is how you switch environment variables using test.env flag using the command line or directly in the framework. And to save the environment variables safely in your framework, you can use .env file to save those values. That's it, guys, and see you in the next lesson.