Merge pull request #1903 from lukashass/improve-e2e

add playwright and run e2e tests on production-like environment
This commit is contained in:
David Négrier 2022-02-19 18:57:59 +01:00 committed by GitHub
commit f9ac8e0c18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 2108 additions and 7285 deletions

View File

@ -1,6 +1,4 @@
# https://help.github.com/en/categories/automating-your-workflow-with-github-actions name: 'End to end tests'
name: "End to end tests"
on: on:
push: push:
@ -10,117 +8,31 @@ on:
pull_request: pull_request:
jobs: jobs:
test:
start-runner: timeout-minutes: 60
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork == false)
name: Start self-hosted EC2 runner
runs-on: ubuntu-latest runs-on: ubuntu-latest
outputs:
label: ${{ steps.start-ec2-runner.outputs.label }}
ec2-instance-id: ${{ steps.start-ec2-runner.outputs.ec2-instance-id }}
steps: steps:
- name: Configure AWS credentials - uses: actions/checkout@v2
uses: aws-actions/configure-aws-credentials@v1 - uses: actions/setup-node@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Start EC2 runner
id: start-ec2-runner
uses: machulav/ec2-github-runner@v2
with:
mode: start
github-token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
ec2-image-id: ami-094dbcc53250a2480
ec2-instance-type: m5.2xlarge
subnet-id: subnet-0ac40025f559df1bc
security-group-id: sg-0e36e96e3b8ed2d64
#iam-role-name: my-role-name # optional, requires additional permissions
#aws-resource-tags: > # optional, requires additional permissions
# [
# {"Key": "Name", "Value": "ec2-github-runner"},
# {"Key": "GitHubRepository", "Value": "${{ github.repository }}"}
# ]
end-to-end-tests:
name: "End-to-end testcafe tests"
needs: start-runner # required to start the main job when the runner is ready
runs-on: ${{ needs.start-runner.outputs.label }} # run the job on the newly created runner
steps:
- name: "Checkout"
uses: "actions/checkout@v2.0.0"
- name: "Setup NodeJS"
uses: actions/setup-node@v1
with: with:
node-version: '14.x' node-version: '14.x'
- name: Install dependencies
- name: "Install dependencies" run: npm ci
run: npm install working-directory: tests
working-directory: "tests" - name: Install Playwright
run: npx playwright install --with-deps
- name: "Setup .env file" - name: 'Setup .env file'
run: cp .env.template .env run: cp .env.template .env
- name: Start WorkAdventure
- name: "Edit ownership of file for test cases" run: docker-compose -f docker-compose.yaml -f docker-compose.e2e.yml up -d --build
run: sudo chown 1000:1000 -R . - name: Wait for environment to Start
run: sleep 60
- name: "Start environment" - name: Run Playwright tests
run: LIVE_RELOAD=0 docker-compose up -d run: npm run test-prod-like
working-directory: tests
- name: "Wait for environment to build (and downloading testcafe image)" - uses: actions/upload-artifact@v2
run: (docker-compose -f docker-compose.testcafe.yml build &) && docker-compose logs -f --tail=0 front | grep -q "Compiled successfully" if: always()
# - name: "temp debug: display logs"
# run: docker-compose logs
#
# - name: "Wait for back start"
# run: docker-compose logs -f back | grep -q "WorkAdventure HTTP API starting on port"
#
# - name: "Wait for pusher start"
# run: docker-compose logs -f pusher | grep -q "WorkAdventure starting on port"
- name: "Run tests"
run: PROJECT_DIR=$(pwd) docker-compose -f docker-compose.testcafe.yml up --exit-code-from testcafe
- name: Upload failed tests
if: ${{ failure() }}
uses: actions/upload-artifact@v2
with: with:
name: my-artifact name: playwright-report
path: './tests/screenshots/' path: tests/playwright-report/
retention-days: 30
- name: Display state
if: ${{ failure() }}
run: docker-compose ps
- name: Display logs
if: ${{ failure() }}
run: docker-compose logs
stop-runner:
name: Stop self-hosted EC2 runner
needs:
- start-runner # required to get output from the start-runner job
- end-to-end-tests # required to wait when the main job is done
runs-on: ubuntu-latest
if: ${{ always() }} # required to stop the runner even if the error happened in the previous jobs
steps:
- name: Configure AWS credentials
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork == false)
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Stop EC2 runner
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork == false)
uses: machulav/ec2-github-runner@v2
with:
mode: stop
github-token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
label: ${{ needs.start-runner.outputs.label }}
ec2-instance-id: ${{ needs.start-runner.outputs.ec2-instance-id }}

View File

@ -67,46 +67,20 @@ $ docker-compose exec back yarn run pretty
WorkAdventure is based on a video game engine (Phaser), and video games are not the easiest programs to unit test. WorkAdventure is based on a video game engine (Phaser), and video games are not the easiest programs to unit test.
Nevertheless, if your code can be unit tested, please provide a unit test (we use Jasmine), or an end-to-end test (we use Testcafe). Nevertheless, if your code can be unit tested, please provide a unit test (we use Jasmine), or an end-to-end test (we use Playwright).
If you are providing a new feature, you should setup a test map in the `maps/tests` directory. The test map should contain If you are providing a new feature, you should setup a test map in the `maps/tests` directory. The test map should contain
some description text describing how to test the feature. some description text describing how to test the feature.
* if the features is meant to be manually tested, you should modify the `maps/tests/index.html` file to add a reference * if the features is meant to be manually tested, you should modify the `maps/tests/index.html` file to add a reference
to your newly created test map to your newly created test map
* if the features can be automatically tested, please provide a testcafe test * if the features can be automatically tested, please provide an end-to-end test
#### Running testcafe tests #### Running end-to-end tests
End-to-end tests are available in the "/tests" directory. End-to-end tests are available in the "/tests" directory.
To run these tests locally: More information on running end-to-end tests can be found in the [`/tests/README`](/tests/README.md).
```console
$ LIVE_RELOAD=0 docker-compose up -d
$ cd tests
$ npm install
$ npm run test
```
Note: If your tests fail on a Javascript error in "sockjs", this is due to the
Webpack live reload. The Webpack live reload feature is conflicting with testcafe. This is why we recommend starting
WorkAdventure with the `LIVE_RELOAD=0` environment variable.
End-to-end tests can take a while to run. To run only one test, use:
```console
$ npm run test -- tests/[name of the test file].ts
```
You can also run the tests inside a container (but you will not have visual feedbacks on your test, so we recommend using
the local tests).
```console
$ LIVE_RELOAD=0 docker-compose up -d
# Wait 2-3 minutes for the environment to start, then:
$ PROJECT_DIR=$(pwd) docker-compose -f docker-compose.testcafe.yml up
```
### A bad wording or a missing language ### A bad wording or a missing language

47
docker-compose.e2e.yml Normal file
View File

@ -0,0 +1,47 @@
version: '3.5'
services:
# overrides for e2e tests to be closer to production
# use with command:
# docker-compose -f docker-compose.yaml -f docker-compose.e2e.yml up -d --build
front:
image: 'wa-front-e2e'
build:
context: ./
dockerfile: front/Dockerfile
environment:
STARTUP_COMMAND_1: 'envsubst < dist/env-config.template.js > dist/env-config.js'
STARTUP_COMMAND_2: ''
command: apache2-foreground
volumes: []
labels:
- "traefik.enable=true"
- "traefik.http.routers.front.rule=Host(`play.workadventure.localhost`)"
- "traefik.http.routers.front.entryPoints=web"
- "traefik.http.services.front.loadbalancer.server.port=80"
- "traefik.http.routers.front-ssl.rule=Host(`play.workadventure.localhost`)"
- "traefik.http.routers.front-ssl.entryPoints=websecure"
- "traefik.http.routers.front-ssl.tls=true"
- "traefik.http.routers.front-ssl.service=front"
pusher:
image: 'wa-pusher-e2e'
build:
context: ./
dockerfile: pusher/Dockerfile
environment:
STARTUP_COMMAND_1: ''
STARTUP_COMMAND_2: ''
command: yarn run runprod
volumes: []
back:
image: 'wa-back-e2e'
build:
context: ./
dockerfile: back/Dockerfile
environment:
STARTUP_COMMAND_1: ''
STARTUP_COMMAND_2: ''
command: yarn run runprod
volumes: []

View File

@ -1,19 +0,0 @@
version: "3.5"
services:
testcafe:
build: tests/
working_dir: /project/tests
command:
- --dev
# Run as root to have the right to access /var/run/docker.sock
user: root
environment:
BROWSER: "chromium --use-fake-device-for-media-stream"
PROJECT_DIR: ${PROJECT_DIR}
ADMIN_API_TOKEN: ${ADMIN_API_TOKEN}
volumes:
- ./:/project
- ./maps:/maps
- /var/run/docker.sock:/var/run/docker.sock
# security_opt:
# - seccomp:unconfined

View File

@ -40,7 +40,7 @@
{ {
"fontfamily":"Sans Serif", "fontfamily":"Sans Serif",
"pixelsize":11, "pixelsize":11,
"text":"Test:\nThis test is to be run automatically using testcafe", "text":"Test:\nThis test is to be run automatically using playwright",
"wrap":true "wrap":true
}, },
"type":"", "type":"",

View File

@ -40,7 +40,7 @@
{ {
"fontfamily":"Sans Serif", "fontfamily":"Sans Serif",
"pixelsize":11, "pixelsize":11,
"text":"Test:\nThis test is to be run automatically using testcafe\n\n(2nd file)", "text":"Test:\nThis test is to be run automatically using playwright\n\n(2nd file)",
"wrap":true "wrap":true
}, },
"type":"", "type":"",

2
tests/.gitignore vendored
View File

@ -1 +1,3 @@
/node_modules /node_modules
test-results/
playwright-report/

View File

@ -1,30 +0,0 @@
const BROWSER = process.env.BROWSER || "chrome --use-fake-ui-for-media-stream --use-fake-device-for-media-stream";
module.exports = {
"browsers": BROWSER,
"hostname": "localhost",
"src": "tests/",
"screenshots": {
"path": "screenshots/",
"takeOnFails": true,
"thumbnails": false,
},
"assertionTimeout": 20000,
"selectorTimeout": 60000,
"videoPath": "screenshots/videos",
"videoOptions": {
"failedOnly": true,
}
/*"skipJsErrors": true,
"clientScripts": [ { "content": `
window.addEventListener('error', function (e) {
if (e instanceof Error && e.message.includes('_jp')) {
console.log('Ignoring sockjs related error');
return;
}
throw e;
});
` } ]*/
}

View File

@ -1,5 +0,0 @@
FROM testcafe/testcafe:1.18.2
USER root
RUN apk add docker-compose
USER user

View File

@ -1,20 +1,48 @@
End-to-end tests # End-to-end tests
This directory contains automated end to end tests. This directory contains automated end to end tests.
To run them locally: ## Installation
```console ```bash
$ npm install npm install
$ ADMIN_API_TOKEN=123 npm test npx playwright install --with-deps
``` ```
You'll need to adapt the `ADMIN_API_TOKEN` to the value you use in your `.env` file. ## Run on development environment
Alternatively, you can use docker-compose to run the tests: Start WorkAdventure with:
```console ```bash
$ PROJECT_DIR=$(pwd) docker-compose -f docker-compose.testcafe.yml up --exit-code-from testcafe docker-compose up -d
``` ```
Note: by default, tests are running in Chrome locally and in Chromium in the Docker image. Wait 2-3 minutes for the environment to start, then:
Start the tests with:
```bash
npm run test
```
## Run on production like environment
Start WorkAdventure with:
```bash
docker-compose -f docker-compose.yaml -f docker-compose.e2e.yml up -d --build
```
Start the tests with:
```bash
npm run test-prod-like
```
## Run selected tests
End-to-end tests can take a while to run. To run only one test in one browser, use:
```bash
npm run test -- [name of the test file] --project=[chromium|firefox|webkit]
```

8236
tests/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,14 +1,13 @@
{ {
"devDependencies": { "devDependencies": {
"@playwright/test": "^1.19.1",
"@types/dockerode": "^3.3.0",
"axios": "^0.24.0",
"dockerode": "^3.3.1", "dockerode": "^3.3.1",
"testcafe": "^1.18.0" "dotenv-cli": "^5.0.0"
}, },
"scripts": { "scripts": {
"test": "testcafe" "test": "dotenv -e ../.env -- playwright test",
}, "test-prod-like": "OVERRIDE_DOCKER_COMPOSE=docker-compose.e2e.yml npm run test"
"dependencies": {
"@ffmpeg-installer/ffmpeg": "^1.1.0",
"@types/dockerode": "^3.3.0",
"axios": "^0.24.0"
} }
} }

105
tests/playwright.config.ts Normal file
View File

@ -0,0 +1,105 @@
import type { PlaywrightTestConfig } from '@playwright/test';
import { devices } from '@playwright/test';
/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// require('dotenv').config();
/**
* See https://playwright.dev/docs/test-configuration.
*/
const config: PlaywrightTestConfig = {
testDir: './tests',
/* Maximum time one test can run for. */
timeout: 30 * 1000,
expect: {
/**
* Maximum time expect() should wait for the condition to be met.
* For example in `await expect(locator).toHaveText();`
*/
timeout: 5000
},
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests. */
workers: 1,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: 'html',
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */
actionTimeout: 0,
/* Base URL to use in actions like `await page.goto('/')`. */
// baseURL: 'http://localhost:3000',
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},
/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: {
...devices['Desktop Chrome'],
},
},
{
name: 'firefox',
use: {
...devices['Desktop Firefox'],
},
},
{
name: 'webkit',
use: {
...devices['Desktop Safari'],
},
},
/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: {
// ...devices['Pixel 5'],
// },
// },
// {
// name: 'Mobile Safari',
// use: {
// ...devices['iPhone 12'],
// },
// },
/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: {
// channel: 'msedge',
// },
// },
// {
// name: 'Google Chrome',
// use: {
// channel: 'chrome',
// },
// },
],
/* Folder for test artifacts such as screenshots, videos, traces, etc. */
// outputDir: 'test-results/',
/* Run your local dev server before starting the tests */
// webServer: {
// command: 'npm run start',
// port: 3000,
// },
};
export default config;

View File

@ -1 +0,0 @@
*

View File

@ -0,0 +1,15 @@
import { expect, test } from '@playwright/test';
import { assertLogMessage } from './utils/log';
import { login } from './utils/roles';
test.describe('Module', () => {
test('loading should work out of the box', async ({ page }) => {
await page.goto(
'http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Modules/with_modules.json'
);
await login(page, 'Alice', 2);
await assertLogMessage(page, 'Successfully loaded module: foo = bar');
});
});

View File

@ -1,44 +0,0 @@
import {assertLogMessage} from "./utils/log";
const fs = require('fs');
const Docker = require('dockerode');
import { Selector } from 'testcafe';
import {login} from "./utils/roles";
import {
findContainer,
rebootBack,
rebootPusher,
resetRedis,
rebootTraefik,
startContainer,
stopContainer, stopRedis, startRedis
} from "./utils/containers";
import {getBackDump, getPusherDump} from "./utils/debug";
fixture `Modules`
.page `http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Modules/with_modules.json`;
test("Test that module loading works out of the box", async (t: TestController) => {
await login(t, 'http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Modules/with_modules.json');
await assertLogMessage(t, 'Successfully loaded module: foo = bar');
t.ctx.passed = true;
}).after(async t => {
if (!t.ctx.passed) {
console.log("Test 'Test that module loading works out of the box' failed. Browser logs:")
try {
console.log(await t.getBrowserConsoleMessages());
} catch (e) {
console.error('Error while fetching browser logs (maybe linked to a closed iframe?)', e);
try {
console.log('Logs from main window:');
console.log(await t.switchToMainWindow().getBrowserConsoleMessages());
} catch (e) {
console.error('Unable to retrieve logs', e);
}
}
}
});

View File

@ -0,0 +1,26 @@
import { expect, test } from '@playwright/test';
import { findContainer, startContainer, stopContainer } from './utils/containers';
import { login } from './utils/roles';
test.setTimeout(60000);
test.describe('Connection', () => {
test('can succeed even if WorkAdventure starts while pusher is down', async ({ page }) => {
await page.goto(
'http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/mousewheel.json'
);
// Let's stop the pusher
const container = await findContainer('pusher');
await stopContainer(container);
await page.goto(
'http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/mousewheel.json'
);
await expect(page.locator('.error-div')).toContainText('Unable to connect to WorkAdventure');
await startContainer(container);
await page.waitForResponse(response => response.status() === 200, { timeout: 60000 }),
await login(page);
});
});

View File

@ -1,63 +0,0 @@
import {assertLogMessage} from "./utils/log";
const fs = require('fs');
const Docker = require('dockerode');
import { Selector } from 'testcafe';
import {login, resetLanguage} from "./utils/roles";
import {findContainer, rebootBack, rebootPusher, resetRedis, startContainer, stopContainer} from "./utils/containers";
fixture `Reconnection`
.page `http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/mousewheel.json`;
test("Test that connection can succeed even if WorkAdventure starts while pusher is down", async (t: TestController) => {
// Let's stop the pusher
const container = await findContainer('pusher');
await stopContainer(container);
const errorMessage = Selector('.error-div');
await resetLanguage('en-US');
await t
.navigateTo('http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/mousewheel.json')
.expect(errorMessage.innerText).contains('Unable to connect to WorkAdventure')
await startContainer(container);
await login(t, 'http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/mousewheel.json');
t.ctx.passed = true;
}).after(async t => {
if (!t.ctx.passed) {
console.log("Test failed. Browser logs:")
console.log(await t.getBrowserConsoleMessages());
}
});
/*
test("Test that variables cache in the back don't prevent setting a variable in case the map changes", async (t: TestController) => {
// Let's start by visiting a map that DOES not have the variable.
fs.copyFileSync('../maps/tests/Variables/Cache/variables_cache_1.json', '../maps/tests/Variables/Cache/variables_tmp.json');
await t.useRole(userAlice);
//.takeScreenshot('before_switch.png');
// Let's REPLACE the map by a map that has a new variable
// At this point, the back server contains a cache of the old map (with no variables)
fs.copyFileSync('../maps/tests/Variables/Cache/variables_cache_2.json', '../maps/tests/Variables/Cache/variables_tmp.json');
await t.openWindow('http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Variables/Cache/variables_tmp.json')
.resizeWindow(960, 800)
.useRole(userAlice);
//.takeScreenshot('after_switch.png');
// Let's check we successfully manage to save the variable value.
await assertLogMessage(t, 'SUCCESS!');
t.ctx.passed = true;
}).after(async t => {
if (!t.ctx.passed) {
console.log("Test failed. Browser logs:")
console.log(await t.getBrowserConsoleMessages());
}
});
*/

View File

@ -0,0 +1,22 @@
import { expect, test } from '@playwright/test';
import { login } from './utils/roles';
test.describe('Translation', () => {
test('can be switched to French', async ({
page,
}) => {
await page.goto(
'http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/mousewheel.json'
);
await login(page);
await page.click('.menuIcon img:first-child');
await page.click('button:has-text("Settings")');
await page.selectOption('.languages-switcher', 'fr-FR');
await page.click('button:has-text("Save")');
await page.click('.menuIcon img:first-child');
await expect(page.locator('.menu-submenu-container h2')).toHaveText('Paramètres');
});
});

View File

@ -1,34 +0,0 @@
import { Selector } from "testcafe";
import { login } from "./utils/roles";
fixture`Translation`
.page`http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/mousewheel.json`;
test("Test that I can switch to French", async (t: TestController) => {
const languageSelect = Selector(".languages-switcher");
const languageOption = languageSelect.find("option");
await login(
t,
"http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/mousewheel.json"
);
await t
.click(".menuIcon img:first-child")
.click(Selector("button").withText("Settings"))
.click(".languages-switcher")
.click(languageOption.withText("Français (France)"))
.click(Selector("button").withText("Save"))
.wait(5000)
.click(".menuIcon img:first-child")
.expect(Selector("button").withText("Paramètres").innerText)
.contains("Paramètres");
t.ctx.passed = true;
}).after(async (t) => {
if (!t.ctx.passed) {
console.log("Test failed. Browser logs:");
console.log(await t.getBrowserConsoleMessages());
}
});

View File

@ -1,6 +1,6 @@
//import Docker from "dockerode"; //import Docker from "dockerode";
//import * as Dockerode from "dockerode"; //import * as Dockerode from "dockerode";
import Dockerode = require( 'dockerode') import Dockerode from 'dockerode';
const util = require('util'); const util = require('util');
const exec = util.promisify(require('child_process').exec); const exec = util.promisify(require('child_process').exec);
const { execSync } = require('child_process'); const { execSync } = require('child_process');
@ -8,20 +8,14 @@ const path = require("path");
const fs = require('fs'); const fs = require('fs');
/** /**
* Execute Docker compose, passing the correct host directory (in case this is run from the TestCafe container) * Execute Docker compose, passing the correct host directory
*/ */
export function dockerCompose(command: string): void { export function dockerCompose(command: string): void {
let param = ''; let param = '';
const projectDir = process.env.PROJECT_DIR; const overrideDockerCompose = process.env.OVERRIDE_DOCKER_COMPOSE;
if (!projectDir && fs.existsSync('/project')) { if (overrideDockerCompose) {
// We are probably in the docker-image AND we did not pass PROJECT_DIR env variable param += ' -f docker-compose.yaml -f '+overrideDockerCompose;
throw new Error('Incorrect docker-compose command used to fire testcafe tests. You need to add a PROJECT_DIR environment variable. Please refer to the CONTRIBUTING.md guide');
}
if (projectDir) {
const dirName = path.basename(projectDir);
param = '--project-name '+dirName+' --project-directory '+projectDir;
} }
let stdout = execSync('docker-compose '+param+' '+command, { let stdout = execSync('docker-compose '+param+' '+command, {

View File

@ -1,20 +1,27 @@
import { Selector } from 'testcafe'; import { expect, Page } from '@playwright/test';
const POLLING_INTERVAL = 50;
/** /**
* Tries to find a given log message in the logs (for 10 seconds) * Tries to find a given log message in the logs (for 10 seconds)
*/ */
export async function assertLogMessage(t: TestController, message: string): Promise<void> { export async function assertLogMessage(
let i = 0; page: Page,
let logs: string[]|undefined; substring: string,
do { timeout: number = 10000
const messages = await t.getBrowserConsoleMessages(); ): Promise<void> {
logs = messages['log']; let logs = [];
if (logs.find((str) => str === message)) { await page.on('console', async (msg) => {
logs.push(await msg.text());
});
// wait for log to appear
for (let i = 0; i < timeout / POLLING_INTERVAL; i++) {
if (logs.includes(substring)) {
break; break;
} }
await t.wait(1000); await page.waitForTimeout(POLLING_INTERVAL);
i++; }
} while (i < 30);
expect(logs).toContain(substring);
await t.expect(logs).contains(message);
} }

View File

@ -1,20 +1,20 @@
import { Role, ClientFunction } from 'testcafe'; import { Page } from '@playwright/test';
export const resetLanguage = ClientFunction((browserLanguage) => window.localStorage.setItem('language', browserLanguage)); export async function login(
page: Page,
userName: string = 'Alice',
characterNumber: number = 2,
browserLanguage: string | null = 'en-US'
) {
// window.localStorage.setItem('language', browserLanguage)
export async function login(t: TestController, url: string, userName: string = "Alice", characterNumber: number = 2, browserLanguage: string|null = 'en-US') { await page.fill('input[name="loginSceneName"]', userName);
await page.click('button.loginSceneFormSubmit');
await resetLanguage(browserLanguage);
t = t
.navigateTo(url)
.typeText('input[name="loginSceneName"]', userName)
.click('button.loginSceneFormSubmit');
for (let i = 0; i < characterNumber; i++) { for (let i = 0; i < characterNumber; i++) {
t = t.click('button.selectCharacterButtonRight'); await page.click('button.selectCharacterButtonRight');
} }
return t.click('button.selectCharacterSceneFormSubmit') await page.click('button.selectCharacterSceneFormSubmit');
.click('button.letsgo'); await page.click('button.letsgo');
} }

View File

@ -0,0 +1,164 @@
import { expect, test, chromium } from '@playwright/test';
import fs from 'fs';
import {
rebootBack,
rebootPusher,
rebootTraefik,
resetRedis,
startRedis,
stopRedis,
} from './utils/containers';
import { getBackDump, getPusherDump } from './utils/debug';
import { assertLogMessage } from './utils/log';
import { login } from './utils/roles';
test.setTimeout(180000);
test.describe('Variables', () => {
// WARNING: Since this test restarts traefik and other components, it might fail when run against the vite dev server.
// when running with --headed you can manually reload the page to avoid this issue.
test('storage works', async ({ page }) => {
await resetRedis();
await Promise.all([rebootBack(), rebootPusher()]);
await page.goto(
'http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Variables/shared_variables.json?somerandomparam=1'
);
await login(page);
const textField = page
.frameLocator('#cowebsite-buffer iframe')
.locator('#textField');
await expect(textField).toHaveValue('default value');
await textField.fill('');
await textField.fill('new value');
await textField.press('Tab');
await page.goto(
'http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Variables/shared_variables.json'
);
await expect(textField).toHaveValue('new value');
// Now, let's kill the reverse proxy to cut the connexion
console.log('Rebooting traefik');
rebootTraefik();
console.log('Rebooting done');
// Maybe we should:
// 1: stop Traefik
// 2: detect reconnecting screen
// 3: start Traefik again
await expect(textField).toHaveValue('new value', { timeout: 60000 });
stopRedis();
await textField.fill('');
await textField.fill('value set while Redis stopped');
await textField.press('Tab');
startRedis();
await page.goto('http://maps.workadventure.localhost/tests/');
const backDump = await getBackDump();
//console.log('backDump', backDump);
for (const room of backDump) {
if (
room.roomUrl ===
'http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Variables/shared_variables.json'
) {
throw new Error('Room still found in back');
}
}
const pusherDump = await getPusherDump();
//console.log('pusherDump', pusherDump);
expect(
pusherDump[
'http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Variables/shared_variables.json'
]
).toBe(undefined);
await page.goto(
'http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Variables/shared_variables.json'
);
// Redis will reconnect automatically and will store the variable on reconnect!
// So we should see the new value.
await expect(textField).toHaveValue('value set while Redis stopped');
// Now, let's try to kill / reboot the back
await rebootBack();
await page.goto(
'http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Variables/shared_variables.json'
);
await expect(textField).toHaveValue('value set while Redis stopped', {
timeout: 60000,
});
await textField.fill('');
await textField.fill('value set after back restart');
await textField.press('Tab');
await page.goto(
'http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Variables/shared_variables.json'
);
// Redis will reconnect automatically and will store the variable on reconnect!
// So we should see the new value.
await expect(textField).toHaveValue('value set after back restart');
// Now, let's try to kill / reboot the back
await rebootPusher();
await page.goto(
'http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Variables/shared_variables.json'
);
await expect(textField).toHaveValue('value set after back restart', {
timeout: 60000,
});
await textField.fill('');
await textField.fill('value set after pusher restart');
await textField.press('Tab');
await page.goto(
'http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Variables/shared_variables.json'
);
// Redis will reconnect automatically and will store the variable on reconnect!
// So we should see the new value.
await expect(textField).toHaveValue('value set after pusher restart');
});
test('cache doesnt prevent setting a variable in case the map changes', async ({
page,
}) => {
// Let's start by visiting a map that DOES not have the variable.
fs.copyFileSync(
'../maps/tests/Variables/Cache/variables_cache_1.json',
'../maps/tests/Variables/Cache/variables_tmp.json'
);
await page.goto(
'http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Variables/Cache/variables_tmp.json'
);
await login(page, 'Alice', 2);
// Let's REPLACE the map by a map that has a new variable
// At this point, the back server contains a cache of the old map (with no variables)
fs.copyFileSync(
'../maps/tests/Variables/Cache/variables_cache_2.json',
'../maps/tests/Variables/Cache/variables_tmp.json'
);
const browser = await chromium.launch();
const page2 = await browser.newPage();
await page2.goto(
'http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Variables/Cache/variables_tmp.json'
);
await login(page2, 'Alice', 2);
// Let's check we successfully manage to save the variable value.
await assertLogMessage(page2, 'SUCCESS!');
});
});

View File

@ -1,202 +0,0 @@
import {assertLogMessage} from "./utils/log";
const fs = require('fs');
const Docker = require('dockerode');
import { Selector } from 'testcafe';
import {login} from "./utils/roles";
import {
findContainer,
rebootBack,
rebootPusher,
resetRedis,
rebootTraefik,
startContainer,
stopContainer, stopRedis, startRedis
} from "./utils/containers";
import {getBackDump, getPusherDump} from "./utils/debug";
// Note: we are also testing that passing a random query parameter does not cause any issue.
fixture `Variables`
.page `http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Variables/shared_variables.json?somerandomparam=1`;
test("Test that variables storage works", async (t: TestController) => {
const variableInput = Selector('#textField');
await resetRedis();
await Promise.all([
rebootBack(),
rebootPusher(),
]);
//const mainWindow = await t.getCurrentWindow();
await login(t, 'http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Variables/shared_variables.json?somerandomparam=1');
await t //.useRole(userAliceOnPage('http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Variables/shared_variables.json?somerandomparam=1'))
.switchToIframe("#cowebsite-buffer iframe")
.expect(variableInput.value).eql('default value')
.selectText(variableInput)
.pressKey('delete')
.typeText(variableInput, 'new value')
.pressKey('tab')
.switchToMainWindow()
//.switchToWindow(mainWindow)
.wait(500)
// reload
.navigateTo('http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Variables/shared_variables.json')
.switchToIframe("#cowebsite-buffer iframe")
.expect(variableInput.value).eql('new value')
//.debug()
.switchToMainWindow()
//.wait(5000)
//.switchToWindow(mainWindow)
/*
.wait(5000)
//.debug()
.switchToIframe("#cowebsite-buffer iframe")
.expect(variableInput.value).eql('new value')
.switchToMainWindow();*/
// Now, let's kill the reverse proxy to cut the connexion
//console.log('Rebooting traefik');
rebootTraefik();
//console.log('Rebooting done');
// Maybe we should:
// 1: stop Traefik
// 2: detect reconnecting screen
// 3: start Traefik again
await t
.switchToIframe("#cowebsite-buffer iframe")
.expect(variableInput.value).eql('new value')
stopRedis();
// Redis is stopped, let's try to modify a variable.
await t.selectText(variableInput)
.pressKey('delete')
.typeText(variableInput, 'value set while Redis stopped')
.pressKey('tab')
.switchToMainWindow()
startRedis();
// Navigate to some other map so that the pusher connection is freed.
await t.navigateTo('http://maps.workadventure.localhost/tests/')
.wait(3000);
const backDump = await getBackDump();
//console.log('backDump', backDump);
for (const room of backDump) {
if (room.roomUrl === 'http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Variables/shared_variables.json') {
throw new Error('Room still found in back');
}
}
const pusherDump = await getPusherDump();
//console.log('pusherDump', pusherDump);
await t.expect(pusherDump['http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Variables/shared_variables.json']).eql(undefined);
await t.navigateTo('http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Variables/shared_variables.json')
.switchToIframe("#cowebsite-buffer iframe")
// Redis will reconnect automatically and will store the variable on reconnect!
// So we should see the new value.
.expect(variableInput.value).eql('value set while Redis stopped')
.switchToMainWindow()
// Now, let's try to kill / reboot the back
await rebootBack();
await t.navigateTo('http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Variables/shared_variables.json')
.switchToIframe("#cowebsite-buffer iframe")
.expect(variableInput.value).eql('value set while Redis stopped')
.selectText(variableInput)
.pressKey('delete')
.typeText(variableInput, 'value set after back restart')
.pressKey('tab')
.switchToMainWindow()
.navigateTo('http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Variables/shared_variables.json')
.switchToIframe("#cowebsite-buffer iframe")
// Redis will reconnect automatically and will store the variable on reconnect!
// So we should see the new value.
.expect(variableInput.value).eql('value set after back restart')
.switchToMainWindow()
// Now, let's try to kill / reboot the back
await rebootPusher();
await t.navigateTo('http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Variables/shared_variables.json')
.switchToIframe("#cowebsite-buffer iframe")
.expect(variableInput.value).eql('value set after back restart')
.selectText(variableInput)
.pressKey('delete')
.typeText(variableInput, 'value set after pusher restart')
.pressKey('tab')
.switchToMainWindow()
.navigateTo('http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Variables/shared_variables.json')
.switchToIframe("#cowebsite-buffer iframe")
// Redis will reconnect automatically and will store the variable on reconnect!
// So we should see the new value.
.expect(variableInput.value).eql('value set after pusher restart')
.switchToMainWindow()
t.ctx.passed = true;
}).after(async t => {
if (!t.ctx.passed) {
console.log("Test 'Test that variables storage works' failed. Browser logs:")
try {
console.log(await t.getBrowserConsoleMessages());
} catch (e) {
console.error('Error while fetching browser logs (maybe linked to a closed iframe?)', e);
try {
console.log('Logs from main window:');
console.log(await t.switchToMainWindow().getBrowserConsoleMessages());
} catch (e) {
console.error('Unable to retrieve logs', e);
}
}
}
});
test("Test that variables cache in the back don't prevent setting a variable in case the map changes", async (t: TestController) => {
// Let's start by visiting a map that DOES not have the variable.
fs.copyFileSync('../maps/tests/Variables/Cache/variables_cache_1.json', '../maps/tests/Variables/Cache/variables_tmp.json');
//const aliceOnPageTmp = userAliceOnPage('http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Variables/Cache/variables_tmp.json');
await login(t, 'http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Variables/Cache/variables_tmp.json', 'Alice', 2);
//.takeScreenshot('before_switch.png');
// Let's REPLACE the map by a map that has a new variable
// At this point, the back server contains a cache of the old map (with no variables)
fs.copyFileSync('../maps/tests/Variables/Cache/variables_cache_2.json', '../maps/tests/Variables/Cache/variables_tmp.json');
await t.openWindow('http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Variables/Cache/variables_tmp.json')
.resizeWindow(960, 800)
await login(t, 'http://play.workadventure.localhost/_/global/maps.workadventure.localhost/tests/Variables/Cache/variables_tmp.json', 'Bob', 3);
//.takeScreenshot('after_switch.png');
// Let's check we successfully manage to save the variable value.
await assertLogMessage(t, 'SUCCESS!');
t.ctx.passed = true;
}).after(async t => {
if (!t.ctx.passed) {
console.log("Test 'Test that variables cache in the back don't prevent setting a variable in case the map changes' failed. Browser logs:")
console.log(await t.getBrowserConsoleMessages());
}
});