From e87beaef7a8cb492b8a014ac0e2e24a63c935ef9 Mon Sep 17 00:00:00 2001 From: Michael Goeke Date: Wed, 1 May 2024 12:25:57 -0700 Subject: [PATCH] clean up ensureLoggedIn (just have user pass URL), update readme --- README.md | 16 +++++++++++++--- src/testCommon.js | 11 ++++------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index aa051b1..7ca0295 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ npx k6-script-from-har createTimeEntryWorkflow.har tests/createTimeEntryWorkflow run the test ``` sh -npx k6 run tests/createTimeEntryWorkflow.js +k6 run tests/createTimeEntryWorkflow.js ``` # Har file creation @@ -43,5 +43,15 @@ Playwright # Advanced usage -`testTemplate.js` and `testCommon.js` are used by the script. -These files can be modified for your specific purposes e.g. hardcoded auth information, etc. +`testCommon.js` will fetch an Authorization Bearer token, and attach the header automatically on all `httpRequest` if the environment variable `AT_AUTH_URL` is set. +note: to set an environment variable in a command prompt wrap the command with double quotes if the value includes & characters. +e.g. `set "AT_AUTH_URL=https://your.auth.url/oauth2/v2.0/token?username=myUserName&password=myPassword&etc_etc_etc"` + +the following variables are also available +* `AT_VU_COUNT` - defaults to 15 +* `AT_ITERATIONS` - defaults to AT_VU_COUNT * 3 +* k6 reference details [here](https://grafana.com/docs/k6/latest/using-k6/k6-options/how-to/#where-to-set-options) +by default the testTemplate defines options to run a constant number of VUs a given number of iterations. More executor details (here)[https://grafana.com/docs/k6/latest/using-k6/scenarios/executors/] + +`testTemplate.js` and `testCommon.js` are used by the script. +These files can be modified for your specific purposes e.g. hardcoded auth information, set the default iterations, etc. diff --git a/src/testCommon.js b/src/testCommon.js index 7c12616..53884e1 100644 --- a/src/testCommon.js +++ b/src/testCommon.js @@ -21,10 +21,10 @@ export function httpRequest(method, url, body, params = {}) { check(res, checks); } -export function _ensureLoggedIn(username, password, url) { +export function _ensureLoggedIn(url) { if (state.access_token) return; - const res = http.post(url.replace('${username}', username).replace('${password}', password)); + const res = http.post(url); if (!(res.status >= 200 && res.status < 400)){ console.error(res.status_text); throw 'could not log in'; @@ -35,9 +35,6 @@ export function _ensureLoggedIn(username, password, url) { } export function commonSetup() { - if (!__ENV.AT_USERNAME) console.warn('environment variable `AT_USERNAME` not specified'); - if (!__ENV.AT_PASSWORD) console.warn('environment variable `AT_PASSWORD` not specified'); - if (!__ENV.AT_AUTH_URL) console.warn('environment variable `AT_AUTH_URL` not specified'); - if (!__ENV.AT_USERNAME || !__ENV.AT_PASSWORD || !__ENV.AT_AUTH_URL) return; - _ensureLoggedIn(__ENV.AT_USERNAME, __ENV.AT_PASSWORD, __ENV.AT_AUTH_URL); + if (!__ENV.AT_AUTH_URL) { console.warn('environment variable `AT_AUTH_URL` not specified'); return; } + _ensureLoggedIn(__ENV.AT_AUTH_URL); } \ No newline at end of file