Automation Framework - Part 2

PM2 Implementation

·

3 min read

Automation Framework - Part 2

Photo by Lukas on Unsplash

Introduction

In the previous article we learned how to create a basic automation framework from scratch. We also learned to start & run a selenium-standalone server as a foreground process and terminate it manually using the escape sequence ctrl+c.

In this article, we will learn how to run the selenium-standalone server as a detached process using PM2.


Implementation

Note: PM2 is a manager to keep an application process daemonized using the CLI, if you want to know more check outhere.

Step 1 - Install Dependency

Note: installing PM2 as a global dependency is recommended.

npm install -g pm2

Step 2 - Configuration

Let's create a file ecosystem.config.js at the project root as recommended in PM2 documentation.

pm2 init simple
module.exports = {
  apps: [
    {
      name: "standalone-server",
      script: "./app.js",
    },
  ],
};

Next, we will create an app.js file at the project root.

Note: we will be using NodeJS "child_process" to execute npm commands

const { exec } = require("child_process");
exec("npm run setup && npm start");

Note: here we are running the "setup" first to ensure the browser driver dependencies are installed before starting the server.


Step 3 - Start Server

Let's now start the selenium-standalone server as a daemonized process.

pm2 start ecosystem.config.js

image.png

Note: the above command will start the selenium server in the background, you can access it athttp://localhost:4444

image.png


Step 4 - Run Tests

Let's execute our test cases.

npm test

Step 5 - Stop Server

Once the test cases are executed and there are no more tests to run, we can stop the server process.

Note: the stopped process can be started anytime as mentioned in Step 3.

pm2 stop ecosystem.config.js

image.png


Step 6 - Dispose Server

We can also delete the server process at any point in time.

pm2 delete ecosystem.config.js

image.png

Logs

By default, PM2 stores two types of logs as below

  • global logs at $HOME/.pm2/pm2.log

  • application logs at $HOME/.pm2/logs/

To display all the logs in realtime

pm2 logs

image.png

To display application logs

pm2 logs selenium-standalone

image.png


Monitoring

We can also monitor the application logs using the below command.

pm2 monit

image.png

Note: The above image is just a sample.


Conclusion

So far, we learned how to create a bare minimum automation framework from scratch, we also learned how to daemonize the selenium-standalone server to run as a background process on a standalone operating system.

In the upcoming article, we will learn to execute the automation scripts on docker containers, a live preview and much more in action.

The source code is available on GitHub.

If you find this article helpful or have any suggestions, reach out to me on LinkedIn.

Thank you and keep learning!