django – How to create a firefox health check

I have written some functional tests for a django app which I test with selenium in a docker container. I use circleci for ci/cd. I have also written a health test for selenium hub to ensure it is up and running before the testing can begin. It looks like below:

#!/usr/bin/env python3


import logging
import time
import urllib.request

logger = logging.getLogger("selenium-hub-health-check")

status = 0
while status != 200:
    try:
        f = urllib.request.urlopen('http://selenium-hub:4444/wd/hub/status')
        status = f.status
        logger.info("The Selenium hub is ready!")
        f.close()
        logger.info("Closing this session")
        time.sleep(1)
    except Exception as e:
        logger.error(e)
        time.sleep(1)
        pass

This one runs fine. However, recently, I have started noticing the error below in one in four tests which I believe comes from the firefox service that I’m also using as the browser not being ready.

ERROR: test_visitor_can_access_the_about_page (functional_tests.navigation.ftest_endpoints.EndpointsVisitorTest)
When a user goes to the browser and types the domain with the
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/teke/common/tests/base_function_tests.py", line 8, in setUp
    self.browser = webdriver.Remote(
  File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Error forwarding the new session Empty pool of VM for setup Capabilities {acceptInsecureCerts: true, browserName: firefox, marionette: true}
Stacktrace:
    at org.openqa.grid.web.servlet.handler.RequestHandler.process (RequestHandler.java:118)
    at org.openqa.grid.web.servlet.DriverServlet.process (DriverServlet.java:85)
    at org.openqa.grid.web.servlet.DriverServlet.doPost (DriverServlet.java:69)
    at javax.servlet.http.HttpServlet.service (HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service (HttpServlet.java:790)
    at org.seleniumhq.jetty9.servlet.ServletHolder.handle (ServletHolder.java:865)
    at org.seleniumhq.jetty9.servlet.ServletHandler.doHandle (ServletHandler.java:535)
    at org.seleniumhq.jetty9.server.handler.ScopedHandler.handle (ScopedHandler.java:146)
    at org.seleniumhq.jetty9.security.SecurityHandler.handle (SecurityHandler.java:548)
    at org.seleniumhq.jetty9.server.handler.HandlerWrapper.handle (HandlerWrapper.java:132)
    at org.seleniumhq.jetty9.server.handler.ScopedHandler.nextHandle (ScopedHandler.java:257)
    at org.seleniumhq.jetty9.server.session.SessionHandler.doHandle (SessionHandler.java:1595)
    at org.seleniumhq.jetty9.server.handler.ScopedHandler.nextHandle (ScopedHandler.java:255)
    at org.seleniumhq.jetty9.server.handler.ContextHandler.doHandle (ContextHandler.java:1340)
    at org.seleniumhq.jetty9.server.handler.ScopedHandler.nextScope (ScopedHandler.java:203)
    at org.seleniumhq.jetty9.servlet.ServletHandler.doScope (ServletHandler.java:473)
    at org.seleniumhq.jetty9.server.session.SessionHandler.doScope (SessionHandler.java:1564)
    at org.seleniumhq.jetty9.server.handler.ScopedHandler.nextScope (ScopedHandler.java:201)
    at org.seleniumhq.jetty9.server.handler.ContextHandler.doScope (ContextHandler.java:1242)
    at org.seleniumhq.jetty9.server.handler.ScopedHandler.handle (ScopedHandler.java:144)
    at org.seleniumhq.jetty9.server.handler.HandlerWrapper.handle (HandlerWrapper.java:132)
    at org.seleniumhq.jetty9.server.Server.handle (Server.java:503)
    at org.seleniumhq.jetty9.server.HttpChannel.handle (HttpChannel.java:364)
    at org.seleniumhq.jetty9.server.HttpConnection.onFillable (HttpConnection.java:260)
    at org.seleniumhq.jetty9.io.AbstractConnection$ReadCallback.succeeded (AbstractConnection.java:305)
    at org.seleniumhq.jetty9.io.FillInterest.fillable (FillInterest.java:103)
    at org.seleniumhq.jetty9.io.ChannelEndPoint$2.run (ChannelEndPoint.java:118)
    at org.seleniumhq.jetty9.util.thread.strategy.EatWhatYouKill.runTask (EatWhatYouKill.java:333)
    at org.seleniumhq.jetty9.util.thread.strategy.EatWhatYouKill.doProduce (EatWhatYouKill.java:310)
    at org.seleniumhq.jetty9.util.thread.strategy.EatWhatYouKill.tryProduce (EatWhatYouKill.java:168)
    at org.seleniumhq.jetty9.util.thread.strategy.EatWhatYouKill.produce (EatWhatYouKill.java:132)
    at org.seleniumhq.jetty9.util.thread.QueuedThreadPool.runJob (QueuedThreadPool.java:765)
    at org.seleniumhq.jetty9.util.thread.QueuedThreadPool$2.run (QueuedThreadPool.java:683)
    at java.lang.Thread.run (Thread.java:748)

I would appreciate any tips on how to write a firefox health check similar to the one above to prevent this occurrence from happening from time to time. Below is the docker-compose file responsible for setting up the selenium environment.

version: "3.7"
services:
  teke:
    container_name: teke_running_app_test
    networks:
      - teke_test_network
    depends_on:
      - database
  teketestservice:
    build:
      context: .
    container_name: teke_testservice_test
    networks:
      - teke_test_network
    environment:
      POSTGRES_PASSWORD: db-pwd
      POSTGRES_USER: db-user
      POSTGRES_DB: db-name
      POSTGRES_IP: database
      SECRET_KEY: secret-key
    depends_on:
      - database
      - firefox
      - selenium-hub
      - teke
    command:
      - sh
      - -c
      - "postgres-healthcheck && firefox-healthcheck && selenium-healthcheck && python manage.py test -p ftest*.py"
  database:
    container_name: teke_db_test
    networks:
      - teke_test_network
  selenium-hub:
    image: selenium/hub:3.141.59-20210128
    container_name: selenium-hub
    depends_on:
      - firefox
    ports:
      - "4444:4444"
    networks:
      - teke_test_network
  firefox:
    image: selenium/node-firefox:3.141.59-20210128
    container_name: firefox
    shm_size: 2gb
    volumes:
      - /dev/shm:/dev/shm
    environment:
      - HUB_HOST=selenium-hub
      - HUB_PORT=4444
    networks:
      - teke_test_network
networks:
  teke_test_network:
    name: teke_test_network

Thanks in advance

Product of the Month September 2016

Source link

Leave a Comment

Your email address will not be published. Required fields are marked *