Skip to content

Commit

Permalink
clean up ensureLoggedIn (just have user pass URL), update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelgoeke committed May 1, 2024
1 parent a556138 commit e87beae
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
11 changes: 4 additions & 7 deletions src/testCommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
}

0 comments on commit e87beae

Please sign in to comment.