Search code examples
dockerseleniumkubernetesjenkins-pipelinewebdriver-io

Create a selenium backend in a jenkins pipeline


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?


Solution

  • 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.