Welcome to the 6th part of the blog post series called “Creating an End-to-End Web Test Automation Project from Scratch.” It was a long series, I know. But we are in the final round! So please, just bear with me! 🐻
So far, we have covered many topics, from the creation of a project to integrating it with CI/CD pipeline. If you need to review the previous chapters, you can find the articles at the links below.
You will make a magic touch, which will enable you to auto-scale your Selenium Grid with the help of KEDA, Kubernetes Event-driven Autoscaling. KEDA will help you scale your nodes not according to CPU or RAM usage but according to the queue size of our tests to be run! Now, if you are ready, let's get down to it!
First you are going to install minikube to create a kubernetes instance in your local machine.
Let’s install minikube with brew:
brew install minikube
If you do not want to use brew or if you have another OS, you can go to the official minikube site and install it by choosing our OS and CPU Architecture type.
Now let’s start minikube:
minikube start --vm-driver=docker
Here, the –vm-driver is the driver type of which a minikube can be deployed. For MacOS, Docker is recommended. See the full list of drivers for minikube.
After successfully starting your minikube, browse your dashboard with:
minikube dashboard
It will automatically open the dashboard on your default browser:
Now since you have your kubernetes up and running, let’s add your Selenium Grid and your Nodes to your Cluster!
For your Grid and Node configurations, you will use example yaml files for these in the official Kubernetes GitHub repository. Let’s navigate to this repository. You need to download these three yaml files:
- selenium-hub-deployment.yaml => To deploy selenium hub.
- selenium-hub-svc.yaml => A service for nodes to connect to the hub.
- selenium-node-chrome-deployment.yaml => To deploy Chrome node.
In selenium-node-chrome-deployment.yaml, let’s change replica count to `1`.
Note: If you have a computer which has Apple Silicon with Arm64 Architecture, you need to make these small changes in the following files as well:
In selenium-hub-deployment.yaml file, change the container image to `seleniarm/hub`.
In selenium-node-chrome-deployment.yaml file, change the container image to `seleniarm/node-chromium`.
Now let’s connect them to your k8s cluster.
1. Hub Deployment:
kubectl create -f selenium-hub-deployment.yaml
2. Service Deployment:
kubectl create -f selenium-hub-svc.yaml
3. Node Deployment:
kubectl create -f selenium-node-chrome-deployment.yaml
Now let’s check if your deployments are successful with the command below.
kubectl get deploy
If your deployments were successful, you should see something like below.
Now let’s check your service with:
kubectl get services
You can see your service as well:
Now let’s find out the url of your grid with the command below:
minikube service selenium-hub --url
Your grid is on one of these ports below. Usually the first one:
After entering correct port, you should see something like this:
Next, you will configure KEDA.
From the KEDA releases page, download the latest KEDA version. At the moment of writing this blogpost, the latest version is 2.10.1.
And you need to create a scaled-object for your Chrome node. Let’s create a file named scaled-object-chrome.yaml and populate it with the code below:
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: selenium-chrome-scaledobject
namespace: default
labels:
deploymentName: selenium-node-chrome
spec:
minReplicaCount: 0
maxReplicaCount: 4
scaleTargetRef:
name: selenium-node-chrome
pollingInterval: 1
triggers:
- type: selenium-grid
metadata:
url: 'http://selenium-hub.default:4444/graphql'
browserName: 'chrome'
In here:
- minReplicaCount is the number of replicas when there are not any tests in the queue.
- maxReplicaCount is the max number of replicas when the activation threshold is reached. In this instance, this means that you won’t have more than 4 Chrome nodes no matter how many tests are in queue.
- activationThreshold is the threshold to scale up your Chrome node. If the queue reaches 5, it initiates a replication.Now let’s integrate them into your Cluster.
kubectl apply -f keda-2.10.1.yaml
After that, let’s apply your scaled-object. Since your Chrome deployment in the default namespace, you will apply your scaled-object to there too.
kubectl apply -f ./scaled-object-chrome.yaml --namespace=default
Now give it a few moments and check your Selenium Grid again:
No nodes!? Why? Because you configured your minReplicaCount as 0. So when there aren’t any tests in the queue, KEDA scales down your nodes to zero. Now I call this resource management!
Okay, let’s change the remote_url variable in the test automation project to our current Selenium Grid Port, which is 64824 in this instance, and run 8 tests in parallel with the `parallel_cucumber -n 8` command. And let’s see what happens.
Now that’s it! You have a grid that is auto-scaled with the help of Kubernetes and KEDA!
This is the end of a long journey. Thanks for bearing with me with this eight-chapter-long blogpost series.
Before closing up, let’s summarise what you have accomplished so far:
✅You created a test automation project using Ruby - Capybara - Cucumber and Installed all dependencies.
✅You learned how to write CSS selectors.
✅You wrote test scenarios to be used on your test automation project.
✅You recorded failed test scenarios as video files in Ruby.
✅You used remote connections to run your tests in different machines.
✅You used Selenium Grid and ran your tests in parallel.
✅You dockerized your test automation project so that it can be run in any environment.
✅You recorded failed test runs in a Docker container using Selenium Video image.
✅You integrated CI/CD pipeline to your project by using Jenkins.
✅You configured auto-scaling for your project by using KEDA.
Hope to see you in upcoming sprints!