Skip to content

Commit 8a32fd8

Browse files
committed
Fix iOS and Safari test issues
1 parent 4e5c404 commit 8a32fd8

File tree

10 files changed

+58
-78
lines changed

10 files changed

+58
-78
lines changed

.env-default

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ LOCATION=developer_x
88

99
# Define a test browser on local machine
1010
DEV_LIMIT=1
11+
DEV_NAME=
1112
DEV_BROWSER=
1213
DEV_BROWSER_VERSION=
1314
DEV_OS=ios

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"watch": "node_modules/.bin/nodemon compile.js --ignore dist",
99
"build": "node compile.js",
1010
"pretest": "npm run build -- testing",
11-
"test": "if [ ! $BROWSERSTACK_USERNAME ]; then node -r dotenv/config ./test/index.js; else node ./test/index.js; fi",
11+
"test": "node -r dotenv/config ./test/index.js",
1212
"posttest": "npm run build"
1313
},
1414
"repository": {

src/default.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
var doNotTrack = "doNotTrack";
3434
var nav = window.navigator;
3535
var loc = window.location;
36-
var locationHostname = loc.hostname;
36+
var locationHostname = loc.host;
3737
var doc = window.document;
3838
var userAgent = nav.userAgent;
3939
var notSending = "Not sending request ";

test/constants/browserstack.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module.exports.BS_CAPABILITIES = {
3434
"browserstack.console": "disable",
3535
"browserstack.networkLogs": "true",
3636
"browserstack.ie.noFlash": "true",
37+
"browserstack.idleTimeout": 20,
3738
"browserstack.localIdentifier": BROWSERSTACK_LOCAL_IDENTIFIER,
3839
"browserstack.user": BROWSERSTACK_USERNAME,
3940
"browserstack.key": BROWSERSTACK_ACCESS_KEY,

test/helpers/index.js

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,10 @@ const log = (...messages) =>
99

1010
module.exports.sleep = promisify(setTimeout);
1111

12-
module.exports.getIPv4 = () =>
13-
new Promise((resolve, reject) => {
14-
const networkInterfaces = getNetworkInterfaces();
15-
const publicIPv4s = Object.keys(networkInterfaces)
16-
.reduce((interfaces, name) => {
17-
interfaces.push(...networkInterfaces[name]);
18-
return interfaces;
19-
}, [])
20-
.filter(({ family, internal }) => {
21-
return family === "IPv4" && !internal;
22-
});
23-
if (publicIPv4s.length) return resolve(publicIPv4s[0].address);
24-
return reject(Error("No local IPv4 address found"));
25-
});
26-
27-
module.exports.getLocalhost = async ({ useLocalIp = false } = {}) =>
28-
`${useLocalIp ? `${await this.getIPv4()}` : "localhost"}`;
12+
module.exports.getLocalhost = async ({ browser, os } = {}) => {
13+
if (os === "ios" || browser === "safari") return "bs-local.com";
14+
return "localhost";
15+
};
2916

3017
module.exports.generateRandomString = (length = 30) =>
3118
crypto.randomBytes(Math.ceil(length / 2)).toString("hex");
@@ -47,15 +34,9 @@ module.exports.makeUnique = (array = [], keys = []) => {
4734
}, []);
4835
};
4936

50-
module.exports.navigate = async ({
51-
browser,
52-
name,
53-
useLocalIp,
54-
driver,
55-
commands,
56-
}) => {
37+
module.exports.navigate = async ({ browser, os, name, driver, commands }) => {
5738
const localhost =
58-
`http://` + (await this.getLocalhost({ useLocalIp })) + ":" + SERVER_PORT;
39+
`http://` + (await this.getLocalhost({ os, browser })) + ":" + SERVER_PORT;
5940

6041
for (const {
6142
sleep: sleepMs = 0,
@@ -83,6 +64,7 @@ module.exports.navigate = async ({
8364
medium: "medium",
8465
ref: "ref",
8566
browser,
67+
os,
8668
}).toString();
8769

8870
if (sleepMs) {

test/helpers/server.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const bool = (input) => {
1919
const route = async (req, res) => {
2020
const { pathname, query } = url.parse(req.url, true);
2121
const {
22+
os,
2223
browser,
2324
script,
2425
redirect = true,
@@ -79,24 +80,26 @@ const route = async (req, res) => {
7980
if (pathname === "/script.js" && script) {
8081
res.writeHead(200, { "Content-Type": "text/javascript" });
8182
body = readFileSync(`./dist${script}`, "utf8");
82-
if (CI) {
83-
body = body.replace(/"https:\/\/queue\."/g, '"http://"');
84-
body = body.replace(
85-
/"simpleanalyticscdn\.com"/g,
86-
`"localhost:${SERVER_PORT}"`
87-
);
88-
} else {
89-
body = body.replace(/"https:/gi, `"http:`);
90-
body = body.replace(/"simpleanalyticscdn.com"/gi, `"localhost"`);
91-
}
83+
84+
const localhost =
85+
os === "ios" || browser === "safari"
86+
? `bs-local.com:${SERVER_PORT}`
87+
: `localhost:${SERVER_PORT}`;
88+
89+
body = body.replace(/"https:\/\/queue\."/g, '"http://"');
90+
body = body.replace(/"simpleanalyticscdn\.com"/g, `"${localhost}"`);
91+
9292
res.write(body);
9393
return res.end();
9494
}
9595

9696
res.writeHead(200, { "Content-Type": "text/html" });
9797
body = `<!DOCTYPE html>
9898
<html>
99-
<head><title>Simple Analytics Test</title></head>
99+
<head>
100+
<title>Simple Analytics Test</title>
101+
<meta name="viewport" content="width=device-width, initial-scale=1">
102+
</head>
100103
<body style="height: 300vh;">
101104
<h1>Path: ${pathname}</h1>`;
102105

@@ -125,18 +128,22 @@ const route = async (req, res) => {
125128
}
126129

127130
if (script) {
128-
const params = script ? new URLSearchParams({ script }).toString() : "";
131+
const params = new URLSearchParams({ script, browser, os }).toString();
129132
const attributes = ["defer", "async"];
130133
if (allowparams) attributes.push(`data-allow-params="${allowparams}"`);
131134
const attr = attributes.join(" ");
132135
const host =
133-
browser === "ie"
134-
? `http://localhost:${SERVER_PORT}`
136+
os === "ios" || browser === "safari"
137+
? `http://bs-local.com:${SERVER_PORT}`
135138
: `http://localhost:${SERVER_PORT}`;
136139
const scriptHTML = `<script ${attr} src="${host}/script.js?${params}"></script>`;
137140
body += scriptHTML;
141+
body += `<p><code>&lt;script ${attr} src="${host}/script.js?${params}"&gt;&lt;/script&gt;</code></p>`;
138142
}
139143

144+
body += `<p>OS: ${os}</p>`;
145+
body += `<p>Browser: ${browser}</p>`;
146+
140147
body += `</body></html>`;
141148

142149
res.write(body);

test/index.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const {
1818
} = require("./constants/browserstack");
1919

2020
const {
21+
DEV_NAME,
2122
DEV_BROWSER,
2223
DEV_BROWSER_VERSION,
2324
DEV_OS,
@@ -36,13 +37,10 @@ const matches = (param, search) => {
3637
return search && search.toLowerCase() !== param?.toLowerCase();
3738
};
3839

39-
const localBrowserFilter = ({
40-
device,
41-
os,
42-
os_version,
43-
browser,
44-
browser_version,
45-
}) => {
40+
const localBrowserFilter = (options) => {
41+
const { device, os, os_version, browser, browser_version } = options;
42+
const name = getDeviceName(options);
43+
if (matches(name, DEV_NAME)) return false;
4644
if (matches(device, DEV_DEVICE)) return false;
4745
if (matches(os, DEV_OS)) return false;
4846
if (matches(os_version, DEV_OS_VERSION)) return false;
@@ -79,6 +77,7 @@ const setTimezoneSupport = ({ device, os, os_version }) => {
7977
const osMajorVersion = getMajorVersion(os_version);
8078
if (os === "android" && device.includes("Samsung") && osMajorVersion >= 12)
8179
return false;
80+
if (os === "ios" && osMajorVersion < 13) return false;
8281
return true;
8382
};
8483

@@ -107,8 +106,6 @@ const getDriverWithTimeout = (capabilitiesRaw, timeout = 1080000) =>
107106
delete capabilities.supportsClientHints;
108107
delete capabilities.useLocalIp;
109108

110-
console.log(JSON.stringify({ capabilities }, null, 2));
111-
112109
const start = Date.now();
113110
let responded = false;
114111

@@ -257,8 +254,6 @@ const getDeviceName = ({
257254

258255
let driver = await getDriverWithTimeout(driverOptions);
259256

260-
console.log({ driver, typeof: typeof driver?.get });
261-
262257
// Try again with new device when driver is not available
263258
if (typeof driver?.get !== "function") {
264259
driver = await getDriverWithTimeout(driverOptions);

test/test-events.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ module.exports = async ({ os, browser }) => {
2323

2424
expect(
2525
new Date(JSON.parse(requests[2].body.metadata).date),
26-
"Event should have a date in the last 60 seconds"
27-
).to.greaterThan(new Date(Date.now() - 60000));
26+
"Event should have a date in the last 5 minutes"
27+
).to.greaterThan(new Date(Date.now() - 300000));
2828

2929
requests.map((request) => {
3030
expect(
@@ -54,21 +54,16 @@ module.exports = async ({ os, browser }) => {
5454
"page_id should be a valid UUIDv4"
5555
).to.be.true;
5656

57-
if (os === "ios") {
58-
expect(
59-
/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/.test(request.body.hostname),
60-
"Hostname should be an IP on iOS"
61-
).to.be.true;
62-
} else if (os === "OS X" && browser === "safari") {
57+
if (os === "ios" || browser === "safari") {
6358
expect(
6459
request.body.hostname,
6560
"Hostname should be bs-local.com on OS X Safari"
66-
).to.equal("bs-local.com");
61+
).to.equal("bs-local.com:3000");
6762
} else {
6863
expect(
6964
request.body.hostname,
7065
"Hostname should be localhost on non iOS"
71-
).to.equal("localhost");
66+
).to.equal("localhost:3000");
7267
}
7368

7469
expect(

test/test-no-pushstate.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,22 @@ module.exports = async () => {
99
body: { type: "pageview" },
1010
});
1111

12+
console.log(JSON.stringify(pageViewRequests, null, 2));
13+
1214
expect(
1315
pageViewRequests,
1416
"There are not enough page views requests found"
1517
).to.have.lengthOf(2);
1618

17-
expect([true, "true"], "The first visit should be unique").to.include(
18-
pageViewRequests[0].body.unique
19-
);
19+
expect(
20+
pageViewRequests[0].body.unique,
21+
"The first visit should be unique"
22+
).to.equal("true");
2023

21-
expect([false, "false"], "The second visit should not be unique").to.include(
22-
pageViewRequests[1].body.unique
23-
);
24+
expect(
25+
pageViewRequests[1].body.unique,
26+
"The second visit should not be unique"
27+
).to.equal("false");
2428

2529
pageViewRequests.map((request) => {
2630
expect(
@@ -54,10 +58,7 @@ module.exports = async () => {
5458
"Version should be a valid number"
5559
).to.be.a("number");
5660

57-
// We replace "https:" with "http:" string in server.js
58-
// that's why we expect local request to be https
59-
expect([true, "true"], "HTTPS should be a boolean").to.include(
60-
request.body.https
61-
);
61+
// We replace "https:" with "http:" string on CI
62+
expect(request.body.https, "HTTPS should be a boolean").to.equal("false");
6263
});
6364
};

test/test-pushstate.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { expect } = require("chai");
22
const UUIDvalidate = require("uuid-validate");
3+
const { CI } = require("./constants");
34
const { getRequests } = require("./helpers");
45

56
module.exports = async () => {
@@ -63,10 +64,7 @@ module.exports = async () => {
6364
"Version should be a valid number"
6465
).to.be.a("number");
6566

66-
// We replace "https:" with "http:" string in server.js
67-
// that's why we expect local request to be https
68-
expect([false, "false"], "HTTPS should be a boolean").to.include(
69-
request.body.https
70-
);
67+
// We replace "https:" with "http:" string on CI
68+
expect(request.body.https, "HTTPS should be a boolean").to.equal("false");
7169
});
7270
};

0 commit comments

Comments
 (0)