Jenkins: Run Serenity acceptance tests without failure - maven

I'm trying to achieve the following:
Run a set of Serenity (plus Cucumber) tests as part of a build pipeline
Collect the reports regardless of whether all tests passed or not (they are especially useful in failures obviously)
In the case of test failures only, then email the contributors
Never fail the build because of a failed acceptance test as this pipeline is for the commit CI. Only want to fail if there are broken acceptance tests in the Nightly.
So with all that in mind I set off attempting to configure the build:
stage ('Serenity') {
steps {
// For the Delivery CI build don't fail on regression failure
sh 'mvn clean verify -pl regression -DskipCuke=false'
}
post {
always {
publishHTML([allowMissing: true, alwaysLinkToLastBuild: true,
keepAll: true, reportDir: 'regression/target/site/serenity',
reportFiles: 'index.html', reportName: 'Serenity',
reportTitles: ''])
}
failure{
echo 'There are regression suite failures.'
script {
currentBuild.result = 'SUCCESS'
}
emailext attachLog: true, body: 'Find Attached',
compressLog: true, recipientProviders: [[$class:
'CulpritsRecipientProvider']], subject: 'Broken Regression Tests',
to: 'dennis#dennis.ru'
}
}
}
However it does not work as I cannot reset the value of currentBuild.result to 'SUCCESS'. So I could all || true to the mvncommand, but that would mean that I can't email about the broken regression tests.
So I am wondering if anyone else out their has dealt with this in some clever way. Do I need to assign an exit code or something, and would that involve overriding the default shell parameters in Jenkins?
Any help much appreciated.

I think you would need to put a try/catch around the shell (so run it in a script{} block), and do your email in the catch. Then you can keep the build set to SUCCESS.

I actually solved this in a slightly different manner to #Rob's suggestion, but the key to it was understanding that what I wanted to do needed to use the script block with the returnStatus flag. I prefer this to a try-catch, as I am actually expecting (unfortunately) this to fail from time to time, and so would prefer to branch this below.
stage ('Serenity') {
steps {
script{
// For the Delivery CI build don't fail on regression failure
def bddPassed = ( sh ( returnStatus:true, script:'mvn clean verify -pl regression -DskipCuke=false') == 0 )
if( !bddPassed ){
echo 'There are regression suite failures.'
def mySubject = "Regression Test Failure: ${env.JOB_NAME} - Build# ${env.BUILD_NUMBER}"
def myBody = "Hi<br/>Please go to <a href='${env.BUILD_URL}Serenity'>the Serenity Report</a> to see more<br/>";
emailext attachLog: true,
mimeType: 'text/html',
body: myBody,
compressLog: true,
recipientProviders: [[$class: 'CulpritsRecipientProvider']],
subject: mySubject,
to: 'xxxxxxx'
}
publishHTML([allowMissing: true, alwaysLinkToLastBuild: true,
keepAll: true, reportDir: 'regression/target/site/serenity', reportFiles: 'index.html',
reportName: 'Serenity', reportTitles: ''])
}
}
}

Related

report folder does not exist error with htmlpublisher

I am trying to write a jenkins pipeline script for one of my playwright test. Below is one simple code which i have done so far.
pipeline {
agent any
stages {
stage('Run Playwright Test') {
steps {
runTest()
}
}
stage('Publish Report'){
steps {
script {
sh 'ls -lrta'
//print REPORT_FILES
}
publishHTML([
allowMissing: false,
alwaysLinkToLastBuild: true,
keepAll: true,
//reportDir: '.',
reportDir : "./TestReport2",
reportFiles: 'index.html',
reportName: "Html Reports",
reportTitles: 'Report title'
])
}
}
}
}
def runTest() {
node('MYNODE') {
docker.image('image details').inside('--user root'){
git branch: 'mybranchName',credentialsId: 'ID, url: 'url'
catchError() {
sh """
cd WebTests
npm install
npx playwright test --project=CHROME_TEST --grep #hello
"""
}
sh "cp -R WebTests/TestReport TestReport2"
sh 'cd TestReport2; ls -lrta'
}
When I use the above code, the test executed successfully however i am seeing an error while trying to publish the report.
Below is the error :
Specified HTML directory '/bld/workspace//TestReport2' does not exist.
observation: when i put a ls -ltr after the runTest code i could not see the TestReport2 folder even if it was copied successfully.
Another thing i tried is when i put the code to publish the HTML as part of the runTest() it worked fine and i am able to see the reports generated. Something is going on with the TestReport2 folder when the block of code for runTest() is completed.
Does anyone have an eye on what is the root cause. Any suggestion will be appreciated

How can I trigger Allure 2 jira-plugin without using Maven?

I am trying to get my test results visible in Jira using Allure 2.13.8. The test results are visible but are not displayed as links back to the report, so called backlinks.
Here is my situation.
When I generate an Allure report with maven using allure-maven:
mvn clean test allure:report
an executor.json is created with maven build info inside allure-results dir.
environment {
ALLURE_JIRA_LAUNCH_ISSUES = 'xxx-9'
ALLURE_JIRA_ENABLED = 'true'
ALLURE_JIRA_ENDPOINT = 'http://localhost:8080/rest/'
ALLURE_JIRA_USERNAME = 'xxx'
ALLURE_JIRA_PASSWORD = 'xxxx'
}
stage('Test Execution') {
steps {
withMaven(options: [artifactsPublisher(disabled: true)],
maven: 'MAVEN_HOME') {
sh "mvn clean test -Dtest=ApplyForCreditCardTestsDE,_FailedTests* -Denv=qa-de"
}
}
}
The above env variables trigger the jira-plugin within Allure to export test results and launch to Jira. This works, but this executor.json do not contain links needed to create these backlinks. Just some limited info such as project name, that's it.
When I generate an Allure report with Jenkins Allure plugin:
stage('Generate Test Report') {
steps {
script {
allure([
includeProperties: true,
jdk: '',
properties: [
[key: 'ALLURE_JIRA_LAUNCH_ISSUES',value: 'xxx-9'],
[key: 'ALLURE_JIRA_ENABLED',value: 'true'],
[key: 'ALLURE_JIRA_ENDPOINT',value: 'http://localhost:8080/rest/'],
[key: 'ALLURE_JIRA_USERNAME',value: 'xxx'],
[key: 'ALLURE_JIRA_PASSWORD',value: 'xxxx']
],
reportBuildPolicy: 'ALWAYS',
results: [[path: 'target/allure-results']]
])
}
}
}
an executor.json is created with jenkins build info inside allure-results dir.
{"buildName":"test #35","buildOrder":"35","reportName":"AllureReport","name":"Jenkins","buildUrl":"http://localhost:8085/job/test/35/","reportUrl":"http://localhost:8085/job/test/35/allure","type":"jenkins","url":"http://localhost:8085/"}
This is what I need to be pushed to Jira.
For some reason I cannot figure out how the jira-plugin can be triggered from this plugin? Or do I need to use Allure Command Line Interface to do this? I'm lost.
If you can help me out that would be much appreciated.

How to refer to the parameters in groovy pipeline script?

I merged this as vars/gitCheckout.goovy add this as library into the jenkins
def call(String branch = '*/master') {
checkout([$class: 'GitSCM',
branches: [[name: ${branch}]],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'SubmoduleOption',
disableSubmodules: false,
parentCredentials: false,
recursiveSubmodules: true,
reference: '',
trackingSubmodules: false]],
submoduleCfg: [],
userRemoteConfigs: [[url: 'https://my-server.com/some/project.git']]])
}
Calling this method as below from jenkins Pipeline Script:
#Library('jenkins-library#master') _
pipeline {
agent { label 'my-server' }
stages {
stage('Git Checkout') {
steps {
gitCheckout()
}
}
}
}
This fails with error java.lang.NoSuchMethodError: No such DSL method '$' found among steps [ArtifactoryGradleBuild, MavenDescriptorStep, ....
I tried $branch, params.branch, but it didn't work, the code otherwise works if I don't use parameter and hardcode the branch name. Also, whenever I make any update to this .groovy script, should I test it by merging and running it as jenkins job? is there any other way to test before merging the groovy script ?
Replace ${branch} in the 3rd line with just branch. You use $ with a variable name when you e.g. interpolate variables inside Groovy strings:
def value = "current branch is: ${branch}" // produces: current branch is */master
If you forgot to use $ in string interpolation, nothing would happen:
def value = "current branch is: branch" // produces: current branch is branch

Jenkinsfile conditions in Stage phase

should it possible to define several conditions in a Stage phase of Jenkins file??
Let me to explain my problem: I want to publishHtml in my Jenkins pipeline only if a condition is valid:
stage(Documentation)
{
agent any
steps
{
sh"./documents.sh " //This generate the documentation of several modules
if(firstModule) { //Is it possible to publishHTML only if documentation of this module was generated???
publishHTML (target: [
allowMissing: false,
alwaysLinkToLastBuild: false,
keepAll: true,
reportDir: "${env.WORKSPACE}/firstModule//html",
reportFiles: 'index.html',
reportName: "Fist Module Documentation"
])
}
if(secondModule) { //Is it possible to publishHTML only if documentation of this module was generated???
publishHTML (target: [
allowMissing: false,
alwaysLinkToLastBuild: false,
keepAll: true,
reportDir: "${env.WORKSPACE}/secondModule/html",
reportFiles: 'index.html',
reportName: "Second Modul Documentation"
])
}
}
I don't know if firstModule was built or not... this is what I would like to know.
I don't want to create different stages, but just one "Documentation" visible in the jenkins view.
Thank for your suggestion.
Prisco
it sounds like you want to publish documentation for firstModule only if "${env.WORKSPACE}/firstModule/html/index.html exists.
instead of:
if(firstModule) {
try:
script {
if (fileExists("${env.WORKSPACE}/firstModule/html/index.html")) {
# publishHTML and whatnot
}
}

how to switching off stack failure message?

While running a test suit, when something fails it also show the stack message like this
Failures:
1) Should validate labels
Message:
Failed: No element found using locator: By.cssSelector(".container h1")
Stack:
NoSuchElementError: No element found .........................
.........
......
....
can we switch off this stack output? I have tried
protractor conf.js --no-stackTrace
also updated conf.js file with settings
stackTrace: false,
// Options to be passed to Jasmine.
jasmineNodeOpts: {
defaultTimeoutInterval: 30000,
includeStackTrace: false,
}
but its always showing stack output, how to fix it?
If you are using and old protractor (<=1.4 I think), setting both isVerbose and includeStackTrace to false would work for you:
jasmineNodeOpts: {
isVerbose: false,
includeStackTrace: false
}
Unfortunately, nowadays isVerbose or includeStackTrace would not be recognized in jasmineNodeOpts (explanation here):
Similar to jasmine 1.3, you may include jasmineNodeOpts in the config
file. However, because we changed the runner from
"https://github.com/juliemr/minijasminenode" to
"https://github.com/jasmine/jasmine-npm", the options have changed
slightly. Notably options print and grep are new, but we will no
longer support options isVerbose and includeStackTrace (unless, of
course, "jasmine-npm" introduces these options).
See also:
isVerbose has no effect
An elegant solution that I found was located on the protractor github at https://github.com/bcaudan/jasmine-spec-reporter/blob/master/docs/protractor-configuration.md
You can modify your jasmineNodeOpts like so
jasmineNodeOpts: {
...
print: function() {}
}
And that took care of the problem for me
I was successfully able to disable the Stacktraces in my test suite using the following setup in my "conf.js" file:
...
framework: 'jasmine',
// Options to be passed to Jasmine-node.
jasmineNodeOpts: {
// If true, display spec names.
isVerbose : false,
// Use colors in the command line report.
showColors: true,
// If true, include stack traces in failures.
includeStackTrace : false,
// Default time to wait in ms before a test fails.
defaultTimeoutInterval: 60000,
// If true, print timestamps for failures
showTiming: true,
// Print failures in real time.
realtimeFailure: true
}
...
I found this GitHub issue (https://github.com/angular/protractor/issues/696) useful with this question. Setting both the "isVerbose" and "includeStackTrace" flags to 'false' worked for me.
includeStackTrace was removed in https://github.com/angular/protractor/commit/bf5b076cb8897d844c25baa91c263a12c61e3ab3
so the previous answers did not work for me.
The jasmine-spec-reporter has changed and no longer has a protractor-configuration.md file, so that advice no longer worked for me either.
However, despite the lack of a protractor-configuration.md file, I did find that jasmine-spec-reporter had the working solution for me.
I found that using the jasmine-spec-reporter in this way with Protractor 5.2.0 in my config file:
setup = function() {
var jasmineReporters = require('jasmine-reporters');
jasmine.getEnv().addReporter(new jasmineReporters.TerminalReporter({
verbosity: 3,
color: true,
showStack: false }));
}
exports.config = {
onPrepare: setup
};
The key was to change the stackTrace parameter to false in the TerminalReporter

Resources