Wednesday, May 22, 2019

Selenium Grid + Docker + AWS EC2

This is going to be a simple (and single) entry point for setting up and running Selenium Grid inside of Docker containers on AWS EC2.

So steps will be the next:


1. Create and start EC2 instance
2. Open port 4444 as it’s used by the grid through the adding needed security rule:
    I. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/
    II. In the navigation pane, choose the Security Groups link. 
    III. Select the Inbound tab, choose Edit.
    IV. In the dialog, choose Add Rule and do the following:
        - Type: Custom TCP
        - Protocol: TCP
        - Part Range: 4444
        - Source: My IP
        - Description: Selenium Grid (or any other one)

Once the Selenium Grid is up and running, if 52.222.124.100 (for example) is the public facing IP address  connecting to http://52.222.124.100:4444/grid/console will connect to the console.

Login into and install Docker:

Install Docker on the AWS instance: 

$ sudo yum install -y docker

Start the docker service: 

$ sudo service docker start

Add the ec-2-user to the Docker group: 

$ sudo usermod -a -G docker ec2-user

Close the Mac Terminal and reopen it to reset permissions.


$ docker network create grid

$ docker run -d -p 4444:4444 --net grid --name selenium-hub selenium/hub:3.11.0-bismuth

$ docker run -d --net grid -e HUB_HOST=selenium-hub -v /dev/shm:/dev/shm selenium/node-chrome:3.11.0-bismuth

$ docker run -d --net grid -e HUB_HOST=selenium-hub -v /dev/shm:/dev/shm selenium/node-firefox:3.11.0-bismuth

With docker-compose file it will be easier.

Install docker-compose:

$ sudo curl -L https://github.com/docker/compose/releases/download/1.20.1/docker-compose-'uname -s'-'uname -m' -o /usr/local/bin/docker-compose

$ sudo chmod +x /usr/local/bin/docker-compose

In home directory (directory from where You’re starting grid):

$ touch docker-compose.yaml

Paste next content (example from SeleniumHQ)
---------------------------------------------------------------------------

# To execute this docker-compose yml file use docker-compose -f <file_name> up  
 # Add the "-d" flag at the end for deattached execution  
 version: '2'  
 services:  
  firefox:  
   image: selenium/node-firefox:3.11.0-bismuth  
   volumes:  
    - /dev/shm:/dev/shm  
   depends_on:  
    - hub  
   environment:  
    HUB_HOST: hub  
   
  chrome:  
   image: selenium/node-chrome:3.11.0-bismuth  
   volumes:  
    - /dev/shm:/dev/shm  
   depends_on:  
    - hub  
   environment:  
    HUB_HOST: hub  
   
  hub:  
   image: selenium/hub:3.11.0-bismuth  
   ports:  
    - "4444:4444"  

---------------------------------------------------------------------------

$ docker-compose up -d - to start grid

$ docker-compose down - to stop grid


If needed more than 1 node either for Chrome or Firefox just add the entry into docker-compose file.

Example of usage will be something like that from Your selenium setup (for Jenkins it can be done separately):

@BeforeMethod(alwaysRun = true)
public void setupBaseTest() throws Exception {
    DesiredCapabilities dr = null;
    dr = DesiredCapabilities.chrome();
    driver = new RemoteWebDriver(new URL("http://52.222.124.100:4444/grid/console"), dr);
}

No comments:

Post a Comment