|
| 1 | +import org.jenkinsci.plugins.pipeline.modeldefinition.Utils |
| 2 | + |
| 3 | +node { |
| 4 | + try { |
| 5 | + properties([ |
| 6 | + parameters([ |
| 7 | + credentials(credentialType: 'com.browserstack.automate.ci.jenkins.BrowserStackCredentials', defaultValue:'', description: 'Select your BrowserStack Username', name: 'BROWSERSTACK_USERNAME', required: true), |
| 8 | + choice( |
| 9 | + choices: [ |
| 10 | + 'bstack-single', |
| 11 | + 'bstack-parallel', |
| 12 | + 'bstack-parallel-browsers', |
| 13 | + 'bstack-local', |
| 14 | + 'bstack-local-parallel', |
| 15 | + 'bstack-local-parallel-browsers' |
| 16 | + ], |
| 17 | + description: 'Select the test you would like to run', |
| 18 | + name: 'TEST_TYPE' |
| 19 | + ), |
| 20 | + string(defaultValue: '5', description: 'Enter the number of parallels to run', name: 'PARALLELISM', trim: true) |
| 21 | + ]) |
| 22 | + ]) |
| 23 | + |
| 24 | + stage('Pull code from Github') { |
| 25 | + dir('test') { |
| 26 | + git branch: 'main', changelog: false, poll: false, url: 'https://github.com/browserstack/browserstack-examples-cucumber-testng' |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + stage('Start Local') { |
| 31 | + if ("${params.TEST_TYPE}".contains('local')) { |
| 32 | + dir('app') { |
| 33 | + git branch: 'main', changelog: false, poll: false, url: 'https://github.com/browserstack/browserstack-demo-app' |
| 34 | + sh label: '', returnStatus: true, script: '''#!/bin/bash -l |
| 35 | + cd browserstack - demo - app |
| 36 | + npm install |
| 37 | + npm run build |
| 38 | + npm start &''' |
| 39 | + } |
| 40 | + } else { |
| 41 | + Utils.markStageSkippedForConditional('Start Local') |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + stage('Run Test') { |
| 46 | + browserstack(credentialsId: "${params.BROWSERSTACK_USERNAME}") { |
| 47 | + def user = "${env.BROWSERSTACK_USERNAME}" |
| 48 | + if (user.contains('-')) { |
| 49 | + user = user.substring(0, user.lastIndexOf('-')) |
| 50 | + } |
| 51 | + withEnv(['BROWSERSTACK_USERNAME=' + user]) { |
| 52 | + sh label: '', returnStatus: true, script:'''#!/bin/bash -l |
| 53 | + cd test |
| 54 | + mvn -Ddataproviderthreadcount=${PARALLELISM} clean test -P ${TEST_TYPE} ''' |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + stage('Generate Report'){ |
| 59 | + if("${params.TEST_TYPE}".contains('bstack')){ |
| 60 | + browserStackReportPublisher 'automate' |
| 61 | + } |
| 62 | + allure([ |
| 63 | + includeProperties: false, |
| 64 | + jdk: '', |
| 65 | + reportBuildPolicy: 'ALWAYS', |
| 66 | + results: [[path: 'target/allure-results'], [path: '**/test/allure-results']] |
| 67 | + ]) |
| 68 | + } |
| 69 | + stage('Generate cucumber report') { |
| 70 | + cucumber buildStatus: 'UNSTABLE', |
| 71 | + reportTitle: 'My report', |
| 72 | + fileIncludePattern: "**/cucumber.json", |
| 73 | + trendsLimit: 10 |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + }catch (e) { |
| 79 | + currentBuild.result = 'FAILURE' |
| 80 | + echo e |
| 81 | + throw e |
| 82 | + } finally { |
| 83 | + notifySlack(currentBuild.result) |
| 84 | + } |
| 85 | +} |
| 86 | +def notifySlack(String buildStatus = 'STARTED') { |
| 87 | + // Build status of null means success. |
| 88 | + buildStatus = buildStatus ?: 'SUCCESS' |
| 89 | + |
| 90 | + def color |
| 91 | + |
| 92 | + if (buildStatus == 'STARTED') { |
| 93 | + color = '#D4DADF' |
| 94 | + } else if (buildStatus == 'SUCCESS') { |
| 95 | + color = '#BDFFC3' |
| 96 | + } else if (buildStatus == 'UNSTABLE') { |
| 97 | + color = '#FFFE89' |
| 98 | + } else { |
| 99 | + color = '#FF9FA1' |
| 100 | + } |
| 101 | + |
| 102 | + def msg = "${buildStatus}: `${env.JOB_NAME}` #${env.BUILD_NUMBER}:\n${env.BUILD_URL}" |
| 103 | + if (buildStatus != 'STARTED' && buildStatus != 'SUCCESS') { |
| 104 | + // slackSend(color: color, message: msg) |
| 105 | + } |
| 106 | +} |
0 commit comments