☀️ In this use case, we will see how to execute the selenium script execution inside AWS EC2 instances using Selenium grid and docker image.
Flow Diagram:
🦋 EC2 instance creation with preinstalled docker amazon Linux AMI
✏️ Go to AWS Management console, In AMI search for an 'docker' and select one of the Linux AMI where docker already been pre-installed as below.
✏️ Create a new key-pair in order to connect with EC2 instances. If you are using putty please make you are selecting the .ppk file
✏️ In Network settings, make sure you add the inbound security group with port 4444 and 7900 as below
✏️ Once you click create instances make sure your instance is up and running
✏️Click on connect option using SSH client as below
ssh -i "vj.pem" ec2-user@<ipv4 public dns>.compute-1.amazonaws.com
You can either use SSH Client via putty agent or windows powershell. Once you are connected you will get as below
🦋 How to install the docker in amazon EC2 instance if not installed
🐳 Consider that if selected AMI doesn't contain any docker. You can install the docker by using below commands
sudo yum update
sudo yum install -y docker
sudo usermod -a -G docker ec2-user
sudo service docker start
sudo chkconfig docker on
🐳 You can check for docker version using below command
docker --version
🦋 How to pull the selenium standalone chrome image from docker hub
👨🏻💻 In order to run your webdriver tests remotely. You need to pull and install the latest standalone selenium chrome version as below
docker run -d -p 4444:4444 -p 7900:7900 selenium/standalone-chrome:latest
👨🏻💻 To check containers are up and running. Enter the below command
docker ps -a
👨🏻💻 Similarly, if you need install the latest standalone firefox version. You can use the below command
docker run -d -p 4444:4444 -p 7900:7900 selenium/standalone-firefox:latest
👨🏻💻 Copy the IPV4 or public IPV4 address with colon 4444, open it in a new tab
👨🏻💻 You can check how many images installed using below docker command
docker images
👨🏻💻If you wish to install the docker compose to scale up the number of nodes. Below is the command
sudo curl -L https://github.com/docker/compose/releases/download/1.21.0/docker-compose-`uname -s`-`uname -m` | sudo tee /usr/local/bin/docker-compose > /dev/null
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version
🦋 Another way - Creation of selenium grid with docker compose yml file
✏️ Create the docker-compose yml file using below command
touch docker-compose.yml
✏️ Edit the newly created docker compose yml file in visual editor by pressing i in your keyboard. Enter the below yml file and quit using :wq!
version: "3"
services:
chrome:
image: selenium/node-chrome:4.14.1
depends_on:
- selenium-hub
environment:
- SE_EVENT_BUS_HOST=selenium-hub
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
- SE_NODE_MAX_INSTANCES=3
edge:
image: selenium/node-edge:4.14.1
depends_on:
- selenium-hub
environment:
- SE_EVENT_BUS_HOST=selenium-hub
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
firefox:
image: selenium/node-firefox:4.14.1
depends_on:
- selenium-hub
environment:
- SE_EVENT_BUS_HOST=selenium-hub
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
selenium-hub:
image: selenium/hub:4.14.1
container_name: selenium-hub
ports:
- "4442:4442"
- "4443:4443"
- "4444:4444"
✏️ Enter the following command to make the docker up and running
docker-compose up -d
✏️ You will get below output once done with your installation
✏️ You can check all the docker containers running by using the below command
docker ps -a
🦋 Checking the selenium grid using EC2 IPV4 address and port 4444
⭐ Checking the selenium grid using IPV4 public ip address and port 4444
🦋 Configure your EC2 IPV4 instance address in your execution script as a hub
☀️ You can check the current execution and queue size in your selenium grid as below
☀️ You can increase your queue size using below command
docker compose -f docker-compose-v3.yml up --scale chrome=2
☀️ Below are the changes to be done in your driver configuration files
public void user_login_page() throws MalformedURLException {
WebDriverManager.chromedriver().setup();
DesiredCapabilities capabilities=new DesiredCapabilities();
capabilities.setBrowserName("chrome");
driver = new RemoteWebDriver(new
URL("http://52.66.250.166:4444/wd/hub"), capabilities);
driver.get("https://selectorshub.com/");
}
☀️ You can tear down the container using below command
docker-compose down
☀️ Please make sure terminate your ubuntu instances created to avoid costs
Thanks for being patient and followed me. Keep supporting 🙏
Clap👏 if you liked the blog
For more exercises — pls do follow me below ✅!
https://www.linkedin.com/in/vijayaraghavanvashudevan/
Top comments (0)