DEV Community

Cover image for Selenium script execution in AWS EC2 instance using selenium grid and docker containers
Vijayaraghavan Vashudevan for AWS User Group NCR

Posted on • Updated on

Selenium script execution in AWS EC2 instance using selenium grid and docker containers

☀️ 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:

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.

Linux AMI

✏️ 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

key-pair

✏️ In Network settings, make sure you add the inbound security group with port 4444 and 7900 as below

Inbound security group

✏️ Once you click create instances make sure your instance is up and running

EC2 running state

✏️Click on connect option using SSH client as below

ssh -i "vj.pem" ec2-user@<ipv4 public dns>.compute-1.amazonaws.com
Enter fullscreen mode Exit fullscreen mode

You can either use SSH Client via putty agent or windows powershell. Once you are connected you will get as below

EC2 instance connected via SSH

🦋 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
Enter fullscreen mode Exit fullscreen mode

docker install

🐳 You can check for docker version using below command

docker --version
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

selenium standalone chrome installation

standalone chrome

👨🏻‍💻 To check containers are up and running. Enter the below command

docker ps -a
Enter fullscreen mode Exit fullscreen mode

container with chrome

👨🏻‍💻 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
Enter fullscreen mode Exit fullscreen mode

selenium standalone firefox installation

👨🏻‍💻 Copy the IPV4 or public IPV4 address with colon 4444, open it in a new tab

selenium grid with chrome

👨🏻‍💻 You can check how many images installed using below docker command

docker images
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

🦋 Another way - Creation of selenium grid with docker compose yml file

✏️ Create the docker-compose yml file using below command

touch docker-compose.yml
Enter fullscreen mode Exit fullscreen mode

✏️ 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 fullscreen mode Exit fullscreen mode

✏️ Enter the following command to make the docker up and running

docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

✏️ You will get below output once done with your installation

installation of docker compose

✏️ You can check all the docker containers running by using the below command

docker ps -a
Enter fullscreen mode Exit fullscreen mode

container ids

🦋 Checking the selenium grid using EC2 IPV4 address and port 4444

⭐ Checking the selenium grid using IPV4 public ip address and port 4444

selenium grid

🦋 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

queue size

containers

☀️ You can increase your queue size using below command

docker compose -f docker-compose-v3.yml up --scale chrome=2
Enter fullscreen mode Exit fullscreen mode

☀️ 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/");
}
Enter fullscreen mode Exit fullscreen mode

☀️ You can tear down the container using below command

docker-compose down
Enter fullscreen mode Exit fullscreen mode

☀️ Please make sure terminate your ubuntu instances created to avoid costs

ec2

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/

aws #awscommunitybuilder #awsreskill #awsec2 #selenium #docker #ubuntu #selenium-grid #docker-compose #cross-broswer #awsugmdu #awsugncr #cloudnloud #awsugncrdelhi

Top comments (0)