How to get Selenium working with Jenkins2 in GCP

10/25/2017

I'm trying to get Selenium Grid and Jenkins working together in GKE.

I found the Selenium plugin (https://plugins.jenkins.io/selenium) for Jenkins, but I'm not sure it can be used to get what I want.

I stood Jenkins up by following the steps here: https://github.com/GoogleCloudPlatform/kube-jenkins-imager ( I changed the image for the jenkins node to use Jenkins 2.86 )

This creates an instance of Jenkins running in kubernetes that spawns slaves into the cluster as needed.

But I don't believe that this is compatible with the Selenium plug-in. What's the best way to take what I have and get it working with this instance of Jenkins?

I was also able to get an instance of Selenium up and going in the same cluster using this: https://gist.github.com/elsonrodriguez/261e746cf369a60a5e2d ( I dropped the version 2.x from the instances to pull in the latest containers. )

I had to bump the k8s nodes up to n1-standard-2 (2 vCPUs, 7.5 G Memory ) to get those containers to run.

For this proof of concept, the SE nodes don't need to be ephemeral. But I'm unsure what kind of permanent node container image I can deploy in k8s that would have the necessary SE drivers.

On the other hand, maybe it would be easier to just use the stand-alone SE containers that I found. If so, how do I use them with Jenkins2?

Has anyone else gone down this path?

Edit: I'm not interested in third-party selenium services at this time.

-- Cognitiaclaeves
google-kubernetes-engine
jenkins
jenkins-plugins
linux
selenium

2 Answers

10/25/2017

SauceLabs is a selenium grid in the cloud.

I wrote Saucery to make integrating from C# or Java with NUnit2, NUnit3 or JUnit 4 easy.

You can see the source code here, here and here or take a look at the Github Pages site here for more information.

-- Andrew Gray
Source: StackOverflow

10/31/2017

Here is what I figured out.

I saw many indications that it was a hassle to run your own instance of Selenium grid. Enough time may have passed for this to be a little easier than it used to be. There seem to be a few ways to do it.

Jenkins itself has a plugin that is supposed to turn your Jenkins cluster into a Selenium 3 grid: https://plugins.jenkins.io/selenium . The problem I had with this is that I'm planning on hosting these instances in the cloud, and I wanted the Jenkins slaves to be ephemeral. I was unable to figure out how to get the plugin to work with ephemeral slaves.

I was trying to get this done as quickly as I could, so I only spent three days total on this project.

These are the forked repos that I'm using for the Jenkins solution: https://github.com/jnorment-q2/kube-jenkins-imager which basically implements this: https://github.com/jnorment-q2/continuous-deployment-on-kubernetes

I'm pointing to my own repos to give a reference to exactly what I used in late October 2017 to get this working. Those repos are forked from the main repos, and it should be easy to compare the differences.

I had contacted google support with a question, they responded that this link might actually be a bit clearer: https://cloud.google.com/solutions/jenkins-on-container-engine-tutorial

From what I can tell, this is a manual version of the more automated scripts I referenced.

To stand up Selenium, I used this: https://github.com/jnorment-q2/selenium-on-k8s

This is a project I built from a gist referenced in the Readme, which references a project maintained by SeleniumHQ.

The main trick here is that Selenium is resource hungry. I had to use the second tier of google compute engines in order for it to deploy in Kubernetes. I adapted the script I used to stand up Jenkins to deploy Selenium Grid in a similar fashion.

Also of note, there appear to be only Firefox and Chrome options in the project from SeleniumHQ. I have yet to determine if it is even possible to run an instance of Safari.

For now, this is what we're going to go with.

The piece left is how to make a call to the Selenium grid from Jenkins. It turns out that selenium can be pip-installed into ephemeral slaves, and webdriver.Remote can be used to make the call.

Here is the demo script that I wrote to prove that everything works: https://github.com/jnorment-q2/demo-se-webdriver-pytest/blob/master/test/testmod.py

It has a Jenkinsfile, so it should work with a fresh instance of Jenkins. Just create a new pipeline, change definition to 'Pipeline script from SCM', Git, https://github.com/jnorment-q2/demo-se-webdriver-pytest, then scroll up and click 'run with parameters' and add the parameter SE_GRID_SERVER with the full url ( including port ) of the SE grid server.

It should run three tests and fail on the third. ( The third test requires additional parameters for TEST_URL and TEST_URL_TITLE )

-- Cognitiaclaeves
Source: StackOverflow