5

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
  • 168
  • 2
  • 17
  • There is an option for you. You can check the below link out in which they have shown how to configure zalenium and use it for the selenium test script execution. Just practice with the zalenium first after that you can create your own docker images with novnc configured in it to view the execution into the docker container. Right now you do not need any extra configuration in Jenkins to work with zalenium. You just need to expose the required ports and you are good to use the automatically created nodes of selenium grid. https://qautomation.blog/2019/08/28/zalenium-the-ultimate-tutorial/ – UnknownBeast May 15 '20 at 17:19
  • Thanks, zalenium looks cool. That is not quite what I wanted to know though. I know there are selenium docker images too that I could add in the containers section of my pipeline. What I am struggling with is how to approach a selenium server from my node applcation. I am not looking to create my own docker images. I just want to pull my code, start a server, and run my tests against it. I think the answer I am looking for lies more in the networking corner – Martijn May 15 '20 at 18:10

2 Answers2

2

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
  • 2,138
  • 4
  • 18
  • Ok, so that image has chrome. How would I start a Selenium server there? Just adding Selenium webdriver to my package.json doesn't do that. – Martijn May 26 '20 at 22:40
  • to start that, you need java installed as well. and then run `java -jar selenium.jar` and it will start the selenium server locally and in the same image itself, you need `nodejs` to run your test cases and `chrome` to run your test cases in that – nischay goyal May 26 '20 at 22:41
  • After starting Selenium, is it available at localhost, or somewhere else? – Martijn May 27 '20 at 05:52
  • It will be available `localhost:4444` – nischay goyal May 27 '20 at 13:38
  • Thanks for this answer, I think I have enough to go by now. Do you happen to know how I can install java in this node chrome image? – Martijn May 28 '20 at 08:11
  • Welcome. Try with `apt` package manager. `sudo apt update sudo apt install default-jdk` – nischay goyal May 28 '20 at 08:13
0

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
  • 2,138
  • 4
  • 18
  • Thanks for your effort, but the entire idea is that I don't have acces to the k8s environment where the current grid is hosted. I want to know how I can start a selenium server/grid from my pipeline, and how to approach it – Martijn May 26 '20 at 12:34
  • Please have a look at another answer which is not based on Kubernetes. – nischay goyal May 26 '20 at 13:27