Create a selenium backend in a jenkins pipeline

5/15/2020

I have a set of webdriver.io tests that are run in jenkins. They run against a selenium grid that is managed in k8s by an external company. I would like to have more control over my selenium backend, so I am trying to find a way to set up a selenium backend in my jenkins pipeline. My lack of docker/k8s networking knowledge is holding me back though.

This is rougly how my pipeline looks:

  agent {
    kubernetes {
      defaultContainer 'jnlp'
      yaml """
        apiVersion: v1
        kind: Pod
        spec:
            containers:
              - name: node
                image: node:12.14.1
                command:
                - cat
                tty: true
      """
    }
  }
  stages {
    stage('Checkout codebase') {
      // do checkout
      }  
    }
    stage('Build') {
      steps {
        container('node') {
            sh '''
                npm install --production
            '''
        }
      }
    }
    stage('Test-Mocha') {
      steps {
        container('node') {
            sh "node_modules/.bin/wdio ./test/config/wdio.conf.js --spec ./test/helpers/sandBox/sandbox1.js"
        }
      }
    }
  }
}

What I want is to run my tests against chrome. Any solution that will give me a chrome browser to run against is good.

I have tried to specify an extra container with selenium/standalone-chrome, but I have no idea how to run my tests against that container. I have also read up on setting up a selenium grid using docker containers, but I don't know how to run these commands in this pipeline, and even if this would work, I am not sure how to run against this grid.

Can anyone provide me with an example of what I could do to make this work?

-- Martijn
docker
jenkins-pipeline
kubernetes
selenium
webdriver-io

2 Answers

5/25/2020

There are a couple of ways to do it.

1. Using Selenium Grid (Ideal way) - Below are the steps:

  • Host a separate selenium Grid using the below docker-compose file.
version: '2'
services:
  firefox:
    image: selenium/node-firefox:3.14.0-gallium
    volumes:
      - /dev/shm:/dev/shm
    depends_on:
      - hub
    environment:
      HUB_HOST: hub

  chrome:
    image: selenium/node-chrome:3.14.0-gallium
    volumes:
      - /dev/shm:/dev/shm
    depends_on:
      - hub
    environment:
      HUB_HOST: hub

  hub:
    image: selenium/hub:3.14.0-gallium
    ports:
      - "4444:4444"
  • Execute the docker-compose up -d command to start the Selenium Grid which is running localhost:4444

  • Once this grid is running, please generate the wdio config which be used to run against a grid.

    Ref: https://webdriver.io/docs/clioptions.html

  • After config is generated, run your pipeline by using the pipeline which you mentioned in your question, which will run against the Selenium Grid

Note:- Use the new wdio config which you generated to use against the selenium grid.

Hope this helps.

-- nischay goyal
Source: StackOverflow

5/26/2020

There is 1 approach which is not via Kubernetes.

Use the below image in which nodejs and chrome both are installed.

  agent {
    kubernetes {
      defaultContainer 'jnlp'
      yaml """
        apiVersion: v1
        kind: Pod
        spec:
            containers:
              - name: node-chrome
                image: larcado/nodejs-chrome
                command:
                - cat
                tty: true
      """
    }
  }
  stages {
    stage('Checkout codebase') {
      // do checkout
      }  
    }
    stage('Build') {
      steps {
        container('node') {
            sh '''
                npm install --production
            '''
        }
      }
    }
    stage('Test-Mocha') {
      steps {
        container('node') {
            sh "node_modules/.bin/wdio ./test/config/wdio.conf.js --spec ./test/helpers/sandBox/sandbox1.js"
        }
      }
    }
  }
}

Make sure as part of package.json, selenium-webdriver is part of it.

-- nischay goyal
Source: StackOverflow