Transcripted Summary

In this lecture, we are going to be discussing base URL's and environment variables.

We already have been using base.url that we have here in our config file. We are going to be updating that, so that we can have different URL's for specific environments.

Let us say, you have a QA environment, you have a DEV environment, maybe a staging and a production environment. And each of these would have a different URL. What we can do is to use environment variables, so that you can specify when running your tests, the environment that you want your test to be run on.


To do that, we are going to create a “urls.js” file in the root of our project.


# urls.js

module.exports = {
    qa: 'http://the-internet.herokuapp.com',
    dev: 'https://www.google.com/',
    staging: 'https://www.yahoo.com/'
} 

We're going to have an environment for QA and let us just say that this is the URL that we are using now.

Let's say that we have a DEV environment and for the purpose of this, our DEV environment is Google.com.

Let's just say we have a staging environment, and our staging environment is maybe Yahoo.com.

Okay, so we have 3 different environments.


The next thing that we do is go into our config file and at the very top, we are going to import that URL file.

So, we are going to say


const url = require('./urls')

When we specify the environment that we want in our terminal, we need to use something that is going to determine what that URL is going to be.

So, we are going to process the environment variable that we will be passing it through our terminal.


const ENV = process.env.ENV

process.ENV is started at the run time and it is used to represent the state that your test will run in. So, it is going to parse whatever we pass in. So, if we say “ENV = QA” then it's going to take QA and map it to the URL that we have specified that QA should be. And that is what it's going to use, to run our test on.


Then we are going to have an if.


const url = require('./urls')
const ENV = process.env.ENV

if (!ENV || !['qa', 'dev', 'staging'].includes(ENV)) {
    console.log('Please use the following format when running the test script: ENV=qa|dev|staging')
    process.exit()
}

We are going to say “if you don't say environment (or the environment variable that is passed in is not one of the ones that we are expecting, that we have specified here, which are QA, DEV, and staging)...”

So, we are going to say, “if they didn't use an environment variable or it is not QA, DEV, or staging” and we are just going to check that the area contains a specific element, which are these that we're looking for.

Right, so if environment does not contain any of this, or they do not use the environment variable at all, then we are going to say console.log and an error message — “Please use the following format when running the test script: ENV=qa|dev|staging”.

And we are going to give them a clue, and we're going to say “ENV=qa|dev|staging”.

Okay, and then we are going to exit — process.exit() — and this will stop our test.


So now we also need to change our baseurl.

Our base URL at this point, is still saying this [baseurl: http://the-internet.herokuapp.com/].

But we want it to use the environment variables that we are going to pass in, and then grab whatever that matching environment should be.


baseUrl: url[process.env.ENV]

The URL should be whatever the environment variable that we are passing in is.

So, if we run our test, by saying


ENV=qa npm run actions

Let us run this test suite because it's shorter than running npm run test which will run all the tests that we have here. It should go to the URL that we have been using all this time.

Okay, if we then say — ENV= dev NPM run test — we are expecting it to go to Google.com, and all our tests will fail.

So, we can just control+C and quit that test, instead of allowing all of them to run, and we know that they will fail.

If we say ENV=staging npm run test — we know that it should go to Yahoo.com. And it does go to Yahoo.com. so we can kill our test, as we are expecting that it will fail.

Using the environment variable along with our baseurl that we have been using throughout, it allows for us to manage our tests environments much easier. Instead of going into our tests and specifying the URL each time.

And what if we have a different environment? It takes care of all of that for us and makes it much easier for us to change environments and change URL's quickly.



Resources



Quiz

The quiz for this chapter can be found in section 6.5