Skip to main content

Cypress Component

This component runs Cypress browser tests in a pinned container image. It can verify a review environment or an application supplied as a GitLab CI service.

Usage​

Add the component to .gitlab-ci.yml and point path at the directory that contains cypress.config.* and the Cypress tests:

include:
- component: $CI_SERVER_FQDN/xrow-public/ci-tools/common@stable
- component: $CI_SERVER_FQDN/xrow-public/ci-tools/cypress@stable
inputs:
path: tests/e2e
browser: firefox
review-domain: $CI_ENVIRONMENT_SLUG.review.example.com

The test directory is mounted at /e2e. Its npm dependencies are installed before Cypress starts. Use needs when the tests require artifacts or a job that prepares their target.

Test a public URL​

Set review-domain to the public host and read CI_ENVIRONMENT_URL in the Cypress configuration. The following example checks the public GitLab project API.

.gitlab-ci.yml
include:
- component: $CI_SERVER_FQDN/xrow-public/ci-tools/common@stable
- component: $CI_SERVER_FQDN/xrow-public/ci-tools/cypress@stable
inputs:
path: tests/e2e
browser: electron
environment: gitlab-com
review-domain: gitlab.com
tests/e2e/cypress.config.mjs
export default {
e2e: {
baseUrl: process.env.CI_ENVIRONMENT_URL,
supportFile: false,
},
video: false,
};
tests/e2e/cypress/e2e/gitlab.cy.mjs
describe('GitLab.com', () => {
it('serves the GitLab project', () => {
cy.request('/api/v4/projects/278964').then((response) => {
expect(response.status).to.equal(200);
expect(response.body.path_with_namespace).to.equal('gitlab-org/gitlab');
});
});
});

Add a minimal package.json in tests/e2e so the component can run npm install before Cypress starts:

tests/e2e/package.json
{
"name": "gitlab-cypress-smoke-test",
"private": true
}

The job succeeds after Cypress receives the public GitLab project metadata.

Service under test​

Use services to run an application beside Cypress. The Cypress browser runs in a nested container, so configure its base URL with host.containers.internal and the port exposed by the service. GitLab services in the Kubernetes executor share the job pod network.

include:
- component: $CI_SERVER_FQDN/xrow-public/ci-tools/cypress@stable
inputs:
path: tests/e2e
browser: electron
services:
- name: $CI_REGISTRY_IMAGE/test-server:$CI_COMMIT_SHA
alias: test-server
needs:
- job: build:test-server
artifacts: false
tests/e2e/cypress.config.js
export default {
e2e: {
baseUrl: 'http://host.containers.internal',
},
};

Use proxy and no-proxy when the test environment requires an HTTP proxy. Set record-key to enable Cypress Cloud recording, and use options for additional cypress run arguments.

Inputs​

NameDescriptionDefaultType
------------
browserfirefoxstring
environmentintstring
environment-identifierEnvironment identifier, typically in the format /review-$CI_PIPELINE_IID-$CI_COMMIT_SHORT_SHAstring
imageCypress image to use for running the Cypress tests.registry.gitlab.com/xrow-public/library/cypress/included:15.21.1string
namereview:cypressstring
needsJobs that the start job depends on.[]array
no-proxyComma-separated list of hosts to exclude from proxying.string
options--headlessstring
pathPath in the repository to the Cypress tests.tests/e2estring
proxyProxy to use for the tests.string
record-keystring
review-domainDomain to be tested (create it with the review component).$CI_ENVIRONMENT_SLUG.review.xrow.netstring
rulesThe rules to determine when to run the job.[{"if":"$CI_PIPELINE_SOURCE == \"schedule\"","when":"never"},{"if":"$CI_COMMIT_TAG =~ $CI_PATTERN_SEMVER","when":"never"},{"if":"$CI_PIPELINE_SOURCE == \"merge_request_event\"","when":"on_success"},{"if":"$CI_COMMIT_REF_SLUG =~ $CI_DEFAULT_BRANCH","when":"on_success"},{"when":"never"}]array
servicesAdditional services to run alongside the tests. Like a proxy.[]array
stageteststring
tagstring
test-contextstring