title: Run Cucumber tests inside a docker container using java
body: <p>Problem statement : There is a linux vm on  cloud , that we use to run our regression tests (The test source code is resides in the vm ), so we just need to create a docker container with a grid  installed , we then call mvn clean test to run the tests on the grid</p>
  
  <p>I tried two different approaches to this problem</p>
  
  <p>1) building all source and dependencies into jars , copying it to the container using volume mapping  and then running it in using  </p>
  
  <pre><code> java -cp websiteautomation-1.0.jar:websiteautomation-1.0-tests.jar:libs/* -Dcucumber.options=classpath:features org.junit.runnner.JUnitCore runner.CucumberRunner
  </code></pre>
  
  <p>The problem I am running with  this approach everytime is </p>
  
  <pre><code>Error: Could not find or load main class org.junit.runnner.JUnitCore
  
  Caused by: java.lang.ClassNotFoundException: org.junit.runnner.JUnitCore
  </code></pre>
  
  <p><strong>DEBUG STEPS:</strong></p>
  
  <p>I checked I was in right target folder where all the dependencies, sources and libs reside , I opened libs folder and saw there was  junit-4.12.jar</p>
  
  <p>2) I tried one more approach , since I had my project files already in the vm</p>
  
  <p>Create a selenium hub grid on the vm</p>
  
  <p>Create a working dir in the container that I am creating</p>
  
  <p>Map it to the project directory where my pom.xml is residing</p>
  
  <p>run maven command to run the tests </p>
  
  <pre><code>mvn clean  test -Dtest=CucumberRunner -Dbrowser.name=chrome -Denv.type=uat
  </code></pre>
  
  <p>The problem I am running with this approach is  (error snippets below )</p>
  
  <pre><code>org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
  Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
  tests_1 | System info: host: '57b902c11df9', ip: '172.20.0.2', os.name: 'Linux', os.arch: 'amd64', os.version: '4.9.125-linuxkit', java.version: '11.0.3'
  tests_1 | Driver info: driver.version: RemoteWebDriver
  
  Caused by: java.net.ConnectException: Failed to connect to localhost/127.0.0.1:4444
          tests_1 | at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:247)
  
  
  Caused by: java.net.ConnectException: Connection refused (Connection refused)
  </code></pre>
  
  <p><strong>DEBUG STEPS:</strong></p>
  
  <p>1) Grid is running , maven is downloading my dependencies , starts the test and throws the above errors , cannot find my grid</p>
  
  <p>2) Checked that my Remote driver set up is fine</p>
  
  <pre><code>Platform platform = Platform.fromString(os.toUpperCase());
  else if(os.contains("linux")) {
      System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/drivers/chrome/chromedriver_linux");
      ChromeOptions options = new ChromeOptions();
      options.setCapability("platform", platform);
      options.addArguments("--kiosk");
      driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),options);
      break;
  
  } 
  </code></pre>
  
  <p>3) Verified my HUB, chrome and firefox , selenium are all compatible versions</p>
  
  <p>I will also share my Dockerfile, docker-compose file and pom.xml ,</p>
  
  <p>I need another set of eyes , will be very helpful if you could figure whats wrong , I am now obsessed to fix this thing and get it working</p>
  
  <p><strong>Dockerfile</strong></p>
  
  <pre><code>FROM maven:3.6.0-jdk-8 AS build
  WORKDIR /usr/share/wow
  ENTRYPOINT mvn clean test -Dtest=CucumberRunner -Dbrowser.name=chrome -Denv.type=uat
  </code></pre>
  
  <p><strong>docker-compose.yml</strong></p>
  
  <pre><code>version: "3"
  
  services:
    hub:
      image: selenium/hub:3.141.59  #elgalu/selenium
      networks:
      - private
      ports:
      - 4444:4444
      - 5900:5900
  
      environment:
        PORT: 4444
        GRID_BROWSER_TIMEOUT: 300000
        GRID_TIMEOUT: 300000
        GRID_CLEAN_UP_CYCLE: 300000
        GRID_UNREGISTER_IF_STILL_DOWN_AFTER: 300000
        SE_OPTS: -browserTimeout 86400 -timeout 100
  
    chrome:
      image: selenium/node-chrome:3.141.59 #elgalu/selenium
      networks:
      - private
      depends_on:
      - hub
      environment:
        HUB_PORT_4444_TCP_ADDR: hub
        HUB_PORT_4444_TCP_PORT: 4444
        GRID_BROWSER_TIMEOUT: 300000
        GRID_TIMEOUT: 300000
        SCREEN_WIDTH: 1600
        SCREEN_HEIGHT: 1200
      volumes:
      - /dev/shm:/dev/shm # Mitigates the Chromium issue described at https://code.google.com/p/chromium/issues/detail?id=519952
  
  
    firefox:
        image: selenium/node-firefox:3.141.59 #elgalu/selenium
        networks:
        - private
        depends_on:
        - hub
        environment:
          HUB_PORT_4444_TCP_ADDR: hub
          HUB_PORT_4444_TCP_PORT: 4444
          GRID_BROWSER_TIMEOUT: 300000
          GRID_TIMEOUT: 300000
          SCREEN_WIDTH: 1600
          SCREEN_HEIGHT: 1200
        volumes:
        - /dev/shm:/dev/shm
  
    tests:
            image: vinodsiragaon/wow:v3
            depends_on:
              - chrome
              - firefox
              - hub
            working_dir: /usr/share/wow
            environment:
              - BROWSER=chrome
              - HUB_HOST=hub
              - HUB_PORT:4444
              - CUCUMBER_OPTIONS=classpath:features
            volumes:
              -  /Users/vinodsiragaon/Documents/workspace/WOW-Automation-Web:/usr/share/wow
              - ./output:/usr/share/wow/target/cucumber-reports
  
  
  networks:
    private:
      driver: bridge
  </code></pre>