Setting up Selenium Grid with Docker and Configure on Jenkins.

Setting up Selenium Grid with Docker and run test Cases on Different browsers.

Today, In this article we will seen how Selenium grid helps to run our test cases in different browsers on Ubuntu using Jenkins.

My Setup:

 

Jenkins and Docker are installed ubuntu server(IP Address: 192.168.1.2 (Node2))
VNC Viewer Installed on ubuntu Desktop(IP address: 192.168.1.1(Node1))

1) Basic Selenium test in Python.

Let’s start with simple and basic Selenium test in Python. You can download from link.

import time

import unittest  

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

class ITTroubleshooterSearchTest(unittest.TestCase):
    def setUp(self):

         self.browser = webdriver.Chrome()

    def test_ITTroubleshooter_search_for(self):

        browser = self.browser

        browser.get('https://www.google.com/')

        search_box = browser.find_element_by_name('q')

        search_box.send_keys('ittroubleshooter.in')

        search_box.send_keys(Keys.RETURN)

        browser.get('https://ittroubleshooter.in/')

        time.sleep(3)  # simulate long running test

    def tearDown(self):

        self.browser.quit()  # quit vs close?

if __name__ == '__main__':

    unittest.main()

2) Deploy Selenium hub image on docker.

On Node2(i.e 192.168.1.2), To start Selenium hub from Docker container, refer below command;

docker run -d -p 4444:4444 --name selenium-hub selenium/hub

Once you type and enter above command in your terminal, the Selenium hub will be open from the Docker container. You can verify whether Selenium hub has started or not on your browser: http://192.168.1.2:4444/grid/console

3) Deploy Selenium Nodes image on docker.

On Node2, We used to start chrome node and Firefox node. We have install both the node images into our Docker Machine.

docker run -d  --link selenium-hub:hub selenium/node-chrome-debug

The above command will run chrome node from Docker and you can access (or debug using vnc) from 36361 port number.

docker run -d  --link selenium-hub:hub selenium/node-firefox-debug

This command will run Firefox node from Docker and you can access (or debug using vnc) from 36362 port number.

# docker ps 
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9492c8b625be selenium/node-firefox-debug "/opt/bin/entry_poin…" 2 minutes ago Up 2 minutes 0.0.0.0:36362->5900/tcp keen_wright
126babef772b selenium/node-chrome-debug "/opt/bin/entry_poin…" 2 minutes ago Up 2 minutes 0.0.0.0:32769->5900/tcp admiring_driscoll
dea57cca41f6 selenium/hub "/opt/bin/entry_poin…" 3 minutes ago Up 3 minutes 0.0.0.0:4444->4444/tcp selenium-hub

4) Download and install VNC on Ubuntu.

On Node1(Desktop Machine), You can download vnc viewer from their official site:

I Installed on ubuntu Machine:

 

Once you install vnc, try to connect hub URL and the port number. Click on continue:

 

 

 

 

After clicking on the connect button VNC viewer will ask for a password. By default the password for VNC viewer is secret, type the password as secret and click Ok and you will be able to see a window for the  Firefox browser.

 

 

Same as it is you can use Chrome browser by using a VNC viewer. For Chrome browser 192.168.1.2:32769

After login on VNC you can see below result;

 

 

Once done, open a browser and navigate to the Selenium Grid console at http://192.168.1.2:4444/grid/console to ensure check both of the browser.

5) Configure the remote driver in the test file.

In  step1, we wrote test file, Now we need to update that file with remote driver by updating the setup method.

import os

def setUp(self):
    caps = {'browserName': os.getenv('BROWSER', 'chrome')}
    self.browser = webdriver.Remote(
        command_executor='http://192.168.1.2:4444/wd/hub',
        desired_capabilities=caps
     )

Final Selenium test file.

import time

import unittest

import os    

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

class ITTroubleshooterSearchTest(unittest.TestCase):

    def setUp(self):

        caps = {'browserName': os.getenv('firefox', 'firefox')}

        self.browser = webdriver.Remote(

            command_executor='http://192.168.1.2:4444/wd/hub',

            desired_capabilities=caps

        )

    # def setUp(self):

    #     self.browser = webdriver.Chrome()

    def test_ITTroubleshooter_search_for(self):

        browser = self.browser

        browser.get('https://www.google.com/')

        search_box = browser.find_element_by_name('q')

        search_box.send_keys('ittroubleshooter.in')

        search_box.send_keys(Keys.RETURN)

        browser.get('https://ittroubleshooter.in/')

        time.sleep(3)  # simulate long running test

    def tearDown(self):

        self.browser.quit()  # quit vs close?

if __name__ == '__main__':

    unittest.main()

Note: We simulate test file on firefox browser. You can change the browser using add new line to test2.py file.

caps = {'browserName': os.getenv('chrome', 'chrome')}

6) Integrating our Test file Into Jenkins.

We have all the pieces in place, all we need to do is create and configure Jenkins job. Enter an appropriate name for the new Job, select “Pipeline ” project and click on “Save”.

 

Select pipeline script in definition section and paste the pipeline script.

 

node {
        def newApp
        def registry = 'ittroubleshooter/docker-test'
      
      stage('Git') {
              git 'https://github.com/kumargaurav522/selenium.git'
      }
        
        stage('Building image') {
        def buildName = registry + ":$BUILD_NUMBER"
                newApp = docker.build buildName
        }
        stage('Run Docker Image'){
            sh 'docker run -d ittroubleshooter/docker-test:${BUILD_NUMBER}'
            
        }
}

7) Check Selenium test on browser.

Now we have build the job on jenkins, After running job, you will be able to see the test running through two different browsers.

Check VNC viewer will show how a browser navigates to each website.

Hope this post will help Devops beginners. Please share you feedback and Comments. Stay tune for more updates with ittroubleshooter.in …!!!

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of