-
Notifications
You must be signed in to change notification settings - Fork 239
/
grades.js
89 lines (75 loc) · 2.72 KB
/
grades.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* global dbg:false */
const harness = require('../helpers/harness')
const tests = [
{ url: 'duckduckgo.com', siteGrade: 'A', enhancedGrade: 'D' },
{ url: 'www.independent.co.uk', siteGrade: ['D', 'D-'], enhancedGrade: 'B+' },
{ url: 'google.com', siteGrade: 'D', enhancedGrade: 'D' },
{ url: 'reddit.com', siteGrade: ['D', 'D-', 'C'], enhancedGrade: 'B' },
{ url: 'facebook.com', siteGrade: ['D', 'C+'], enhancedGrade: 'C+' },
// FIXME - This case is flaking.
// { url: 'twitter.com', siteGrade: 'C', enhancedGrade: 'B' },
{ url: 'en.wikipedia.org', siteGrade: 'B+', enhancedGrade: 'B+' }
]
let browser
let bgPage
let teardown
// getting HTTPS can take a while
jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000
const getGradeByUrl = (url) => {
const tabsById = dbg.tabManager.tabContainer
let tab
Object.keys(tabsById).some((id) => {
const t = tabsById[id]
if (t.url.indexOf(url) > -1) {
tab = tabsById[id]
return true
}
return false
})
return tab.site.grade.get()
}
// allow a flexible range defined via an array,
// e.g. if the site serves a variable number of trackers
const checkGrade = (result, expected) => {
if (expected instanceof Array) {
expect(expected).toContain(result)
} else {
expect(result).toEqual(expected)
}
}
describe('grade sanity checks', () => {
beforeAll(async () => {
({ browser, bgPage, teardown } = await harness.setup())
// wait for HTTPs to successfully load
await bgPage.waitForFunction(
() => window.dbg && dbg.https.isReady,
{ polling: 100, timeout: 20000 }
)
})
afterAll(async () => {
try {
await teardown()
} catch (e) {}
})
tests.forEach(test => {
it(`${test.url} should be graded as a ${test.siteGrade} -> ${test.enhancedGrade}`, async () => {
const page = await browser.newPage()
const ua = await browser.userAgent()
await page.setUserAgent(ua.replace(/Headless /, ''))
try {
await page.goto(`http://${test.url}`, { waitUntil: 'networkidle0' })
// give it another second just to be sure
await page.waitForTimeout(1000)
} catch (e) {
// timed out waiting for page to load, let's try running the test anyway
console.log(`Timed out: ${e}`)
}
const grades = await bgPage.evaluate(getGradeByUrl, test.url)
checkGrade(grades.site.grade, test.siteGrade)
checkGrade(grades.enhanced.grade, test.enhancedGrade)
try {
await page.close()
} catch (e) {}
})
})
})