【CircleCI】CircleCI 2.0でE2Eテスト(Kotlin・Junit・Selenium・Selenide)を実行してみた

CircleCiでSelenideを実行してみた

f:id:TheManwiththeYellowHat:20190718122924p:plain

ソースはこちら
github.com

環境・言語

  • OS
  • language
    • kotlin 1.3.31

前提条件

  • Homebrewがインストールされている(Homebrew 2.1.6)
  • gradleがインストールされている(Gradle 5.5)
  • javaがインストールされている(openjdk 11.0.2 2018-10-16)
  • githubにログインできる(登録している)
  • githubで空のリポジトリを作成している

プロジェクト作成(gradle)

mkdir hoge
cd hoge
gradle init --type kotlin-application

build.gradle(上記gradle initにて生成されたファイルを修正)

plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.3.31'
    id 'application'
}

apply plugin: 'kotlin'
apply plugin: 'java'
apply plugin: 'idea'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
    implementation 'com.codeborne:selenide:5.2.4'
    testImplementation 'org.jetbrains.kotlin:kotlin-test'
    testImplementation 'org.jetbrains.kotlin:kotlin-test-junit'
}

テストクラス(/src/test/kotlin/e2e/test/MainTest.kt)

import kotlin.test.Test
import kotlin.test.assertTrue

class MainTest {
    @Test
    fun mainTest() {
        // ChromeDriver を headless モードで利用
        Configuration.browser = WebDriverRunner.CHROME
        Configuration.headless = true
        Configuration.reportsFolder = "test-result/reports"
        Configuration.browserSize = "1024x768"


        // Googleトップページ
        // "selenide"を検索
        open("https://www.google.co.jp/")
        `$`("input[type=text]").`val`("selenide").pressEnter()

        // 検索ページ
        // Selenideの公式ページをクリック
        `$`(byText("Selenide: concise UI tests in Java")).click()

        // Selenide公式ページ
        // 「What is Selenide?」という文言があることを確認
        `$`("body").shouldHave(text("What is Selenide?"))
    }
}

githubリポジトリにpush

git init
git remote add origin {github-target-repository}
git commit -m 'first commit'
git push origin master

Circle Ci に登録

circleci.com

Circle Ci config.yml作成

上記のプロジェクト直下で

touch .circleci/config.yml
  • .circleci/config.yml
version: 2
jobs:
  build:
    docker:
      - image: circleci/openjdk:11.0.2-jdk-browsers
    working_directory: ~/repo
    steps:
      - checkout
      - restore_cache:
          keys:
            # when lock file changes, use increasingly general patterns to restore cache
            - gradle-repo-v1-{{ .Branch }}-{{ checksum "build.gradle" }}
            - gradle-repo-v1-{{ .Branch }}-
            - gradle-repo-v1-
      - run:
          name: Library check
          command: |
            gradle dependencies
      - save_cache:
          paths:
            - ~/.gradle
          key: gradle-repo-v1-{{ .Branch }}-{{ checksum "build.gradle" }}
      - run:
          name: Run Tests
          command: gradle test
      - run:
          name: Save test results
          command: |
            mkdir -p ~/test-results/junit/
            find . -type f -regex ".*/build/test-results/.*xml" -exec cp -p {} ~/test-results/junit/ \;
            mkdir -p ~/test-results/reports/
            find . -type d -regex ".*/build/reports" -exec cp -rp {} ~/test-results/ \;
          when: always
      - store_test_results:
          path: ~/test-results
      - store_artifacts:
          path: ~/test-results/junit
      - store_artifacts:
          path: ~/test-results/reports
workflows:
  version: 2
  # push時のテスト実行用(workflowを使用すると、このworkflow(triggersの設定がなくjobを指定した)がないとpush時にjobが実行されない)
  normal_workflow:
    jobs:
      - build
  # スケジュールテスト実行用
  schedule_workflow:
    triggers:
      - schedule:
          cron: "1 3 1 * *" # UTCで記述。-9hours
          filters:
            branches:
              only:
                - master
    jobs:
      - build

CircleCI CLIインストール

brew install circleci

下記を参照
circleci.com

  • .circleci/config.ymlのチェック
circleci config validate

Config file at .circleci/config.yml is valid.
が出力されればOK。それ以外はエラーメッセージに書かれた内容に沿って対応。

  • ローカルで.circleci/config.ymlのテスト
circleci local execute --job build

Circle Ci にプロジェクト追加

f:id:TheManwiththeYellowHat:20190718114507p:plain

f:id:TheManwiththeYellowHat:20190718114530p:plain

normal_workflowの設定があることにより、上記でjobが実行される

結果の確認

f:id:TheManwiththeYellowHat:20190718115405p:plain

f:id:TheManwiththeYellowHat:20190718115409p:plain

f:id:TheManwiththeYellowHat:20190718115452p:plain

f:id:TheManwiththeYellowHat:20190718115503p:plain

まとめ

circleciのdocker imageが良いですね。
circleci/openjdk:11.0.2-jdk-browsers

無料枠で十分使えるので、楽だしこれから機会があればどんどん使いたいなぁ
250分/週かな?...
Usage Plan - CircleCI

ただし、下記のようなIPの制限があるので、それがネックになる場合は他が良さそうです。
IP アドレスのホワイトリスト登録 – CircleCI Japanese Support Center

参考

CircleCIのドキュメントへようこそ - CircleCI Gradle User Manual