Docker 02: How to execute selenium-python tests on docker containers?

Docker 02: How to execute selenium-python tests on docker containers?

Table of contents

No heading

No headings in the article.

In this blog post, I will show you how to execute the selenium-python tests in the chrome browser on docker containers.

The pre-requisites for this to understand this blog are:

  • Docker installation on your machine.

  • Basic knowledge of docker, pulling the images and running the containers.

If not installed follow the steps in the below link to complete the docker setup.

Step 1: Write simple selenium-python tests of your choice to execute on the docker container. Or else, you can also download my tests from here sample selenium script

''' this module contains tests to sequentially'''
from asyncio import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager

def test_my_blog_search():
    '''Executing the test_google_search'''
    driver=webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
    driver.maximize_window()
    driver.get("http://www.google.com")
    driver.find_element(By.NAME , "q").send_keys("brahmakothapalli.hashnode.com", Keys.ENTER)
    print(driver.title)
    print("Execution completed :: test_google_search")
    driver.quit()

def test_my_channel_youtube_search():
    '''Executing the test_google_search'''
    driver=webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
    driver.maximize_window()
    driver.get("http://www.google.com")
    # driver.find_element(By.ID , "search-input").click()
    driver.find_element(By.NAME , "q").send_keys("@brahmakothapalli youtube", Keys.ENTER)
    print(driver.title)
    print("Execution completed :: test_youtube_search")
    sleep(5)
    driver.quit()

Step 2: In order to execute above selenium tests on chrome browser, we have to pull the "selenium-chrome" docker image from the official SeleniumHQ GitHub account docker selenium grid

docker run -d -p 4444:4444 --shm-size="2g" selenium/standalone-chrome:4.6.0-20221104

Open a terminal or command prompt and run the above command. This command will pull the selenium-chrome image to our local machine first and then runs it, to start the selenium-chrome standalone container.

On successful command execution, the docker image will be downloaded and started as shown in the below image.

downloaded_image.png

Step 3: Now run the docker ps command to see all the containers that are up and running in our machine. Check the below image, we can see that selenium-chrome container is up and running.

docker_container.png

Now go to localhost:4444, we should see, the selenium grid with the chrome browser instance as shown in the below image.

selenium_grid.png

Step 4: Our test script is ready and our selenium container is also ready, only thing we have to do now is to point our WebDriver in the script to http://localhost:4444/wd/hub as shown below.

chrome_options = webdriver.ChromeOptions()
driver = webdriver.Remote(command_executor="http://localhost:4444/wd/hub", options=chrome_options)

Step 5: The last step, now execute our tests in the IDE. As soon as we execute our tests, we should see that the tests are being executed on the grid, as a docker container.

session_started.png

As shown in the above image, one session is started in the selenium grid on the docker container to execute the tests.

This is how we can execute our selenium tests on the docker container. Thank you for reading. Keep learning. #docker #selenium #python #brahmakothapalli

Did you find this article valuable?

Support QA Automation Enthusiast by becoming a sponsor. Any amount is appreciated!