Setting Up a Continuous Testing Environment With Selenium-Grid, Docker and Jenkins Part-3

Ahmet Yasin Türkyılmaz
3 min readFeb 6, 2020

--

In part 2 I did mention about an issue which is trying to connect Selenium Hub in a Docker container. In this part I will go through on that issue and make a Pipeline to start testing.

Part-2 link : https://medium.com/@ahmet.turkyilmaz/setting-up-a-continuous-testing-environment-with-selenium-grid-docker-and-jenkins-part-2-4d165e8fb5a2

Part-3 Making A Pipeline And Pushing Code Into Jenkins

Think about it as a virtual machine. Jenkins running in a virtual machine, you are pushing code into Jenkins and try to connect Selenium Hub running in actual machine. This won’t work because they are not in the same network.

There is 2 easy ways to get this issue done. First way is simply change the localhost:4444 part in the code with selenium-hub:4444.

public class BrowserSettings {
private WebDriver driver = null;
private Properties dataDrivenProperties;
DesiredCapabilities capability;

public BrowserSettings() {
dataDrivenProperties = new Properties();
try {
dataDrivenProperties.load(getClass().getClassLoader().getResourceAsStream("datadriven.properties.sample"));
} catch (IOException e) {

Logger.getLogger(getClass().getName()).log(Level.SEVERE, e.getMessage(), e);
}
}

public WebDriver getWebDriver() throws IOException {
capability = DesiredCapabilities.chrome();
capability.setCapability("--no-sandbox", true);
capability.setCapability("--disable-impl-side-painting", true);
capability.setCapability("--disable-gpu", true);
capability.setCapability("--disable-gpu-sandbox", true);
capability.setCapability("--test-type=ui", true);
capability.setCapability("--disable-accelerated-2d-canvas", true);
capability.setCapability("--disable-accelerated-jpeg-decoding", true);

if (dataDrivenProperties.getProperty("browser").equals("chrome")) {
String localhost = "selenium-hub:4444";
driver = new RemoteWebDriver(new URL("http://" + localhost + "/wd/hub"), capability);

}
return driver;
}
}

Second way is through kitematic, portraine, terminal or whatever you use to play with docker. In this way, you just have to expose the ports of containers. And let them communicate with each other. To do that I’ll just build a bridge the containers through kitematic — container settings — network tab.

Now that your containers communicate we can get to Jenkins side. So our next step is building a simple pipeline project to see the results. Before dealing with the pipeline you must define a Maven with a name of “M3” in Jenkins. After that your pipeline should look like this.

Pipeline is going to be something like this:

//variables

def mvn_version = ‘M3’

node {

stage(‘Prepare’) {

echo ‘Clone repo and checkout development branch.’

deleteDir()

git url: ‘https://github.com/ahmetturkyilmaz/SeleniumGridProject.git'

echo ‘Repo cloned successfully!’

}

stage(‘Test’) {

try {

withEnv([“PATH+MAVEN=${tool mvn_version}/bin”]) {

sh “mvn clean install -DtestngFile=testng.xml”

}

} catch (err) {

echo err

}

echo currentBuild.result

}

}

Save this pipeline and go back to the project page and simply press Build project. And you will see a result like this.

So Our testing environment with Jenkins and Docker ready to testing regularly.

--

--

Ahmet Yasin Türkyılmaz

I am a Software Engineer studying Electronic Engineering in Gebze Technical University. And also working at Continuous Software.