Selenium grid deployment by CloudFormation+Ansible
Overview
Each web application should be tested by UI tests. Selenium is one of the most famous tool to do it. Selenium has the possibility to run tests in parallel and execute test cases faster. It helps to make your application more stable and get higher quality.
This topic describes how to deploy Selenium cluster automatically with 50–100 browsers using a modern approach (CloudFormation and Ansible).
Selenium
Selenium is a portable framework for testing web applications. Selenium provides a playback tool for authoring functional tests without the need to learn a test scripting language (Selenium IDE). It also provides a test domain-specific language (Selenese) to write tests in a number of popular programming languages, including C#, Groovy, Java, Perl, PHP, Python, Ruby and Scala. The tests can then run against most modern web browsers. Selenium runs on Windows, Linux, and macOS. It is open-source software released under the Apache License 2.0.
Selenium supports a lot of browsers:
- Safari
- Opera
- Chrome
- Firefox
- IE
CloudFormation
AWS CloudFormation provides a common language for you to model and provision AWS and third party application resources in your cloud environment. AWS CloudFormation allows you to use programming languages or a simple text file to model and provision, in an automated and secure manner, all the resources needed for your applications across all regions and accounts. This gives you a single source of truth for your AWS and third party resources.
Deployment diagram
Diagram shows a high level architecture of the CloudFormation stack. It shows what uses services stack.
The main steps of the CloudFormation are described in Deployment steps.
Deployment steps
AWS CloudFormation file has some requirements/resources that should be created before creating CloudFormation stack:
- S3 bucket with ansible playbooks
- Route53 Zone
- VPC ID
- SG ID
- Subnet ID
- AMI ID related to the region (https://aws.amazon.com/amazon-linux-ami/)
You should update the template before uploading stack or choose proper resources ID’s during deployment stack.
CloudFormation file https://github.com/helli0n/selenuim-docker-cluster-aws/blob/master/cloudformation/docker-selenium.yml does a lot of steps.
CloudFormation Actions:
- create instances (EC2 with proper role, security groups)
- upload ansible playbooks to the EC2 from S3
- update instances
- deploy software using ansible
/usr/bin/ansible-playbook -i /opt/ansible/hosts /opt/ansible/docker.yml
- create AWS ALB
- create target groups and assign to the EC2
- create Route53 record
When stack is deployed you would have access to the Selenium Hub by url
selenium.example.com:4444
Stack provides Selenium Node with VNC for debugging tests as well
selenium.example.com:4445 - Firefox Selenium Node
selenium.example.com:5900 - VNC port (default password is 'secret')
Ansible deploys docker containers and executes several roles
https://github.com/helli0n/selenuim-docker-cluster-aws/blob/master/ansible/docker.yml
Ansible provisioning actions:
- aws-asg-lb-status (checks Autoscaling group)
- docker-config (configure docker service: enables socket, adds private docker registry)
- portainer (deploys https://www.portainer.io/ — WEB UI for docker cluster, creates docker swarm cluster)
- selenium (deploys Selenuim Cluster)
- selenium-debug (deploys Selenuim Node with VNC access)
- docker-node-config (adds node to the Docker Cluster if it is new node)
Last step is needed for adding new nodes to the existing cluster. You just need to change the Autoscaling capacity and new node will be added automatically to the Docker swarm Cluster.
Selenium cluster schema
When cluster is deployed, you just need to add property into your tests code
WebDriver driver = new RemoteWebDriver(new URL(“http://selenium.example.com:4444/wd/hub"), capabilities);
Tests run automatically on the proper browser (Firexox or Chrome) and return results to the Hub.
At the end you will have reports if you’ve configured it. (https://docs.qameta.io/allure/ — example of the Selenium reports)
Summary
Solution provides easiest way how to deploy Selenium Cluster using CloudFormation. Selenium helps to make your application more stable via running UI tests. It has possibility to execute a lot of tests faster and show status of your application after deployment.
You should definitely use this solution if your business needs it.
When you need to increase the cluster, you can do it very easily. Ansible playbooks have the possibility to add new nodes to the existing cluster on demand. You have to change the number of browsers into Portainer UI to increase browsers and your cluster capacity.
URLs:
https://github.com/helli0n/selenuim-docker-cluster-aws
https://www.selenium.dev/
https://en.wikipedia.org/wiki/Selenium_(software)
https://github.com/SeleniumHQ/docker-selenium
https://aws.amazon.com/cloudformation/
https://www.portainer.io/installation/
https://portainer.readthedocs.io/en/stable/configuration.html
https://docs.qameta.io/allure/