-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.js
149 lines (119 loc) · 4.99 KB
/
test.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* eslint-env node, mocha */
const puppeteer = require( 'puppeteer' )
const assert = require( 'assert' )
const path = require( 'path' )
const manifest = require( '../manifest' )
const extensionPath = path.join( __dirname, '..' )
const chromiumOptions = {
headless: false,
slowMo: 200,
args: [
'--no-sandbox',
`--disable-extensions-except=${extensionPath}`,
`--load-extension=${extensionPath}`
]
}
/**
* Delays the execution for a certain duration
* @param {number} t duration in ms
*/
const delay = t => new Promise( resolve => setTimeout( resolve, t ) )
let browser
let page
let client
before( 'start browser and extension', async function() {
this.enableTimeouts( false )
browser = await puppeteer.launch( chromiumOptions )
// hack, waits until targets are ready
await delay( 2000 )
// see https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#class-target
let extensionTarget = browser.targets()
.find( t => t._targetInfo.title === manifest.name && t._targetInfo.type === 'background_page' )
assert.ok( extensionTarget, 'could not find the extension target' )
client = await extensionTarget.createCDPSession()
} )
after( 'detach extension and close browser', async function() {
if ( client ) await client.detach()
await browser.close()
} )
beforeEach( 'open new page', async function() {
page = await browser.newPage()
} )
afterEach( 'close used page', async function() {
await page.close()
} )
describe( 'extension loaded', function() {
it( 'should display the correct translation for specific country code', async function() {
this.timeout( 25000 )
await chromeStorageSet( { i18nLang: 'pt-BR', i18nMode: 'Load' }, client )
await page.goto( 'https://trakt.tv/users/gervasiocaj/lists/trakttvstats-tests', { waitUntil: 'networkidle2' } )
let options = await chromeStorageGet( [ 'i18nLang' ], client )
assert.strictEqual( options.i18nLang, 'pt-BR' )
// FIXME consider multiple items in grid
// let maleficentMovie = await page.$('div[data-movie-id="73425"]')
let englishTitle = await page.$eval( '.titles h3', el => el.textContent )
assert.strictEqual( englishTitle.trim(), 'Maleficent' )
let translatedTitle = await page.$eval( '.thumb-title_secondary', el => el.textContent )
assert.strictEqual( translatedTitle.trim(), 'Malévola', 'translatedTitle not in pt-BR' )
assert.notStrictEqual( translatedTitle.trim(), 'Maléfica', 'translatedTitle in pt-PT' )
} )
it( 'should display the correct flag for specific country code', async function() {
this.timeout( 25000 )
async function getSecondaryTitleInfo( language, url ) {
await chromeStorageSet( { i18nLang: language }, client )
await page.goto( url, { waitUntil: 'networkidle2' } )
return page.$eval( '.page-title_secondary .info', el => el.textContent )
}
const movieUrl = 'https://trakt.tv/movies/moana-2016'
let countryInfo = await getSecondaryTitleInfo( 'pt', movieUrl )
assert.strictEqual( countryInfo, '(🇵🇹)', 'the subtitle flag for pt does not match' )
countryInfo = await getSecondaryTitleInfo( 'pt-BR', movieUrl )
assert.strictEqual( countryInfo, '(🇧🇷)', 'the subtitle flag for pt-BR does not match' )
countryInfo = await getSecondaryTitleInfo( 'pt-PT', movieUrl )
assert.strictEqual( countryInfo, '(🇵🇹)', 'the subtitle flag for pt-PT does not match' )
} )
} )
/**
* See https://developer.chrome.com/extensions/storage#method-StorageArea-get
* @param {?(string|string[]|object)} keys
* @param cdpSession A "Chrome DevTools Protocol" session
* @returns {Promise} Promise that resolves with items
*/
async function chromeStorageGet( keys, cdpSession ) {
let getItems = cdpSession.send( 'Runtime.evaluate', {
expression: `new Promise(function (resolve, reject) {
chrome.storage.sync.get(${JSON.stringify( keys )}, function (items) {
if (chrome.runtime.lastError) reject(chrome.runtime.lastError)
else resolve(items)
})
})`,
awaitPromise: true,
returnByValue: true,
silent: true
} )
try {
let items = await getItems
return items.result.value
} catch ( err ) {
return Promise.reject( err )
}
}
/**
* See https://developer.chrome.com/extensions/storage#method-StorageArea-set
* @param {Object} items
* @param cdpSession A "Chrome DevTools Protocol" session
* @returns {Promise} Promise with empty resolution
*/
async function chromeStorageSet( items, cdpSession ) {
return cdpSession.send( 'Runtime.evaluate', {
expression: `new Promise(function (resolve, reject) {
chrome.storage.sync.set(${JSON.stringify( items )}, function () {
if (chrome.runtime.lastError) reject(chrome.runtime.lastError)
else resolve()
})
})`,
awaitPromise: true,
returnByValue: true,
silent: true
} )
}