0

How do you run Selenium based tests inside Docker?

I'm trying to get some Python+Selenium tests, which use Firefox and Geckodriver, to run under an Ubuntu 18 Docker image.

My docker-compose.yml file is simply:

version: "3.5"
services:
  app_test:
    build:
      context: .
      shm_size: '4gb'
      mem_limit: 4096MB
      dockerfile: Dockerfile.test

Unfortunately, most tests are failing with errors like:

selenium.common.exceptions.NoSuchWindowException: Message: Browsing context has been discarded

The few search results I can find mentioning this error suggest it's because of low memory. The server I'm running the tests on has 8GB of total memory, although I also tested on a machine with 32GB and received the same error.

I also added a call to print the output of top before each test, and it's showing virtually no memory usage, so I'm not sure what would be causing the test to crash due to insufficient memory.

Some articles suggested adding the shm_size and mem_limit lines, but those had no effect.

I've also tried different versions of Firefox, from the most recent 71 version to the older ESR releases, to rule out it's not a bug due to incompatible versions of Firefox+Selenium+Geckodriver. I'm otherwise following this compatibility table.

What is causing this error and how do I fix it?

Cerin
  • 60,957
  • 96
  • 316
  • 522
  • is your container able to consume that much ram or are you opening an absurd amount of files in your test? you might want to check your `ulimit` values on your host. http://geekswing.com/geek/quickie-tutorial-ulimit-soft-limits-hard-limits-soft-stack-hard-stack/ – ckaserer Jan 08 '20 at 06:46
  • 1
    @DebanjanB This is not a duplicate. That other question does not involve Docker. – Cerin Jan 08 '20 at 16:27
  • @ckaserer Like I mentioned in my question, I print the output of `top` in my tests, and it does not appear to be using much memory at all. At most, it seems to use only 1%. I only increased the limits to rule out the possibility that I was misreading this. My tests don't open my sites, and only browse a few pages. Basic CRUD type tests. – Cerin Jan 08 '20 at 16:29
  • @Cerin Irespective of using Docker or localhost the basic cause of this error remains the same. Can you update the question with your code trials? – undetected Selenium Jan 08 '20 at 19:08
  • @Cerin Do you have an update or solution for your question? I am encountering the same `NoSuchWindowException` when running Python+Selenium+pyvirtualdisplay in an Ubuntu docker container with XVFB installed. – Conrad May 03 '20 at 16:55

1 Answers1

1

Root cause could be running out of RAM memory.

To fix it run the docker container adding --shm-size.

Example:

 --shm-size="2G"
Javier
  • 151
  • 1
  • 6