Skip to content

Commit

Permalink
Add Playwright to the transition repo, and run a few simple tests on …
Browse files Browse the repository at this point in the history
…github.

Fix: #1065
  • Loading branch information
GabrielBruno24 committed Oct 11, 2024
1 parent 2eb1888 commit 8d557eb
Show file tree
Hide file tree
Showing 22 changed files with 790 additions and 62 deletions.
8 changes: 8 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,11 @@ MAIL_TRANSPORT_SMTP_AUTH_PWD=password

# From email
MAIL_FROM_ADDRESS=[email protected]

#Parameters used to login to a test account in the playwright tests
PLAYWRIGHT_TEST_USER=testUser
PLAYWRIGHT_TEST_EMAIL=[email protected]
PLAYWRIGHT_TEST_PASSWORD=testPassword

# Put as true when testing Playwright on the pipeline
IS_TESTING_PLAYWRIGHT=false
59 changes: 56 additions & 3 deletions .github/workflows/transition.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,39 @@ on:
jobs:
build-and-test:
runs-on: ubuntu-latest
services:
postgres:
image: postgis/postgis
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
env:
POSTGRES_USER: testuser
POSTGRES_PASSWORD: testpassword
POSTGRES_DB: testdb
strategy:
matrix:
node-version: [18.x, 20.x]
env:
PROJECT_CONFIG: ${{ github.workspace }}/examples/config.js
PG_CONNECTION_STRING_PREFIX: postgres://testuser:testpassword@localhost:5432/
PG_DATABASE_PRODUCTION: testdb
PG_DATABASE_DEVELOPMENT: testdb
CI: true ## This is to make sure that the tests run in CI mode
PLAYWRIGHT_TEST_USER: testUser
PLAYWRIGHT_TEST_EMAIL: [email protected]
PLAYWRIGHT_TEST_PASSWORD: testPassword
IS_TESTING_PLAYWRIGHT: true
steps:
- uses: actions/checkout@v4
- name: copy env file
run: cp .env.example .env
- name: copy config file
run: cp examples/config-example-no-osrm.js examples/config.js
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
Expand All @@ -31,10 +55,39 @@ jobs:
run: yarn compile
- name: Build Client bundle
run: yarn build:prod
- name: Unit Test
run: yarn test
- name: UI Test
# - name: Unit Test
# run: yarn test
# Following configure the automated UI tests
- name: Create DB
run: yarn setup && yarn migrate
env:
NODE_ENV: production
- name: Get Playwright config
run: cp packages/chaire-lib-frontend/playwright-example.config.ts packages/chaire-lib-frontend/playwright.config.ts
- name: Create test user
run: yarn create-user --username $PLAYWRIGHT_TEST_USER --email $PLAYWRIGHT_TEST_EMAIL --password $PLAYWRIGHT_TEST_PASSWORD --admin
- name: Create test resources directory
run: mkdir -p packages/chaire-lib-frontend/ui-tests-resources
- name: Download gtfs feed
run: curl -L -o packages/chaire-lib-frontend/ui-tests-resources/gtfs.zip https://github.com/chairemobilite/transition/releases/download/playwright/gtfs.zip
- name: Download invalid gtfs feed
run: curl -L -o packages/chaire-lib-frontend/ui-tests-resources/invalid-gtfs.zip https://github.com/chairemobilite/transition/releases/download/playwright/invalid-gtfs.zip
- name: Start application
run: yarn start &
env:
NODE_ENV: production
- name: Start cache
rum: yarn start:json2capnp -- 2000 $PWD/examples/runtime/cache/demo_transition
- name: Run UI tests
run: yarn test:ui
- name: Archive UI Test results
if: always()
uses: actions/upload-artifact@v4
with:
name: playwright-results-${{matrix.node-version}} # This is to make sure that the results are stored in a unique name
path: packages/chaire-lib-frontend/test-results
retention-days: 2
# End of automated UI tests

code-lint:
runs-on: ubuntu-latest
Expand Down
11 changes: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,13 @@ tests/test
profilingData*

#example apps
examples/runtime
examples/runtime
examples/config.js

# playwright test output
**/playwright-report/
**/test-results/
**/playwright.config.ts

# playwright test resources
**/ui-tests-resources/
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,41 @@ For example, to run yarn test in the chaire-lib backend:
You can also run the app this way with:
`docker run -a STDOUT -it -v "${PWD}:/home/project" -w=/home/project/ testtransition yarn start`

### UI testing with Playwright

To execute UI tests with Playwright, you first have to create an account that will be used for logging in to Transition during the tests. This only needs to be done once:
```
yarn create-user --username testUser --email [email protected] --password testPassword --admin
```

You will also need some files used by the "Import GTFS" test. First, create a 'ui-tests-resources' directory in 'packages/chaire-lib-frontend'. Then, download the files 'gtfs.zip' and 'invalid-gtfs.zip' found here: https://github.com/chairemobilite/transition/releases/tag/playwright, and add these files to the newly created 'ui-tests-resources' folder.

Next, configure Playwright by copying the example config file and select the browser to test in:
```
cp packages/chaire-lib-frontend/playwright-example.config.ts packages/chaire-lib-frontend/playwright.config.ts
```

Next, install browser dependencies to correctly execute the tests. If it is not done, an arror message should tell you the command when attempting to run the test. It is possible to install each browser separaly with the following command, for example `firefox`:
```
npx playwright install --with-deps firefox
```

Now that Playwright is configured, you need to start the application as you would to run it normally:
```
yarn build:dev or yarn build:prod
yarn start
```

Then, to run the tests:
```
yarn test:ui
```

You can also use this command to open a graphic interface that allows you to run tests individually and gives more info:
```
yarn test:ui --ui
```

## Contributing

See [CONTRIBUTING.md](CONTRIBUTING.md)
Expand Down
18 changes: 10 additions & 8 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,31 @@ This folder contains an example configuration file, as well as necessary files t

1. Make sure all the pre-requisites are installed. For complete installation instructions of all dependencies, see the [instructions for Ubuntu](../../docs/setupDevEnvironmentUbuntu20.04.md).

2. Update the .env file and set the `PROJECT_CONFIG` to point to the config file in this directory
2. Copy the config-example-osrm.js file, place the copy in the same directory, and rename is config.js. If you want to use the default routing engine instead of OSRM, you may copy config-example-no-osrm.js instead.

3. Update the .env file and set the `PROJECT_CONFIG` to point to the config file in this directory

```
PROJECT_CONFIG=</path/to/this/repo>/examples/transition/config.js
PROJECT_CONFIG=</path/to/this/repo>/examples/config.js
```

3. Follow the [installation instructions](../../README.md#installation) at the root of this repo to setup the database and create users
4. Follow the [installation instructions](../../README.md#installation) at the root of this repo to setup the database and create users

4. Follow the [build and start instructions](../../README.md#build-and-start) at the root of this repo to compile and build the code, but do not start the nodejs server yet.
5. Follow the [build and start instructions](../../README.md#build-and-start) at the root of this repo to compile and build the code, but do not start the nodejs server yet.

5. Get and prepare the road network for `osrm` to route. This step is optional, but required to create new lines that properly follow the road network. The first line will download the Open Street Map network data from the overpass API. The second line will prepare the data for the `osrm` servers. Data is prepared differently for different modes of transportation. Selecting `driving` and `walking` are mandatory modes, as `driving` is the default mode for vehicles and `walking` is required to calculate access, transfer and egress times from transit.
6. Get and prepare the road network for `osrm` to route. This step is optional, but required to create new lines that properly follow the road network. The first line will download the Open Street Map network data from the overpass API. The second line will prepare the data for the `osrm` servers. Data is prepared differently for different modes of transportation. Selecting `driving` and `walking` are mandatory modes, as `driving` is the default mode for vehicles and `walking` is required to calculate access, transfer and egress times from transit.

```shell
yarn node --max-old-space-size=4096 packages/chaire-lib-backend/lib/scripts/osrm/downloadOsmNetworkData.task.js --polygon-file examples/polygon_rtl_area.geojson

yarn node --max-old-space-size=4096 packages/chaire-lib-backend/lib/scripts/osrm/prepareOsmNetworkData.task.js
```

6. Start the Node.js server with `yarn start:demo`.
7. Start the Node.js server with `yarn start:demo`.

7. Navigate to `http://localhost:8080` and log into the application.
8. Navigate to `http://localhost:8080` and log into the application.

8. Import [transit data for the Réseau de Transport de Longueuil](https://transitfeeds.com/p/reseau-de-transport-de-longueuil/37), that will work for the area of this demo. Lines and paths can be edited and added within this territory.
9. Import [transit data for the Réseau de Transport de Longueuil](https://transitfeeds.com/p/reseau-de-transport-de-longueuil/37), that will work for the area of this demo. Lines and paths can be edited and added within this territory.

# Running the application for another territory

Expand Down
158 changes: 158 additions & 0 deletions examples/config-example-no-osrm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
module.exports = {

projectShortname: 'demo_transition',
projectDirectory: `${__dirname}/runtime/`,
auth: {
localLogin: {
allowRegistration: true,
// This will send an email to confirm the user's email address. Email send needs to be configured. By default, users can register and directly login.
// confirmEmail: true
}
},
maxParallelCalculators: 2,

// Maximum number of parallel calculation. Used in tasks to start the calculator with this number of threads.
// maxParallelCalculators: 2,

// @deprecated: Use the cacheAllScenarios in the 'routing.transit.engines.trRouting' configuration instead
// trRoutingCacheAllScenarios: false,
// Configuration for the trRouting services. Single is for the single calculation instance (from the UI and public API), while batch is for the batch calculation instance, for tasks
// routing: {
// transit: {
// defaultEngine: 'trRouting',
// engines: {
// trRouting: {
// single: {
// port: 4000,
// // Enable caching of connections for all scenarios in trRouting. Will use more memory
// cacheAllScenarios: false
// },
// batch: {
// port: 14000,
// // Enable caching of connections for all scenarios in trRouting. Will use more memory and may not be necessary for batch calculations as currently there's only one scenario per task
// cacheAllScenarios: false
// }
// }
// }
// }
// },

mapDefaultCenter: {
lat: 45.5092960,
lon: -73.4769080
},

languages: ['fr', 'en'],

locales: {
fr: 'fr-CA',
en: 'en-CA'
},

languageNames: {
fr: "Français",
en: "English"
},

title: {
fr: "Démo",
en: "Demo"
},

defaultLocale: "fr",
timezone: "America/Montreal",
gtfs: {
socketFileUploaderOptions: {
uploadDirectory : 'gtfs',
fileExtension : 'zip',
renamedFileNameWithoutExtension: 'import',
acceptTypes : ['application/zip'],
maxFileSizeMB : 256,
chunckSizeMB : 10240000,
overwriteExistingFile : true
}
},

defaultPreferences: {
osrmRouting: {
modes: {
driving: {
// !!! Be careful: use your own server, since you may be blocked on the osrm demo server after too many queries
port : 7000, // Port used to access OSRM, either locally or remotely
host : null, // If set to null, localhost will be used. Ignored if autoStart set to true
autoStart: true, // If true, a local instance of OSRM will be started
enabled : true // If true, this mode will be configured, otherwise will be left out
},
cycling: {
port : 8000,
host : null,
autoStart: true,
enabled : true
},
walking: {
port : 5001,
host : null,
autoStart: true,
enabled : false
},
bus_suburb: {
port : 7110,
host : null,
autoStart: true,
enabled : true
},
bus_urban: {
port : 7120,
host : null,
autoStart: true,
enabled : true
},
rail: {
port : 9000,
host : null,
autoStart: false,
enabled : false
},
tram: {
port : 9100,
host : null,
autoStart: false,
enabled : false
},
tram_train: {
port : 9200,
host : null,
autoStart: false,
enabled : false
},
metro: {
port : 9300,
host : null,
autoStart: false,
enabled : false
},
monorail: {
port : 9400,
host : null,
autoStart: false,
enabled : false
},
cable_car: {
port : 9500,
host : null,
autoStart: false,
enabled : false
}
}
},
transit: {
routing: {
batch: {
allowSavingOdTripsToDb: true
}
}
}
},
tokenLifespanDays: 14
};

File renamed without changes.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"test": "yarn workspaces run test",
"test:unit": "yarn workspaces run test:unit",
"test:sequential": "yarn workspaces run test:sequential",
"test:ui": "yarn workspaces run test:ui",
"test:ui": "yarn workspace chaire-lib-frontend run test:ui",
"lint": "yarn workspaces run lint",
"format": "yarn workspaces run format",
"list-tasks": "yarn workspace transition-backend run list-tasks",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ const startTrRoutingProcess = async (
cacheAllScenarios: false // Flag to enable the trRouting connection cache for all scenario
}
) => {
const osrmWalkingServerInfo = osrmService.getMode('walking').getHostPort();
const modes = config.defaultPreferences.osrmRouting.modes;
const walkingEnabled = modes.walking.enabled as boolean;
let osrmWalkingServerInfo;
if (walkingEnabled) {
osrmWalkingServerInfo = osrmService.getMode('walking').getHostPort();
}

const serviceName = getServiceName(port);
const tagName = 'trRouting';

Expand All @@ -39,8 +45,9 @@ const startTrRoutingProcess = async (
const commandArgs = [
// FIXME Extract to constant the 'cache/' part of the directory somewhere
`--port=${port}`,
`--osrmPort=${osrmWalkingServerInfo.port}`,
`--osrmHost=${osrmWalkingServerInfo.host}`,
`--useEuclideanDistance=${walkingEnabled ? '0' : '1'}`,
`--osrmPort=${osrmWalkingServerInfo?.port}`,
`--osrmHost=${osrmWalkingServerInfo?.host}`,
`--debug=${parameters.debug === true ? 1 : 0}`
];
if (parameters.cacheDirectoryPath) {
Expand Down
Loading

0 comments on commit 8d557eb

Please sign in to comment.