-
Notifications
You must be signed in to change notification settings - Fork 445
/
Copy pathbeacons.js
54 lines (48 loc) · 1.6 KB
/
beacons.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import fs from 'fs-extra';
const BEACON_FILE = process.env.LOG_PATH || './test/beacons.log';
/**
* @param {Object} count
* @return {Promise<boolean>} True if the beacon count matches.
*/
export async function beaconCountIs(count) {
await browser.waitUntil(async () => {
const beacons = await getBeacons();
return beacons.length === count;
});
}
/**
* Gets the array of beacons sent for the current page load (i.e. the
* most recently sent beacons with the same metric ID).
* @return {Promise<Array>}
*/
export async function getBeacons(id = undefined) {
const json = await fs.readFile(BEACON_FILE, 'utf-8');
const allBeacons = json.trim().split('\n').filter(Boolean).map(JSON.parse);
if (allBeacons.length) {
const lastBeaconID = allBeacons[allBeacons.length - 1].id;
return allBeacons.filter((beacon) => beacon.id === lastBeaconID);
}
return [];
}
/**
* Clears the array of beacons on the page.
* @return {Promise<void>}
*/
export async function clearBeacons() {
await fs.truncate(BEACON_FILE);
}