Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

End-to-end testing setup - staging #358

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions playwright/staging-tests/.envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use nix
11 changes: 11 additions & 0 deletions playwright/staging-tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
node_modules/
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
*auth.json
.env
.direnv/
/test-results/
__pycache__
*package-lock.json
72 changes: 72 additions & 0 deletions playwright/staging-tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Validation Tests

These tests are designed to run from your local machine (i.e. not from a GitHub action or CI).
The are intended to verify basic functionality is working after a deployment is made to staging, and prior to deployment to production.

## Essential reading:

* https://playwright.dev/
* https://direnv.net/docs/installation.html

## Setting up your environment

Before you can run or record tests, you need to set up your environment.

Running these tests require playwright set up on your local machine, as well as NodeJS.

**NixOS**

If you are a NixOS user, you can set up direnv and then cd into this directory in your shell.

When you do so the first time, you will be prompted to allow direnv which you can do using this command:


```
direnv allow
```

>  This may take a while the first time as NixOS builds you a sandbox environment.

**NON-NixOS**

For a non-NixOS user(Debian/Ubuntu) set up your environment by the following commands:

```bash
npm install
```

To install playwright browsers with OS-level dependencies use:


```bash
npm playwright install --with-deps chromium
```

**NOTE:** This only works with Debian/Ubuntu as they recieve official support from playwright. It will also request your master password to install the dependencies.

## Recording a test

There is a bash helper script that will let you quickly create a new test:

```
Usage: ./record-test.sh TESTNAME
e.g. ./record-test.sh mytest
will write a new test to tests/mytest.spec.ts
Do not use spaces in your test name.
Test files MUST END in .spec.ts

After recording your test, close the test browser.
You can then run your test by doing:
./run-tests.sh
```


>  The first time you record a test, it will store your session credentials in a file ending in ``auth.json``. This file should **NEVER** be committed to git / shared publicly. There is a gitignore rule to ensure this.

## Running a test


```
./run-tests.sh
```

4 changes: 4 additions & 0 deletions playwright/staging-tests/base-url.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

echo "Setting BASE_URL for test site"
BASE_URL=https://staging.plugins.qgis.org/
35 changes: 35 additions & 0 deletions playwright/staging-tests/create-auth.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash

source base-url.sh

source playwright-path.sh

echo "This script will write a new test to tests/deleteme.spec.ts"
echo "then delete it, leaving only the auth config."
echo ""
echo "When the playwright browser opens, log in to the site then exit."
echo "After recording your test, close the test browser."
echo "Recording auth token to auth.json"

# File exists and write permission granted to user
# show prompt
echo "Continue? y/n"
read ANSWER
case $ANSWER in
[yY] ) echo "Writing auth.json" ;;
[nN] ) echo "Cancelled."; exit ;;
esac

$PLAYWRIGHT \
codegen \
--target playwright-test \
--save-storage=auth.json \
-o tests/deleteme.spec.ts \
$BASE_URL

# We are only interested in auth.json
rm tests/deleteme.spec.ts

echo "Auth file creation completed."
echo "You can then run your tests by doing e.g.:"
echo "playwright test --project chromium"
23 changes: 23 additions & 0 deletions playwright/staging-tests/nodesource-install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

echo "NodeJS will be installed if not present"
echo "sudo password will be required"

USES_APT=$(which apt | grep -w "apt" | wc -l)
USES_RPM=$(which rpm | grep -w "rpm" | wc -l)

if [ $USES_APT -eq 1 ]; then
curl -SLO https://deb.nodesource.com/nsolid_setup_deb.sh
sudo chmod 500 nsolid_setup_deb.sh
sudo ./nsolid_setup_deb.sh 20
sudo apt-get install nodejs -y

elif [ $USES_RPM -eq 1 ]; then
curl -SLO https://rpm.nodesource.com/nsolid_setup_rpm.sh
sudo chmod 500 nsolid_setup_rpm.sh
sudo ./nsolid_setup_rpm.sh 20
sudo yum install nodejs -y --setopt=nodesource-nodejs.module_hotfixes=1
fi

echo "Done"
echo ""
75 changes: 75 additions & 0 deletions playwright/staging-tests/nsolid_setup_rpm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/bin/sh
set -e

NSOLID_VERSION=$1

################################
## Logger Function ##
################################
logger() {
local message="$1"
local type="$2"
local length=${#message}
local line=$(printf "%-${length}s" | tr ' ' '-')
echo ""
case "$type" in
"info")
echo "\033[38;5;79m$line\033[0m"
echo "\033[38;5;79m$message\033[0m"
echo "\033[38;5;79m$line\033[0m"
;;
"success")
echo "\033[1;32m$line\033[0m"
echo "\033[1;32m$message\033[0m"
echo "\033[1;32m$line\033[0m"
;;
"error")
echo "\033[1;31m$line\033[0m"
echo "\033[1;31m$message\033[0m"
echo "\033[1;31m$line\033[0m"
;;
*)
echo "\033[1;34m$line\033[0m"
echo "\033[1;34m$message\033[0m"
echo "\033[1;34m$line\033[0m"
;;
esac
echo ""
}


################################
## Error handler function ##
################################
handleError() {
local exit_code=$1
local error_message="$2"
logger "Error: $error_message (Exit Code: $exit_code)" "error"
exit $exit_code
}

################################
##Function to validate version##
################################
getVersion() {
local NSOLID_VERSION=$1
regex='^[0-9][0-9]$'
if ! expr "$NSOLID_VERSION" : "$regex" > /dev/null; then
return 1
fi
logger "Info: Configuring repository for N|solid $NSOLID_VERSION" "info"
}

##################################
##Function to configure the Repo##
##################################
configureRepo() {
local NSOLID_VERSION=$1
# Run 'yum install'
if ! yum install https://rpm.nodesource.com/pub_$NSOLID_VERSION.x/nodistro/repo/nodesource-release-nodistro-1.noarch.rpm -y; then
handleError "$?" "Failed to run yum install https://rpm.nodesource.com/pub_$NSOLID_VERSION.x/nodistro/repo/nodesource-release-nodistro-1.noarch.rpm -y"
fi
}

getVersion $NSOLID_VERSION || handleError $? "Must define a valid N|solid version"
configureRepo $NSOLID_VERSION || handleError $? "Failed configuring repository"
91 changes: 91 additions & 0 deletions playwright/staging-tests/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions playwright/staging-tests/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "staging-tests",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@playwright/test": "^1.41.2",
"@types/node": "^20.8.9"
}
}
Loading