Commit 7ce38487 authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

feat: initial template version

parent 4e5448c1
Loading
Loading
Loading
Loading

.prettierrc

0 → 100644
+4 −0
Original line number Diff line number Diff line
{
  "tabWidth": 2,
  "singleQuote": false
}
+2 −0
Original line number Diff line number Diff line
@@ -5,6 +5,8 @@ We try to make it easy, and all contributions, even the smaller ones, are more t
This includes bug reports, fixes, documentation, examples...
But first, read this page (including the small print at the end).

Contributions are available on https://gitlab.com/to-be-continuous/playwright.

## Legal

All original contributions to _to be continuous_ are licensed under the
+26 −11
Original line number Diff line number Diff line
# GitLab CI template for Playwright

This project implements a GitLab CI/CD template to run your automated tests with [Playwright](https://playwright.dev/docs/intro).

## Usage
@@ -19,23 +20,24 @@ This job starts [Playwright](https://playwright.dev/docs/intro) (functional) tes
It uses the following variable:

| Name                     | description                                                                                                                 | default value                               |
| --------------------- | ---------------------------------------- | ----------------- |
| `PLAYWRIGHT_IMAGE`       | The Docker image used to run Playwright. | `registry.hub.docker.com/playwright:latest` |
| `PLAYWRIGHT_PROJECT_DIR` | The Playwright project directory (containing test scripts) | `.` |
| `PLAYWRIGHT_EXTRA_ARGS`  | Playwright extra [run options](link-to-cli-options-ref) | _none_ |
| ------------------------ | --------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------- |
| `PLAYWRIGHT_IMAGE`       | The Docker image used to run Playwright.                                                                                    | `mcr.microsoft.com/playwright:latest` |
| `PLAYWRIGHT_PROJECT_DIR` | The Playwright root project directory (contains the `playwright.config.ts` file)                                            | `.`                                         |
| `PLAYWRIGHT_EXTRA_ARGS`  | Playwright extra [run options](https://playwright.dev/docs/test-cli)                                                        | _none_                                      |
| `REVIEW_ENABLED`         | Set to `true` to enable Playwright tests on review environments (dynamic environments instantiated on development branches) | _none_ (disabled)                           |

In addition to a textual report in the console, this job produces the following reports, kept for one day:

| Report                                                 | Format                                                                      | Usage                                                                                                 |
| -------------- | ---------------------------------------------------------------------------- | ----------------- |
| ------------------------------------------------------ | --------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
| `$PLAYWRIGHT_PROJECT_DIR/reports/playwright.xunit.xml` | [xUnit](https://github.com/jest-community/jest-junit#readme) test report(s) | [GitLab integration](https://docs.gitlab.com/ee/ci/yaml/artifacts_reports.html#artifactsreportsjunit) |

### base url auto evaluation

By default, the Playwright template tries to auto-evaluate its base url
By default, the Playwright template tries to auto-determine the [baseURL](https://playwright.dev/docs/api/class-testoptions#test-options-base-url) to use
(i.e. the variable pointing at server under test) by looking either for a `$environment_url` variable or for an
`environment_url.txt` file.
When found, the base url is passed as `$BASE_URL` environment variable and can be reused in your Playwright tests (see below).

Therefore if an upstream job in the pipeline deployed your code to a server and propagated the deployed server url,
either through a [dotenv](https://docs.gitlab.com/ee/ci/pipelines/job_artifacts.html#artifactsreportsdotenv) variable `$environment_url`
@@ -43,3 +45,16 @@ or through a basic `environment_url.txt` file, then the Playwright test will aut

:warning: all our deployment templates implement this design. Therefore even purely dynamic environments (such as review
environments) will automatically be propagated to your Playwright tests.

If you want to retrieve the `$BASE_URL` environment variable evaluated by the template, simply load it from your `playwright.config.ts` file as follows:

```ts
import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
  use: {
    /* retrieve from to-be-continuous or use default (dev) */
    baseURL: process.env.BASE_URL || 'http://localhost:3000',
  },
});
```
+3 −3
Original line number Diff line number Diff line
@@ -8,16 +8,16 @@
    {
      "name": "PLAYWRIGHT_IMAGE",
      "description": "The Docker image used to run Playwright",
      "default": "registry.hub.docker.com/playwright:latest"
      "default": "mcr.microsoft.com/playwright:latest"
    },
    {
      "name": "PLAYWRIGHT_PROJECT_DIR",
      "description": "The Playwright project directory (containing test scripts)",
      "description": "The Playwright root project directory (contains the `playwright.config.ts` file)",
      "default": "."
    },
    {
      "name": "PLAYWRIGHT_EXTRA_ARGS",
      "description": "Playwright extra [run options](link-to-cli-options-ref)",
      "description": "Playwright extra [run options](https://playwright.dev/docs/test-cli)",
      "advanced": true
    },
    {
+46 −41
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@
workflow:
  rules:
    # prevent branch pipeline when an MR is open (prefer MR pipeline)
    - if: '$CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS'
    - if: "$CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS"
      when: never
    - if: '$CI_COMMIT_MESSAGE =~ "/\[(ci skip|skip ci) on ([^],]*,)*tag(,[^],]*)*\]/" && $CI_COMMIT_TAG'
      when: never
@@ -41,18 +41,18 @@ workflow:
    - if: $CI_COMMIT_TAG
      when: never
    # on production or integration branch(es): auto & failing
    - if: '$CI_COMMIT_REF_NAME =~ $PROD_REF || $CI_COMMIT_REF_NAME =~ $INTEG_REF'
    - if: "$CI_COMMIT_REF_NAME =~ $PROD_REF || $CI_COMMIT_REF_NAME =~ $INTEG_REF"
    # disable if no review environment
    - if: '$REVIEW_ENABLED != "true"'
      when: never
    # on ADAPTIVE_PIPELINE_DISABLED: auto & failing
    - if: '$ADAPTIVE_PIPELINE_DISABLED == "true"'
    # early stage (dev branch, no MR): manual & non-failing
    - if: '$CI_MERGE_REQUEST_ID == null && $CI_OPEN_MERGE_REQUESTS == null'
    - if: "$CI_MERGE_REQUEST_ID == null && $CI_OPEN_MERGE_REQUESTS == null"
      when: manual
      allow_failure: true
    # Draft MR: auto & non-failing
    - if: '$CI_MERGE_REQUEST_TITLE =~ /^Draft:.*/'
    - if: "$CI_MERGE_REQUEST_TITLE =~ /^Draft:.*/"
      allow_failure: true
    # else (Ready MR): auto & failing
    - when: on_success
@@ -62,14 +62,14 @@ variables:
  TBC_TRACKING_IMAGE: "$CI_REGISTRY/to-be-continuous/tools/tracking:master"

  # Default Docker image (use a public image - can be overridden)
  PLAYWRIGHT_IMAGE: "registry.hub.docker.com/playwright:latest"
  # Directory where Playwright tests are implemented
  PLAYWRIGHT_ROOT_DIR: "."
  PLAYWRIGHT_IMAGE: "mcr.microsoft.com/playwright:latest"
  # Playwright root project dir
  PLAYWRIGHT_PROJECT_DIR: "."

  # default production ref name (pattern)
  PROD_REF: '/^(master|main)$/'
  PROD_REF: "/^(master|main)$/"
  # default integration ref name (pattern)
  INTEG_REF: '/^develop$/'
  INTEG_REF: "/^develop$/"

stages:
  - build
@@ -293,6 +293,21 @@ stages:
    esac
  }

  # maybe install NPM dependencies if package.json file found
  function maybe_install_deps() {
    if [[ -f "package-lock.json" ]]
    then
      log_info "\\e[33;1mpackage-lock.json\\e[0m file found: install dependencies..."
      npm ci --cache .npm --prefer-offline
    elif [[ -f "package.json" ]]
    then
      log_warn "\\e[33;1mpackage.json\\e[0m file found but no \\e[33;1mpackage-lock.json\\e[0m: use Playwright with default dependencies"
      log_warn "If your tests require additional dependencies, please create \\e[33;1mpackage-lock.json\\e[0m with 'npm install' and commit it"
    else
      log_info "No \\e[33;1mpackage.json\\e[0m file found: use Playwright with default dependencies"
    fi
  }

  function eval_all_secrets() {
    encoded_vars=$(env | grep -v '^scoped__' | awk -F '=' '/^[a-zA-Z0-9_]*=@(b64|hex|url)@/ {print $1}')
    for var in $encoded_vars
@@ -301,29 +316,19 @@ stages:
    done
  }

  function exec_hook() {
    if [[ ! -x "$1" ]] && ! chmod +x "$1"
    then
      log_warn "... could not make \\e[33;1m${1}\\e[0m executable: please do it (chmod +x)"
      # fallback technique
      sh "$1"
    else
      "$1"
    fi
  }
  # retrieve server url to test from upstream artifacts ($environment_url variable or 'environment_url.txt' file)
  function eval_env_url() {
    # shellcheck disable=SC2154
    if [[ -n "$environment_url" ]]
    then
      PLAYWRIGHT_BASE_URL="$environment_url"
      export PLAYWRIGHT_BASE_URL
      log_info "Upstream \$environment_url variable set: use base url \\e[33;1m$PLAYWRIGHT_BASE_URL\\e[0m"
      BASE_URL="$environment_url"
      export BASE_URL
      log_info "Upstream \$environment_url variable set: use base url \\e[33;1m$BASE_URL\\e[0m"
    elif [[ -f environment_url.txt ]]
    then
      PLAYWRIGHT_BASE_URL=$(cat environment_url.txt)
      export PLAYWRIGHT_BASE_URL
      log_info "Upstream environment_url.txt file found: use base url \\e[33;1m$PLAYWRIGHT_BASE_URL\\e[0m"
      BASE_URL=$(cat environment_url.txt)
      export BASE_URL
      log_info "Upstream environment_url.txt file found: use base url \\e[33;1m$BASE_URL\\e[0m"
    else
      log_info "No upstream environment url found: leave default"
    fi
@@ -342,32 +347,32 @@ playwright:
    - name: "$TBC_TRACKING_IMAGE"
      command: ["--service", "playwright", "1.0.0"]
  stage: acceptance
  # TODO (if necessary): define cache policy here
  variables:
    # NPM cache
    npm_config_cache: "$CI_PROJECT_DIR/$PLAYWRIGHT_PROJECT_DIR/.npm"
  cache:
    # cache shall be per branch per template
    key: "${CI_COMMIT_REF_SLUG}-playwright"
    # cache per branch per template
    key: "$CI_COMMIT_REF_SLUG-playwright"
    paths:
      - .cache/
      - $PLAYWRIGHT_PROJECT_DIR/.npm/
  before_script:
    - *playwright-scripts
    - install_ca_certs "${CUSTOM_CA_CERTS:-$DEFAULT_CA_CERTS}"
    - eval_env_url
    - cd "$PLAYWRIGHT_ROOT_DIR"
    # TODO (if necessary): do setup stuff here
    - cd "$PLAYWRIGHT_PROJECT_DIR"
    - maybe_install_deps
  script:
    # TODO: run playwright tests
    # TODO (if possible): $TRACE set enables debug logs on the tool
    - mkdir -p -m 777 reports
    - playwright run ${TRACE+--verbose} --env BASE_URL=$PLAYWRIGHT_BASE_URL --junit --output=reports/playwright.xunit.xml $PLAYWRIGHT_EXTRA_ARGS
    - PLAYWRIGHT_JUNIT_OUTPUT_NAME=reports/playwright.xunit.xml npx playwright test ${TRACE+--debug} --reporter=junit $PLAYWRIGHT_EXTRA_ARGS
  artifacts:
    name: "$CI_JOB_NAME artifacts from $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG"
    when: always
    paths:
      - $PLAYWRIGHT_ROOT_DIR/reports/playwright.*
      - $PLAYWRIGHT_PROJECT_DIR/reports/playwright.*
    reports:
      # TODO (if possible): Acceptance tests use JUnit format and GitLab integration (see: https://docs.gitlab.com/ee/ci/yaml/#artifactsreports)
      # use JUnit format and GitLab integration (see: https://docs.gitlab.com/ee/ci/yaml/#artifactsreports)
      junit:
        - $PLAYWRIGHT_ROOT_DIR/reports/playwright.xunit.xml
        - $PLAYWRIGHT_PROJECT_DIR/reports/playwright.xunit.xml
    expire_in: 1 day
  rules:
    - !reference [.acceptance-policy, rules]