MAJOR=4
MINOR=25
DEVNAME=tty25
     ˆ¾�þB gÁ¬€ÏÃƒh¹ÂÀ¿      MAJOR=13
MINOR=32
DEVNAME=input/mouse0
     ˆ¾�þB gÁ 7H ?Ãƒh¹ÂÀ¿      MAJOR=4
MINOR=61
DEVNAME=tty61
  C   	ˆl–Ãa¾”ªi?u8 ¸Û71hß�þYdm,ã�f1�¢t7üÿÄ„"?Ã_ŠI|¥ŠGgYâ¯ÂÁ 3¤    	# How to Test with RSpec and Capybara

We assume you are using our recommended [Devcontainer Setup](../development_environment/devcontainer-setup.md)
and are starting from `develop` branch.

Switching between running tests and doing development work (by running `dev` from `/bin`)
should be effortless without any issues.

RSpec is the recommended way of writing back end tests for Zammad,
and in combination with Capybara also Playwright-based end-to-end tests.

This page explains some Zammad specific extensions that make testing easier.

## Running

To run tests you need to first ensure the `test` database and all assets are in the expected state:

```sh
RAILS_ENV=test bundle exec rake db:drop db:create zammad:ci:test:prepare
RAILS_ENV=test rails assets:precompile
```

Now, running a single test can be done via the following command:

```sh
bundle exec rspec spec/system/ticket/zoom_spec.rb
```

Note that it's also possible to run a specific test case by including the line number in the command:

```sh
bundle exec rspec spec/system/ticket/zoom_spec.rb:1072
```

Capybara end-to-end tests run in Chromium by default. To use Firefox instead, set the `TEST_BROWSER` environment
variable:

```sh
TEST_BROWSER=firefox bundle exec rspec spec/system/ticket/zoom_spec.rb
```

Also running failed tests only is possible with the option `--only-failures`.

```sh
bundle exec rspec --only-failures spec/system/ticket/zoom_spec.rb
```

The browsers are driven by Playwright, which needs a one-time browser download and records a trace of every example to
debug a failing browser test. See [How to debug system tests with Playwright](how-to-debug-with-playwright.md).

To check whether a flaky system test is stable, loop it in CI, see
[How to run filtered CI pipelines](how-to-run-filtered-ci-pipelines.md#loop-a-system-test).

## Ports Used by a Test Run

A run claims two ports, both taken from the environment. Their defaults sit
next to - never on - the ports of the development stack, so a suite and the app
of the same checkout can run at the same time:

| Server | Variable | Default | Dev stack |
| --- | --- | --- | --- |
| Capybara / Puma | `CAPYBARA_PORT` | `3001` | `3000` |
| WebSocket server | `WS_PORT` | `6043` | `6042` |

Set both to run two suites side by side:

```sh
CAPYBARA_PORT=3101 WS_PORT=6143 bundle exec rspec spec/system/ticket/zoom_spec.rb
```

Without that, the second run fails on the port the first one holds:

```text
Couldn't start WebSocket server on port 6043. Maybe another websocket server
process is already running? Set WS_PORT to a free port to run this suite
alongside it.
```

Note that the two runs still share the `test` database, the Redis keys and the
Elasticsearch indices, so give the second one its own `DATABASE_URL`,
`REDIS_URL` and `ES_INDEX` as well.

## Default RSpec Environment

RSpec will populate the database at startup. These users are available in any test right away:

- admin: `admin@example.com`
- agent: `agent1@example.com`
- client: `nicole.braun@zammad.org`

## Re-initialize database

To reset an existing development database (without running `auto_wizard`)

- [Run db:init again](../development_environment/development-workflow.md#database-tasks)

## RSpec Meta Attributes

### `authenticated_as`

Manages logging in.

Example usage: `Rspec.describe :example, authenticated_as: true`

Takes boolean, symbol or lambda. Symbol is a method name to be executed. Works for RSpec's `let` too! Lambda or
referenced method are expected to return `User` object or a boolean.

`true` is the default value. It logs in with `admin@example.com` admin user. If a `User` is returned, it attempts to
login with it. `false` causes to skip logging in altogether.

Lambda or referenced method is evaluated before web page is loaded. Thus this is a good place t