Running WebDriver Selenium tests on your Jenkins build server

So you have setup Jenkins as your build server and you test your project automated with Selenium WebDriver. Now you want to run the automated tests in a Jenkins job, but you get the following error:


. Caused by: org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
.
. (process:26912): GLib-CRITICAL **: g_slice_set_config: assertion 'sys_page_size == 0' failed
. Error: cannot open display: :1

This post explains how to fix this. The reason is that the user that runs your Jenkins service does not have a display. Therefore there is no possibility to open a browser like Firefox.

In order the facilitate this, we will use xvfb. This is a display server that performs all graphical user interface [GUI] operations in memory and without showing any screen output.

1. Install FireFox on the machine that runs Jenkins
In /etc/apt/sources.list add the following line.

ppa:mozillateam/firefox-stable

Run the following commands to upgrade or install latest version of Firefox to work with Selenium.

sudo apt-get update
sudo apt-get install firefox

2. Install Xvfb on your server
Run the following command to install Xvfb on your server

apt-get install xvfb x11-xkb-utils xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic x11-apps

3. Install Xvfb in Jenkins
Install the Xvfb plugin https://wiki.jenkins-ci.org/display/JENKINS/Xvfb+Plugin
Schermafbeelding 2015-06-21 om 19.28.28

Configure the plugin via Manage Jenkins / Configure System
Schermafbeelding 2015-06-21 om 20.45.31

4. Configure Xvfb in your Jenkins job
Schermafbeelding 2015-06-21 om 20.45.54

Nexus: Remote host closed connection during handshake

If you have below exception, you should probably update Maven to a newer version!

Caused by: org.apache.maven.artifact.deployer.ArtifactDeploymentException: Faile
d to retrieve remote metadata nl:parent:1.18-SNAPSHO
T/maven-metadata.xml: Could not transfer metadata nl:parent:1.18-SNAPSHOT/maven-metadata.xml from/to corporateRepo (https://maven.company.nl/nexus/content/repositories/rib-snapshot): Remote host closed connectio
n during handshake
at org.apache.maven.artifact.deployer.DefaultArtifactDeployer.deploy(Def
aultArtifactDeployer.java:141)
at org.apache.maven.plugin.deploy.AbstractDeployMojo.deploy(AbstractDepl
oyMojo.java:171)
at org.apache.maven.plugin.deploy.DeployMojo.deployProject(DeployMojo.ja
va:242)
… 22 more
Caused by: org.sonatype.aether.deployment.DeploymentException: Failed to retriev
e remote metadata nl:parent:1.18-SNAPSHOT/maven-meta
data.xml: Could not transfer metadata nl:parent:1.18
-SNAPSHOT/maven-metadata.xml from/to corporateRepo (https://maven.company.nl/ne
xus/content/repositories/rib-snapshot): Remote host closed connection during han
dshake
at org.sonatype.aether.impl.internal.DefaultDeployer.upload(DefaultDeplo
yer.java:403)
at org.sonatype.aether.impl.internal.DefaultDeployer.deploy(DefaultDeplo
yer.java:256)
at org.sonatype.aether.impl.internal.DefaultDeployer.deploy(DefaultDeplo
yer.java:211)
at org.sonatype.aether.impl.internal.DefaultRepositorySystem.deploy(Defa
ultRepositorySystem.java:443)
at org.apache.maven.artifact.deployer.DefaultArtifactDeployer.deploy(Def
aultArtifactDeployer.java:137)
… 24 more
Caused by: org.sonatype.aether.transfer.MetadataTransferException: Could not tra
nsfer metadata nl.company.gict.rib.emandates:parent:1.18-SNAPSHOT/maven-metadat
a.xml from/to corporateRepo (https://maven.company.nl/nexus/content/repositorie
s/rib-snapshot): Remote host closed connection during handshake
at org.sonatype.aether.connector.wagon.WagonRepositoryConnector$3.wrap(W
agonRepositoryConnector.java:930)
at org.sonatype.aether.connector.wagon.WagonRepositoryConnector$3.wrap(W
agonRepositoryConnector.java:920)
at org.sonatype.aether.connector.wagon.WagonRepositoryConnector$GetTask.
run(WagonRepositoryConnector.java:669)
at org.sonatype.aether.util.concurrency.RunnableErrorForwarder$1.run(Run
nableErrorForwarder.java:60)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExec
utor.java:908)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
.java:931)
at java.lang.Thread.run(Thread.java:773)
Caused by: org.apache.maven.wagon.TransferFailedException: Remote host closed co
nnection during handshake
at org.apache.maven.wagon.shared.http4.AbstractHttpClientWagon.fillInput
Data(AbstractHttpClientWagon.java:799)
at org.apache.maven.wagon.StreamWagon.getInputStream(StreamWagon.java:11
6)
at org.apache.maven.wagon.StreamWagon.getIfNewer(StreamWagon.java:88)
at org.apache.maven.wagon.StreamWagon.get(StreamWagon.java:61)
at org.sonatype.aether.connector.wagon.WagonRepositoryConnector$GetTask.
run(WagonRepositoryConnector.java:601)
… 4 more
Caused by: javax.net.ssl.SSLHandshakeException: Remote host closed connection du
ring handshake
at com.ibm.jsse2.SSLSocketImpl.a(SSLSocketImpl.java:412)
at com.ibm.jsse2.SSLSocketImpl.h(SSLSocketImpl.java:643)
at com.ibm.jsse2.SSLSocketImpl.a(SSLSocketImpl.java:529)
at com.ibm.jsse2.k.write(k.java:3)
at org.apache.maven.wagon.providers.http.httpclient.impl.io.AbstractSess
ionOutputBuffer.flushBuffer(AbstractSessionOutputBuffer.java:131)
at org.apache.maven.wagon.providers.http.httpclient.impl.io.AbstractSess
ionOutputBuffer.flush(AbstractSessionOutputBuffer.java:138)
at org.apache.maven.wagon.providers.http.httpclient.impl.AbstractHttpCli
entConnection.doFlush(AbstractHttpClientConnection.java:270)
at org.apache.maven.wagon.providers.http.httpclient.impl.AbstractHttpCli
entConnection.flush(AbstractHttpClientConnection.java:275)
at org.apache.maven.wagon.providers.http.httpclient.impl.conn.AbstractCl
ientConnAdapter.flush(AbstractClientConnAdapter.java:197)
at org.apache.maven.wagon.providers.http.httpclient.protocol.HttpRequest
Executor.doSendRequest(HttpRequestExecutor.java:258)
at org.apache.maven.wagon.providers.http.httpclient.protocol.HttpRequest
Executor.execute(HttpRequestExecutor.java:123)
at org.apache.maven.wagon.providers.http.httpclient.impl.client.DefaultR
equestDirector.tryExecute(DefaultRequestDirector.java:645)
at org.apache.maven.wagon.providers.http.httpclient.impl.client.DefaultR
equestDirector.execute(DefaultRequestDirector.java:464)
at org.apache.maven.wagon.providers.http.httpclient.impl.client.Abstract
HttpClient.execute(AbstractHttpClient.java:820)
at org.apache.maven.wagon.providers.http.httpclient.impl.client.Abstract
HttpClient.execute(AbstractHttpClient.java:754)
at org.apache.maven.wagon.shared.http4.AbstractHttpClientWagon.execute(A
bstractHttpClientWagon.java:674)
at org.apache.maven.wagon.shared.http4.AbstractHttpClientWagon.fillInput
Data(AbstractHttpClientWagon.java:793)
… 8 more
Caused by: java.io.EOFException: SSL peer shut down incorrectly
at com.ibm.jsse2.b.a(b.java:200)
at com.ibm.jsse2.SSLSocketImpl.a(SSLSocketImpl.java:209)
… 24 more

Run shell scripts on Jenkins as super user [sudo] without given too much privileges

You have a Jenkins build server and want to run a script which requires super user rights. But you do not want do make your jenkins user a super user.

With the following you give your jenkins user only sudo rights for a specific script.

In the /etc/sudoers file you can give sudo rights for a specific file to the user with which you run jenkins.
Schermafbeelding 2014-12-02 om 19.48.27

Then in your Jenkins job, you can sudo this script
Schermafbeelding 2014-12-02 om 19.48.46

Add a HTTPS certificate to your Java keychain

You have a tool like GIT or Maven that connects to a HTTPS website. Then your tool needs to know whether to trust that website’s certificate.

When that tool uses Java underneath, you can follow the following steps to add the website’s certificate to your keystore.

1) Visit the website [like for example https://maven.company.nl/nexus] using Firefox.

2) (optional) Confirm and add the exception if you get the notification “This Connection is Untrusted”

3) Export the certificate to a file with the name “CompanyCA.pem” ( click on lock left next to the domain name -> More information -> View certificate -> Details -> Export (choose format “X.509 Certificate with chain PEM”))

Click on the lock
Schermafbeelding 2014-11-27 om 20.51.54

View the details
Schermafbeelding 2014-11-27 om 20.52.36

View the certificate
Schermafbeelding 2014-11-27 om 20.52.49

Export the certificate with format “X.509 Certificate with chain PEM”
Schermafbeelding 2014-11-27 om 21.24.46

4) Add the certificate to you cacerts keychain to the java version that maven uses via the following steps

4.1) Determine which java version your maven uses via
mvn –version
https-keychain-security

4.2) On the command line, go to the java directory via
cd < java home directory >

Note: when you use Windows, then you need to start the command prompt as “Run as administrator”.

4.3) open the lib/security directory within this folder
4.4) Add the website’s CA certificate to the cacerts keychain via
keytool -keystore cacerts -importcert -alias companyca -file < file directory >/CompanyCA.pem

Note Replace the placeholder < file directory > with the file location where you stored the pem certificate. If you are on a unix like system, then you need to prefix this command with sudo.

Provide your password. By default this is changeit
Schermafbeelding 2014-11-27 om 21.07.31

When asked to trust it, type yes
Schermafbeelding 2014-11-27 om 21.08.22

When the notification “Certificate was added to keystore” is shown without errors, then you are done!

Additionally
Do ensure that in your settings.xml and your pom files, all links to you repository point to the https url, so for example https://maven.company.nl/nexus

How do know your certificate is not trusted properly?
When you get one of the below exceptions…

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project parent: Failed to retrieve remote metadata nl.company:parent:1.5-SNAPSHOT/maven-metadata.xml: Could not transfer metadata nl.company:parent:1.5-SNAPSHOT/maven-metadata.xml from/to corporateRepo (https://maven.company.nl/nexus/content/repositories/snapshots): peer not authenticated -> [Help 1]

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project parent: Failed to retrieve remote metadata nl.company:parent:1.5-SNAPSHOT/maven-metadata.xml: Could not transfer metadata nl.company:parent:1.5-SNAPSHOT/maven-metadata.xml from/to corporateRepo (https://maven.company.nl/nexus/content/repositories/snapshots): sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target -> [Help 1]

Store Subversion credentials in Maven

When you are using Subversion from the command line instead of via a tool like TortoiseSVN, then you do NOT want to provide your username and password everything again when you want to update or commit the code of your Maven project.

To ensure that your SVN never asks for the credentials again, you can set these in your settings.xml via below addition.

The ID needs to be the host of your Subversion repository. So, when your Subversion url is “https://subversion.company.nl/svn/projects/project1″, then the id is “subversion.company.nl”.

  <servers>
    <server>
      <id>subversion.company.nl</id>
      <username>...</username>
      <password>...</password>
    </server>

    ...
  </servers>

An example of an error from the maven-release-plugin when the [proper] SVN credentials are not available.

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.0:prepare (default-cli) on project internet-portal-parent: Unable to commit files
[ERROR] Provider message:
[ERROR] The svn command failed.
[ERROR] Command output:
[ERROR] svn: E170001: Commit failed (details follow):
[ERROR] svn: E170001: Unable to connect to a repository at URL 'https://subversion.company.nl/svn/projects/internet-portal-parent/trunk'
[ERROR] svn: E170001: OPTIONS of 'https://subversion.company.nl/svn/projects/internet-portal-parent/trunk': authorization failed: Could not authenticate to server: rejected Basic challenge (https://subversion.company.nl)
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException