概要
Seleniumのテストコードを複数作成した場合、どうやってJenkinsで定期実行するかを調べたのでメモしておく。
ファイル構成
下記のようなファイルの構成で作成する。
ファイル一覧
├ groovy
│ ├ script
│ │ └ slack.groovy
│ └ Jenkinsfile
└ python
├ test001-hoge.py
└ test002-fuga.py
実行環境
DockerのJenkins(公式)コンテナにpython3を追加したものを利用することにします。
Dockerfile
FROM jenkins
USER root
# system update
RUN apt-get update
# locale
RUN apt-get -y install locales && \
localedef -f UTF-8 -i ja_JP ja_JP.UTF-8
ENV LANG ja_JP.UTF-8
ENV LANGUAGE ja_JP:ja
ENV LC_ALL ja_JP.UTF-8
# timezone (Asia/Tokyo)
ENV TZ JST-9
# etc
ENV TERM xterm
# add tools to work.
RUN apt-get install -y vim less
# python3 install
RUN apt-get install -y python3 python3-pip
RUN apt-get -f install
RUN apt-get install -y libnss3-tools libnss3 libnss3-dbg libnss3-dev
RUN apt-get install -y libgconf2-4 libxss1
RUN apt-get install -y fonts-liberation libappindicator1 xdg-utils
# selenium install
RUN pip3 install selenium
Slack通知メソッド作成
下記の投稿に作り方がありますのでそちらを参照ください。
Jenkinsfile作成
Jenkinsfile
# !/usr/bin/env groovy
pipeline {
agent any
environment {
TEST_ENV='testing'
}
options {
timeout(time: 1, unit: 'HOURS')
}
stages {
stage('Seleniumテスト') {
parallel {
stage('シナリオ-001') {
steps {
script {
retry(5) {
sh "python3 ${pwd()}/python/test001-hoge.py"
}
}
}
}
stage('シナリオ-002') {
steps {
script {
retry(5) {
sh "python3 ${pwd()}/python/test002-fuga.py"
}
}
}
}
}
}
}
post {
success {
echo 'All Test OK'
}
failure {
script{
slack = load "${pwd()}/groovy/script/slack.groovy"
slack.post('danger', 'FAIL')
}
}
}
}
参考サイト(pipeline plugin)
- Jenkins Pipeline
- Jenkins Pipeline Syntax
- Post-steps in Jenkins declarative pipeline
- Jenkinsの設定を最小限でJenkinsfile(Pipeline)を使う
- 使ってみよう!Jenkins 「Pipeline」
- jenkinsのpipeline入門(jenkinsfile)
- Declarative PipelineでJenkinsfileを書いてみた
- Jenkins PipelineでYamlを設定ファイルとして使う方法