aboutsummaryrefslogtreecommitdiffstats
path: root/test/screens/new-ui.js
blob: e3ba7f6ab8d0c6f545dd6cc0d273d6c53801a0f4 (plain) (blame)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
const path = require('path')
const fs = require('fs')
const pify = require('pify')
const mkdirp = require('mkdirp')
const rimraf = require('rimraf')
const webdriver = require('selenium-webdriver')
const endOfStream = require('end-of-stream')
const clipboardy = require('clipboardy')
const Ethjs = require('ethjs')
const GIFEncoder = require('gifencoder')
const pngFileStream = require('png-file-stream')
const sizeOfPng = require('image-size/lib/types/png')
const By = webdriver.By
const localesIndex = require('../../app/_locales/index.json')
const { delay, buildChromeWebDriver, buildFirefoxWebdriver, installWebExt, getExtensionIdChrome, getExtensionIdFirefox } = require('../e2e/func')

const eth = new Ethjs(new Ethjs.HttpProvider('http://localhost:8545'))

let driver
let screenshotCount = 0

captureAllScreens()
.then(async () => {
  // build screenshots into gif
  console.log('building gif...')
  await generateGif()

  await driver.quit()
  process.exit()
})
.catch(async (err) => {
  try {
    console.error(err)
    verboseReportOnFailure({ title: 'something broke' })
  } catch (err) {
    console.error(err)
  }

  await driver.quit()
  process.exit(1)
})


async function captureAllScreens() {
  // common names
  let button
  let tabs
  let element

  await cleanScreenShotDir()

  const extPath = path.resolve('dist/chrome')
  driver = buildChromeWebDriver(extPath)
  const extensionId = await getExtensionIdChrome(driver)

  await driver.get(`chrome-extension://${extensionId}/home.html`)
  await delay(500)
  tabs = await driver.getAllWindowHandles()
  await driver.switchTo().window(tabs[0])
  await delay(1000)
  await setProviderType('localhost')
  await delay(300)

  // click try new ui
  await driver.findElement(By.css('#app-content > div > div.app-primary.from-right > div > div.flex-row.flex-center.flex-grow > p')).click()
  await delay(300)

  // close metamask homepage and extra home.html
  tabs = await driver.getAllWindowHandles()
  // metamask homepage is opened on prod, not dev
  if (tabs.length > 2) {
    await driver.switchTo().window(tabs[2])
    driver.close()
  }
  await driver.switchTo().window(tabs[1])
  driver.close()
  await driver.switchTo().window(tabs[0])
  await delay(300)
  await captureLanguageScreenShots('welcome-new-ui')

  // setup account
  await delay(1000)
  await driver.findElement(By.css('body')).click()
  await delay(300)
  await captureLanguageScreenShots('welcome')

  await driver.findElement(By.css('button')).click()
  await captureLanguageScreenShots('create password')

  const password = '123456789'
  const passwordBox = await driver.findElement(By.css('input#create-password'))
  const passwordBoxConfirm = await driver.findElement(By.css('input#confirm-password'))
  passwordBox.sendKeys(password)
  passwordBoxConfirm.sendKeys(password)
  await delay(500)
  await captureLanguageScreenShots('choose-password-filled')

  await driver.findElement(By.css('button')).click()
  await delay(500)
  await captureLanguageScreenShots('unique account image')

  await driver.findElement(By.css('button')).click()
  await delay(500)
  await captureLanguageScreenShots('privacy note')

  await driver.findElement(By.css('button')).click()
  await delay(300)
  await captureLanguageScreenShots('terms')

  await delay(300)
  element = driver.findElement(By.linkText('Attributions'))
  await driver.executeScript('arguments[0].scrollIntoView(true)', element)
  await delay(300)
  await captureLanguageScreenShots('terms-scrolled')

  await driver.findElement(By.css('button')).click()
  await delay(300)
  await captureLanguageScreenShots('secret backup phrase')

  await driver.findElement(By.css('button')).click()
  await delay(300)
  await captureLanguageScreenShots('secret backup phrase')

  await driver.findElement(By.css('.backup-phrase__reveal-button')).click()
  await delay(300)
  await captureLanguageScreenShots('secret backup phrase - reveal')

  const seedPhrase = await driver.findElement(By.css('.backup-phrase__secret-words')).getText()
  const seedPhraseWords = seedPhrase.split(' ')
  await driver.findElement(By.css('button')).click()
  await delay(300)
  await captureLanguageScreenShots('confirm secret backup phrase')

  // enter seed phrase
  const seedPhraseButtons = await driver.findElements(By.css('.backup-phrase__confirm-seed-options > button'))
  const seedPhraseButtonWords = await Promise.all(seedPhraseButtons.map(button => button.getText()))
  for (let targetWord of seedPhraseWords) {
    const wordIndex = seedPhraseButtonWords.indexOf(targetWord)
    if (wordIndex === -1) throw new Error(`Captured seed phrase word "${targetWord}" not in found seed phrase button options ${seedPhraseButtonWords.join(' ')}`)
    await driver.findElement(By.css(`.backup-phrase__confirm-seed-options > button:nth-child(${wordIndex+1})`)).click()
    await delay(100)
  }
  await captureLanguageScreenShots('confirm secret backup phrase - words selected correctly')

  await driver.findElement(By.css('.backup-phrase__content-wrapper .first-time-flow__button')).click()
  await delay(300)
  await captureLanguageScreenShots('metamask post-initialize greeter screen deposit ether')

  await driver.findElement(By.css('.page-container__header-close')).click()
  await delay(300)
  await captureLanguageScreenShots('metamask account main screen')

  // account details + export private key
  await driver.findElement(By.css('.wallet-view__name-container > .wallet-view__details-button')).click()
  await delay(300)
  await captureLanguageScreenShots('metamask account detail screen')

  await driver.findElement(By.css('.account-modal__button:nth-of-type(2)')).click()
  await delay(300)
  await captureLanguageScreenShots('metamask account detail export private key screen - initial')

  await driver.findElement(By.css('.private-key-password > input')).sendKeys(password)
  await delay(300)
  await captureLanguageScreenShots('metamask account detail export private key screen - password entered')

  await driver.findElement(By.css('.btn-primary.btn--large.export-private-key__button')).click()
  await delay(300)
  await captureLanguageScreenShots('metamask account detail export private key screen - reveal key')

  await driver.findElement(By.css('.export-private-key__button')).click()
  await delay(300)
  await captureLanguageScreenShots('metamask account detail export private key screen - done')

  // get eth from Ganache
  // const viewAddressButton = await driver.findElement(By.css('.wallet-view__address'))
  // await driver.actions({ bridge: true }).move({ origin: viewAddressButton }).perform()
  // console.log('driver.actions', driver.actions({ bridge: true }))
  // await delay(300)
  // await captureLanguageScreenShots('metamask home - hover copy address')

  await driver.findElement(By.css('.wallet-view__address')).click()
  await delay(100)
  await captureLanguageScreenShots('metamask home - hover copy address')

  const primaryAddress = clipboardy.readSync()
  await requestEther(primaryAddress)
  // wait for block polling
  await delay(10000)
  await captureLanguageScreenShots('metamask home - has ether')

}


async function captureLanguageScreenShots(label) {
  const nonEnglishLocales = localesIndex.filter(localeMeta => localeMeta.code !== 'en')
  // take english shot
  await captureScreenShot(`${label} (en)`)
  for (let localeMeta of nonEnglishLocales) {
    // set locale and take shot
    await setLocale(localeMeta.code)
    await delay(300)
    await captureScreenShot(`${label} (${localeMeta.code})`)
  }
  // return locale to english
  await setLocale('en')
  await delay(300)
}

async function setLocale(code) {
  await driver.executeScript('window.metamask.updateCurrentLocale(arguments[0])', code)
}

async function setProviderType(type) {
  await driver.executeScript('window.metamask.setProviderType(arguments[0])', type)
}

async function cleanScreenShotDir() {
  await pify(rimraf)(`./test-artifacts/screens/`)
}

async function captureScreenShot(label) {
  const shotIndex = screenshotCount.toString().padStart(4, '0')
  screenshotCount++
  const artifactDir = `./test-artifacts/screens/`
  await pify(mkdirp)(artifactDir)
  // capture screenshot
  const screenshot = await driver.takeScreenshot()
  await pify(fs.writeFile)(`${artifactDir}/${shotIndex} - ${label}.png`, screenshot, { encoding: 'base64' })
}

async function generateGif(){
  // calculate screenshot size
  const screenshot = await driver.takeScreenshot()
  const pngBuffer = Buffer.from(screenshot, 'base64')
  const size = sizeOfPng.calculate(pngBuffer)

  // read only the english pngs into gif
  const encoder = new GIFEncoder(size.width, size.height)
  const stream = pngFileStream('./test-artifacts/screens/* (en).png')
    .pipe(encoder.createWriteStream({ repeat: 0, delay: 1000, quality: 10 }))
    .pipe(fs.createWriteStream('./test-artifacts/screens/walkthrough (en).gif'))

  // wait for end
  await pify(endOfStream)(stream)
}

async function verboseReportOnFailure(test) {
  const artifactDir = `./test-artifacts/${test.title}`
  const filepathBase = `${artifactDir}/test-failure`
  await pify(mkdirp)(artifactDir)
  // capture screenshot
  const screenshot = await driver.takeScreenshot()
  await pify(fs.writeFile)(`${filepathBase}-screenshot.png`, screenshot, { encoding: 'base64' })
  // capture dom source
  const htmlSource = await driver.getPageSource()
  await pify(fs.writeFile)(`${filepathBase}-dom.html`, htmlSource)
}

async function requestEther(address) {
  const accounts = await eth.accounts()
  await eth.sendTransaction({ from: accounts[0], to: address, value: 1 * 1e18, data: '0x0' })
}