Skip to content

Commit 9863f57

Browse files
levinmrMichael Levin
authored andcommitted
[Tech Debt] Update tests to be able to run against live staging/production environments
1 parent f68ab5f commit 9863f57

6 files changed

Lines changed: 109 additions & 19 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ npm run lint
130130
```
131131

132132
#### Run integration tests
133+
133134
Start up the test site at http://localhost:8080/:
134135

135136
```bash
@@ -142,4 +143,6 @@ Then run the tests against the test site:
142143
npm run cucumber
143144
```
144145

146+
See more details in the [testing docs](features/README.md).
147+
145148
**All members of the digital-analytics-program GitHub organization are required to have two-factor authentication enabled.**

features/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# DAP automated testing docs
2+
3+
The automated tests for the DAP code are implemented using [cucumber-js](https://github.com/cucumber/cucumber-js)
4+
and [puppeteer](https://pptr.dev/).
5+
6+
By default the tests use the local version of the DAP javascript files. Loading
7+
the code into a test HTML page, performing various user actions, and testing
8+
the behavior of the code in response to the user actions.
9+
10+
Use the `DAP_ENV` environment variable to insert live versions of the code into
11+
the test HTML page instead of the local version as described below.
12+
13+
## Running the tests
14+
15+
Start up the test site at http://localhost:8080/:
16+
17+
```bash
18+
npm run test-site
19+
```
20+
21+
Then run the tests against the test site:
22+
23+
```bash
24+
npm run cucumber
25+
```
26+
27+
## Running the tests with a debugger attached
28+
29+
```bash
30+
npm run cucumber:debug
31+
```
32+
33+
## Configuring with environment variables
34+
35+
### Verbose mode
36+
37+
Print debugging information to stdout while running the tests:
38+
39+
```bash
40+
VERBOSE=true npm run cucumber
41+
```
42+
43+
### Run tests against the live staging environment
44+
45+
```bash
46+
DAP_ENV=staging npm run cucumber
47+
```
48+
49+
### Run tests against the live production environment
50+
51+
```bash
52+
DAP_ENV=production npm run cucumber
53+
```

features/support/step_definitions/browser_steps.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ function delay(milliseconds) {
1212
Given("I load an empty browser", async function () {
1313
this.browser = await puppeteer.launch();
1414
this.page = await this.browser.newPage();
15+
16+
if (process.env.VERBOSE == 'true') {
17+
// Log page events to the node console
18+
this.page
19+
.on('console', message =>
20+
console.log(`${message.type().substr(0, 3).toUpperCase()} ${message.text()}`))
21+
.on('pageerror', ({ message }) => console.log(message))
22+
.on('response', response =>
23+
console.log(`${response.status()} ${response.url()}`))
24+
.on('requestfailed', request =>
25+
console.log(`${request.failure().errorText} ${request.url()}`))
26+
}
1527
});
1628

1729
Given("I set the browser to intercept outbound requests", async function () {
@@ -33,7 +45,7 @@ When("I wait {int} seconds", async function (delaySeconds) {
3345
await delay(delaySeconds * 1000);
3446
});
3547

36-
Then("there is a GA4 request", function() {
48+
Then("there is a GA4 request", function () {
3749
const ga4Request = this.requests.find(request => {
3850
try {
3951
const url = new URL(request.url);
@@ -46,8 +58,19 @@ Then("there is a GA4 request", function() {
4658
});
4759

4860
Then("there are no unexpected requests", function () {
49-
const requestUrls = this.requests.map((request) => {
61+
const requestURLs = this.requests.map((request) => {
5062
return (new URL(request.url)).host;
5163
});
52-
expect(["localhost:8080", "www.googletagmanager.com", "www.google-analytics.com"]).to.include.members(requestUrls);
53-
})
64+
65+
const allowedURLs = ["localhost:8080", "www.googletagmanager.com", "www.google-analytics.com"];
66+
67+
if (process.env.DAP_ENV == 'production') {
68+
allowedURLs.push("dap.digitalgov.gov")
69+
} else if (process.env.DAP_ENV == 'staging') {
70+
allowedURLs.push("d3vtlq0ztv2u27.cloudfront.net")
71+
}
72+
73+
requestURLs.forEach((requestURL) => {
74+
expect(allowedURLs).to.include(requestURL);
75+
})
76+
})

features/support/step_definitions/loading_steps.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ Given("DAP is configured with autotracking disabled", function () {
2727
});
2828

2929
When("I load the test site", async function () {
30-
await this.page.goto(`http://localhost:8080?${this.dapConfig.toQueryParams()}`);
31-
});
30+
await this.page.goto(`http://localhost:8080?${this.dapConfig.toQueryParams()}${process.env.DAP_ENV ? ('&testEnv=' + process.env.DAP_ENV) : ''}`);
31+
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"clean": "rm ./Universal-Federated-Analytics-Min.js",
99
"lint": "eslint Universal-Federated-Analytics.js",
1010
"cucumber": "cucumber-js",
11-
"cucumber:debug": "node --inspect-brk cucumber-js",
11+
"cucumber:debug": "VERBOSE=true node --inspect-brk node_modules/@cucumber/cucumber/bin/cucumber-js",
1212
"test-site": "docker build -t dap-test-site . && docker run --rm -p 8080:80 --name dap-test-site dap-test-site"
1313
},
1414
"repository": {

test_site/index.html

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,26 @@
66

77
<title>DAP test site</title>
88
<script>
9-
(function() {
9+
(function() {
1010
const queryParams = new URLSearchParams(window.location.search);
11-
const dapScriptTag = document.createElement("script");
12-
dapScriptTag.id = "_fed_an_ua_tag";
13-
dapScriptTag.async = true;
14-
dapScriptTag.src = `Universal-Federated-Analytics-Min.js?dapdev=true&${queryParams.toString()}`;
15-
document.head.appendChild(dapScriptTag);
16-
})();
11+
const dapScriptTag = document.createElement("script");
12+
let dapCodeLocation;
13+
dapScriptTag.id = "_fed_an_ua_tag";
14+
dapScriptTag.async = true;
15+
16+
if (queryParams.get('testEnv') == 'production') {
17+
queryParams.delete('testEnv');
18+
dapCodeLocation = 'https://dap.digitalgov.gov/Universal-Federated-Analytics-Min.js';
19+
} else if (queryParams.get('testEnv') == 'staging') {
20+
queryParams.delete('testEnv');
21+
dapCodeLocation = 'https://d3vtlq0ztv2u27.cloudfront.net/Universal-Federated-Analytics-Min.js';
22+
} else {
23+
dapCodeLocation = 'Universal-Federated-Analytics-Min.js';
24+
}
1725

26+
dapScriptTag.src = `${dapCodeLocation}?dapdev=true&${queryParams.toString()}`
27+
document.head.appendChild(dapScriptTag);
28+
})();
1829
</script>
1930
</head>
2031

@@ -269,8 +280,8 @@ <h4>Incorrect Event Name</h4>
269280
<a href="https://ca.linkedin.com/" class="fa fa-linkedin" onclick="gas4('social',{'link_text':'<link_text>','link_domain':'<link_domain>','link_url':'<link_url>','link_id':'<link_id>','link_classes':'<link_classes>','social_network':'<social_network>','content_type':'<content_type>','section':'<section>'});"></a>
270281
</div>
271282
</div>
272-
273-
283+
284+
274285

275286
</section>
276287

@@ -300,8 +311,8 @@ <h4>Incorrect Event Name</h4>
300311
<a href="https://ca.linkedin.com/" class="fa fa-linkedin" onclick="gas4('social',{'link_text':'<link_text>','link_domain':'<link_domain>','link_url':'<link_url>','link_id':'<link_id>','link_classes':'<link_classes>','social_network':'<social_network>','content_type':'<content_type>','section':'<section>'});"></a>
301312
</div>
302313
</div>
303-
304-
314+
315+
305316

306317
</section>
307318

@@ -458,7 +469,7 @@ <h2>gas4() - Call to Action Click </h2>
458469
}
459470

460471
.active, .accordion:hover {
461-
background-color: #ccc;
472+
background-color: #ccc;
462473
}
463474

464475
.panel {

0 commit comments

Comments
 (0)