# E2E
Welcome to Angelswing's E2E Test. 

# Structure

It's based on three things: 

1. URL

    Everything is firstly based on the suburl you are on. For example, if you are on `angeslwing.io/login`, what matters is `login`. You are going to test everything that's in that suburl. 

2. General (big) features

    This directly depends on the amount of the code you need to write. For `login`, for example, we have to test files:
    - `login.automaticSignin.test.ts` -> for automatic sign feature in only
    - `login.index.test.ts` --> for general features

    From a code perspective, `automaticSignin` is a big feature because we need to integrate localStorage with it. Plus, writing in the same test file would have some conflict with `beforeEach`, `beforeAll`, ... and so on, because there implementations are different. 

    So please separate out the test files based on 'big features'.

3. Features inside the specific feature

    So what is that 'everything'? Well, for example, in the login page, you have some features to test:

    - Login success
    - Login failure on:
      - Wrong email
      - Wrong password
    - 'Remember me' option
    - 'Sign up' link pointing to the signup page
    - 'Forgot your password' link pointing to the password page
    - Clicking on the left top logo leading to Angelswing homepage
    - Clicking on 'support' leading to the support page
    
    are there some more? I think that's it. right? For more, have a look at `tests/login.test.ts`.
    
    Anyways, it's something like this.

    Every single page has its own features. Some pages have a lot, like the content page. You have to make sure that the feature is tested although there are many. 


# Configuration

0. Make `.env` file at the root directory (inside the e2e folder)

    Fill out two fields only (you don't need to change `HEADLESS` unless you don't want the browser to be shown)

    ```
    ID=e2e@angelswing.io
    PW=Angelswing1234!
    HEADLESS=0
    ```

    **Warning: Use your account at caution! Puppeteer will delete all annotations before starting the test.**

1. Install docker (community version)]

    [Go to the install page, pick the right one based on the OS, and install](https://docs.docker.com/install/)

2. Build the image

    For development purpose:
    ```bash
    docker build -f dev.dockerfile -t angelswing-e2e-dev . 
    ```

    For CI purpose:
    ```bash
    docker build -f ci.dockerfile -t angelswing-e2e-ci .
    ```

# Workflow

0. Launch your local development on `localhost:3500`

    ```bash
    npm start # inside the app folder or project root
    ```

1. Navigate to the e2e testing directory

    ```bash
    cd somewhere/e2e
    ```

1. Run your tests on your local computer against `localhost:3500`

    If you want to see if tests work well in docker, against `localhost:3500` (no browser shown): 

    ```bash
    BASEDIR="$PWD"

    docker run --network=host --volume $BASEDIR/screenshots:/e2e/screenshots --volume $BASEDIR/tests:/e2e/tests --volume $BASEDIR/.env:/e2e/.env --volume $BASEDIR/util:/e2e/util --volume $BASEDIR/types:/e2e/types -e OVERRIDE_URL=localhost:3500 -it angelswing-e2e-dev
    ```

    else if you want to test on your local (browser shown):

    ```bash
    OVERRIDE_URL=localhost:3500 npm test 
    ```

    Note: if `OVERRIDE_URL` is not provided, the test will run for `https://dev.angelswing.io` by default.

    Check if everything works. Fix the things as needed. Repeat this step until tests are successful. 
    **Only if you find things are working successfully, you can move onto step 2.**

2. Run the tests to generate snapshots for `CI`

    **Why is this step needed?**: test may be correct, but the linux docker image somehow shows chrome with a little different font styles. 
    
    You might see this message if you are running image snapshot tests:

    ```
    ● project|content|marker › should create marker #1 at pixel (945, 405)

    Expected image to match or be a close match to snapshot but was 0.3660968660968661% different from snapshot (6168 differing pixels).
    See diff for details: /e2e/tests/Project/Content/__image_snapshots__/__diff_output__/marker-test-ts-project-content-marker-should-create-marker-1-at-pixel-945-405-1-diff.png
    ```
    
    ![Troubleshooting: image snapshot tests](./troubleshooting-image-snapshot-test.png)

    As you can see, the only difference (in the picture in the middle) is the fonts. [This is because the font styles are a bit differently rendered across different OS's](https://stackoverflow.com/questions/5726292/webfonts-rendering-differently-on-windows-mac-and-linux). 

    So you need to manually update snapshots for CI. Snapshots will be **taken inside docker image to make it consistent with the tests run inside docker** and they will be stored in your local. To do that, run:

    ```bash
    # Make sure you are in the e2e testing directory beforehand!!!
    BASEDIR="$PWD"

    docker run --network=host --volume $BASEDIR/screenshots:/e2e/screenshots --volume $BASEDIR/tests:/e2e/tests --volume $BASEDIR/.env:/e2e/.env --volume $BASEDIR/util:/e2e/util --volume $BASEDIR/types:/e2e/types -e FOR_CI=1 -e OVERRIDE_URL=localhost:3500 -it angelswing-e2e-dev 
    ```

    Then this will generate another set of snapshot images stored in each `${__dirname}/__docker_diff_output__/` and `${__dirname}/__docker_image_snapshots__/`.

    Check if all snapshots are correctly generated. That's solely up to you.

    Now you are ready for CI.

# Misc commands

```bash
# copy screenshots to desktop (your container needs to be running)
docker cp [container-id]:/e2e/screenshots ~/Desktop

# Remove all containers
docker rm `docker ps -qa`

# Remove all images
docker rmi -f `docker images -qa`

# Remove all volumes
docker volume rm $(docker volume ls -qf)

# Remove all networks
docker network rm `docker network ls -q`

# Enter bash shell inside the container 
docker run -t -d <image id/name> bash # and then:
docker exec -it <container id/name> bash

# Get the lastest container/image id
CONTAINER_ID=$(docker ps -qa | head -n 1)
IMG_ID=$(docker images -qa | head -n 1)

# Restart the container
docker start $CONTAINER_ID

# Copy with the wildcard
docker exec <id> bash -c "mkdir imgs-to-send; cp -r tests/**/__docker_image_snapshots__/*png s"
docker cp <id>:/imgs-to-send .
```

# Links for help and ideas

- https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md#running-puppeteer-in-docker
- https://stackoverflow.com/questions/51667599/issue-in-executing-puppeteer-in-headful-mode-in-docker
- https://vsupalov.com/headless-chrome-puppeteer-docker/
- https://github.com/GoogleChrome/puppeteer/issues/1645
- https://paul.kinlan.me/hosting-puppeteer-in-a-docker-container/
- https://www.rrweb.io/
- https://github.com/GoogleChrome/puppeteer/issues/478
- https://github.com/muralikg/puppetcam
- https://github.com/synyx/jest-canvas-snapshot-serializer
- https://github.com/muaz-khan/Chrome-Extensions/tree/master/screen-recording


3. Run the test

    For development purpose:
    ```bash
    # Make sure you are in the e2e testing directory beforehand
    BASEDIR="$PWD"

    docker run --network=host --volume $BASEDIR/screenshots:/e2e/screenshots --volume $BASEDIR/tests:/e2e/tests --volume $BASEDIR/.env:/e2e/.env --volume $BASEDIR/util:/e2e/util --volume $BASEDIR/types:/e2e/types -e HEADLESS='1' -it angelswing-e2e-dev
    ```

    For CI purpose:
    ```bash
    docker run --network=host -e HEADLESS=1 -e ID=e2e@angelswing.io -e PW=Angelswing1234! -it angelswing-e2e-dev
    ```

    The reason we are separating dev from CI is that [we cannot mount volumes on CirclecCI](https://support.circleci.com/hc/en-us/articles/360007324514-How-can-I-mount-volumes-to-docker-containers-). That's the only reason. 

    For CI purpose (more secure). [See env variable page on CircleCI](https://circleci.com/gh/angelswing-team/angelswing-frontend/edit#env-vars)
    ```bash
    docker run --network=host -e HEADLESS='1' -e ID=$E2E_ID -e PW=$E2E_PW -it angelswing-e2e-dev
    ```
