Files
pw/config/jobs/scripts/qa-selenium-tests.groovy
T

141 lines
5.2 KiB
Groovy

pipeline {
agent { label 'maven' }
options {
timestamps()
ansiColor('xterm')
}
stages {
stage('Prepare Metadata') {
steps {
script {
wrap([$class: 'BuildUser']) {
env.RUN_TRIGGER_USER = env.BUILD_USER_ID ?: env.BUILD_USER ?: 'system'
env.RUN_TRIGGER_NAME = env.BUILD_USER ?: env.BUILD_USER_ID ?: 'system'
}
currentBuild.displayName = "#${env.BUILD_NUMBER} ${params.BROWSER}/${params.HEADLESS}"
currentBuild.description =
"by=${env.RUN_TRIGGER_NAME}; repo=${params.QA_REPO_URL}; ref=${params.QA_REPO_REF}; mode=${params.EXECUTION_MODE}"
}
}
}
stage('Run Selenium Tests In Docker') {
steps {
sh '''
set -eux
rm -rf ./sources
git clone "${QA_REPO_URL}" ./sources
git -C ./sources checkout "${QA_REPO_REF}"
GIT_SHA="$(git -C ./sources rev-parse --short HEAD)"
if ! grep -q "<artifactId>allure-junit5</artifactId>" ./sources/pom.xml; then
awk -v ver="${ALLURE_ADAPTER_VERSION}" '
index($0, "</dependencies>") && !done {
print " <dependency>"
print " <groupId>io.qameta.allure</groupId>"
print " <artifactId>allure-junit5</artifactId>"
print " <version>" ver "</version>"
print " <scope>test</scope>"
print " </dependency>"
done=1
}
{ print }
' ./sources/pom.xml > ./sources/pom.xml.tmp
mv ./sources/pom.xml.tmp ./sources/pom.xml
fi
rm -rf ./artifacts
mkdir -p ./artifacts
{
echo "job=${JOB_NAME}"
echo "build=${BUILD_NUMBER}"
echo "trigger_user=${RUN_TRIGGER_USER}"
echo "trigger_name=${RUN_TRIGGER_NAME}"
echo "qa_repo_url=${QA_REPO_URL}"
echo "qa_repo_ref=${QA_REPO_REF}"
echo "qa_repo_sha=${GIT_SHA}"
echo "browser=${BROWSER}"
echo "headless=${HEADLESS}"
echo "execution_mode=${EXECUTION_MODE}"
echo "selenoid_url=${SELENOID_URL}"
echo "base_url=${BASE_URL}"
echo "timestamp_utc=$(date -u +%Y-%m-%dT%H:%M:%SZ)"
} > ./artifacts/run-info.txt
EXTRA_ARGS=""
if [ "${BROWSER}" = "chrome" ]; then
EXTRA_ARGS="-Dchrome.binary=/usr/bin/chromium"
fi
CID="$(docker create \
--add-host=host.docker.internal:host-gateway \
localhost:5005/otus/test-selenium:1.0.0 \
bash -lc "set -e; cd /workspace; mvn -Dallure.version=${ALLURE_ADAPTER_VERSION} -Dexecution.mode=${EXECUTION_MODE} -Dbrowser=${BROWSER} -Dbrowser.version= -Dselenoid.url=${SELENOID_URL} -Dbase.url=${BASE_URL} -Dselenide.headless=${HEADLESS} -Dallure.results.directory=target/allure-results ${EXTRA_ARGS} test")"
cleanup_container() {
docker rm -f "${CID}" >/dev/null 2>&1 || true
}
trap cleanup_container EXIT INT TERM
tar -C ./sources -cf - . | docker cp - "${CID}:/workspace"
set +e
docker start -a "${CID}"
TEST_RC=$?
docker cp "${CID}:/workspace/target" "./artifacts" || true
mkdir -p ./artifacts/target/allure-results
{
echo "job=${JOB_NAME}"
echo "build=${BUILD_NUMBER}"
echo "trigger.user=${RUN_TRIGGER_USER}"
echo "trigger.name=${RUN_TRIGGER_NAME}"
echo "repo.url=${QA_REPO_URL}"
echo "repo.ref=${QA_REPO_REF}"
echo "repo.sha=${GIT_SHA}"
echo "browser=${BROWSER}"
echo "headless=${HEADLESS}"
echo "execution.mode=${EXECUTION_MODE}"
echo "base.url=${BASE_URL}"
} > ./artifacts/target/allure-results/environment.properties
cat > ./artifacts/target/allure-results/executor.json <<EOF
{
"name": "Jenkins",
"type": "jenkins",
"url": "${JENKINS_URL}",
"buildName": "${JOB_NAME} #${BUILD_NUMBER}",
"buildUrl": "${BUILD_URL}",
"reportUrl": "${BUILD_URL}allure",
"buildOrder": ${BUILD_NUMBER}
}
EOF
cat > ./artifacts/target/allure-results/categories.json <<'EOF'
[
{
"name": "Infrastructure issues",
"matchedStatuses": ["broken"],
"messageRegex": ".*(Connection refused|No route to host|timed out|timeout).*"
},
{
"name": "UI locator/assertion issues",
"matchedStatuses": ["failed", "broken"],
"messageRegex": ".*(Element not found|NoSuchElement|AssertionError).*"
}
]
EOF
trap - EXIT INT TERM
docker rm -f "${CID}" || true
exit "${TEST_RC}"
'''
}
}
}
post {
always {
script {
try {
allure commandline: 'allure', includeProperties: false, jdk: '', results: [[path: 'artifacts/target/allure-results']]
} catch (Exception ex) {
echo "Allure publisher unavailable: ${ex.message}"
}
}
junit allowEmptyResults: true, testResults: 'artifacts/target/surefire-reports/*.xml'
archiveArtifacts allowEmptyArchive: true, artifacts: 'artifacts/run-info.txt,artifacts/target/**'
}
}
}