# Screenshots and evidence

> Take screenshots, keep the evidence a failed run needs, log in with SSH, and remove everything afterwards.

## Screenshots

```sh
endia desktop screenshot -o evidence/desktop.png "$m"              # the whole screen
endia desktop screenshot --pid "$pid" -o evidence/window.png "$m"  # that process's largest visible window
endia desktop screenshot "$m" > shot.png                           # PNG on standard output
```

The screenshot is returned to your test; nothing is kept on the PC or in the machine. A machine's screen is 1024 by 768 unless its service asks for another size in the Compose file:

```yaml title="compose.yaml"
services:
  client:
    image: lab/tools:1
    x-eastindia:
      screen: 1920x1080
```

The size takes effect when the machine is created, so change it and run `compose up` again to recreate the machines.

## Collecting evidence and logs

Keep everything a failed run needs to be understood without rerunning it. The sample keeps it in `evidence/<run>`:

| Evidence | Command |
|---|---|
| Files the application wrote | `endia cp "$m:C:/ProgramData/MyApp" evidence/` |
| Windows event log entries | `endia exec "$m" powershell -NoProfile -Command "Get-WinEvent -FilterHashtable @{LogName='Application'; ProviderName='MyApp'} \| Format-List TimeCreated,Message" > evidence/events.txt` |
| The last 200 entries of the System event log, for later reading | `endia exec "$m" powershell -NoProfile -Command "Get-WinEvent -LogName System -MaxEvents 200 \| Export-Csv C:\Windows\Temp\system.csv -NoTypeInformation"`, then `endia cp "$m:C:/Windows/Temp/system.csv" evidence/` |
| The processes running | `endia process ls "$m"` (process ID, parent, session, user, start time, name) |
| The visible windows | `endia desktop windows "$m"` |
| A window's elements and values | `endia desktop inspect "$m" "$pid"` |
| Screenshots | `endia desktop screenshot -o ...` |
| Processor, memory, disk and network use | `endia stats --no-stream "$m"` |
| The machine's settings, addresses and ports | `endia inspect "$m"` |
| A main process's output | `endia compose -p "$run" logs <service>`, for a service with a `command` |

**Collect the evidence before `compose down`**

Removing a machine deletes its disk.

## SSH access

Every machine runs an OpenSSH server on port 22, because the root image installs one. To use it from your test, publish the port and give `testuser` your public key:

```yaml title="compose.yaml"
services:
  app:
    image: lab/tools:1
    ports:
      - "12222:22"
```

```sh
ssh-keygen -q -t ed25519 -N "" -f ./id && cp ./id.pub ./authorized_keys
endia exec "$m" cmd /c "mkdir C:\Users\testuser\.ssh"
endia cp ./authorized_keys "$m:C:/Users/testuser/.ssh/"
endia exec "$m" icacls C:\Users\testuser\.ssh\authorized_keys /inheritance:r /grant "testuser:F" /grant "SYSTEM:F"
ssh -i ./id -p 12222 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null testuser@host.docker.internal whoami
```

OpenSSH refuses a key file that other accounts may change, hence the `icacls` line. A new machine has new host keys, so the test should not record them. Most tests need no SSH, because `exec` and `cp` run programs and copy files without keys.

## Cleaning up

```sh
endia compose -p "$run" down -v
endia ps -a        # nothing of the run is left
```

`down` shuts each machine down, removes it with its disk, and removes the fleet's networks; `-v` also removes its named volumes. Images stay, for the next run. `down` gives Windows up to `stopTimeoutSeconds` (120 seconds by default) to shut down, then turns the machine off; `-t <seconds>` changes the wait for one command.

- `endia compose stop` and `start` stop and restart machines while keeping their disks, for a test that needs a restart or a cold boot. `endia restart <machine>` restarts Windows and returns once the machine is ready again, with `testuser` signed in, so the next command needs no wait.
- `endia rmi sample/server:1` removes an image you no longer need; `endia system prune` removes stopped machines and untagged images.
- A run killed before its trap ran leaves its fleet behind. `endia compose ls` lists fleets, and `endia compose -p <name> down -v` removes one.

## Sample project

The [Windows app testing sample](https://github.com/teaToolkit/EastIndia/tree/main/samples/windows-app-testing) holds a Windows service, a desktop program and a tray program, with image recipes, a Compose file and a test script, `run-tests.sh`. The script does all of the above against the three programs:

1. publishes the programs for Windows with `dotnet publish -r win-x64`;
2. builds the tools layer and `sample/server:1`;
3. starts the fleet from `compose.yaml` under a project name of its own, with a trap that removes it;
4. checks the service: Windows reports it running, and the client reaches it by name;
5. drives the desktop program by element, then by keyboard and mouse, and checks the file it saved each time;
6. starts the tray program and checks its process and the file it writes every five seconds;
7. collects the files, event-log entries, process list, screenshots and resource use into `evidence/<run>`.

It needs `endia` with a working context, the .NET 10 SDK and `jq`. The service must hold a [root image](/docs/testing/images/#the-root-image) tagged `lab/win11:base`, which the sample's tools recipe builds on. The script prints `PASS: evidence in evidence/<run>`, or `FAIL:` with the check that failed and exits with 1.
