forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.ts
More file actions
206 lines (166 loc) · 6.62 KB
/
Copy pathcommon.ts
File metadata and controls
206 lines (166 loc) · 6.62 KB
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
import type { ElementHandle, Page } from 'puppeteer'
import { type LaunchContext } from '../common/base'
import { waitForMessage, waitForSuccMessage } from '../common/message'
import { sleep } from '../common/util'
const typeNames: Record<tt4b.backup.Type, string> = {
gist: 'gist',
web_dav: 'dav',
obsidian_local_rest_api: 'obsidian',
none: 'none',
}
const OPTION_TAB_ID = 'pane-backup'
async function waitClientReady(page: Page): Promise<ElementHandle<Element> | null> {
await page.waitForFunction(() => {
const emptyBody = document.querySelector('.el-dialog .el-table__empty-block')
const rows = document.querySelectorAll('.el-dialog .el-table .el-table__row')
return !!emptyBody || !!rows.length
}, { timeout: 1000 })
const nextBtn = await page.waitForSelector(
'.el-overlay:not([style*="display: none"]) .el-dialog .el-button.el-button--primary:not([disabled])',
{ timeout: 1000 },
)
return nextBtn
}
async function findCurrentClientRow(page: Page): Promise<ElementHandle<Element> | null> {
const table = await page.waitForSelector(
'.el-overlay:not([style*="display: none"]) .el-dialog .el-table',
{ timeout: 2000 },
)
const rows = await table!.$$('.el-table__row')
for (const row of rows) {
const currVisible = await row.evaluate(el => {
const tag = el.querySelector('.el-table__cell .el-tag')
if (!tag) return false
const text = tag.textContent
if (!text.toLowerCase().includes('current')) return false
const style = window.getComputedStyle(tag)
return style && style.display !== 'none'
})
if (currVisible) return row
}
return null
}
async function selectCurrentClient(page: Page): Promise<void> {
const currRow = await findCurrentClientRow(page)
expect(currRow).toBeTruthy()
await currRow!.click()
await sleep(.1)
}
export class BackupOptionWrapper {
private _page: Page | undefined
constructor(private context: LaunchContext) { }
private async page() {
if (this._page) {
await this._page.bringToFront()
return this._page
}
this._page = await this.context.openAppPage('/additional/option?i=backup')
return this._page
}
async $(selector: string) {
const page = await this.page()
return await page.$(`#${OPTION_TAB_ID} ${selector}`)
}
async changeType(type: tt4b.backup.Type) {
const page = await this.page()
const pane = await page.$(`#${OPTION_TAB_ID} .el-select`)
await pane?.click()
await sleep(.1)
const backupOptions = await page.$$('.el-popper[data-popper-reference-hidden="false"] .el-select-dropdown__item')
const labelPart = typeNames[type]
for (const option of backupOptions) {
const text = await option.evaluate(el => el.textContent)
if (text?.toLowerCase().includes(labelPart)) {
await option.click()
break
}
}
await sleep(.1)
return page
}
async assertTestInvalid() {
const page = await this.clickButton('test')
// The same as mock server
await waitForMessage(page, 'Unauthorized')
}
async assertTestValid() {
const page = await this.clickButton('test')
await waitForMessage(page, 'Valid!')
}
async assertBackupSuccess() {
const page = await this.clickButton('backup')
// Wait for the success message
await waitForSuccMessage(page)
const lastSyncTime = await this.findLastSyncTime()
expect(lastSyncTime).toBeTruthy()
// Less than 2 seconds
expect(Date.now() - lastSyncTime!.getTime()).toBeLessThan(2 * 1000)
}
private async clickButton(textPart: string): Promise<Page> {
const page = await this.page()
const buttons = await page.$$(`#${OPTION_TAB_ID} .el-button`)
for (const button of buttons) {
const text = await button.evaluate(el => el.textContent)
if (text?.toLowerCase().includes(textPart.toLowerCase())) {
await button.click()
return page
}
}
throw new Error(`Button with text "${textPart}" not found`)
}
private async findLastSyncTime() {
const page = await this.page()
const texts = await page.$$(`#${OPTION_TAB_ID} .el-text`)
for (const textEl of texts) {
const text = await textEl.evaluate(el => el.textContent)
const matchRes = /(?<M>\d{2})\/(?<d>\d{2})\/(?<y>\d{4}) (?<h>\d{2}):(?<m>\d{2}):(?<s>\d{2})/.exec(text ?? '')
if (!matchRes) continue
const groups = matchRes.groups
if (!groups) continue
const { M, d, y, h, m, s } = groups
if (!y || !M || !d || !h || !m || !s) continue
return new Date(
Number.parseInt(y), Number.parseInt(M) - 1, Number.parseInt(d),
Number.parseInt(h), Number.parseInt(m), Number.parseInt(s),
)
}
return undefined
}
async downloadCurrentWithAcc() {
// Open dialog
const page = await this.clickButton('download')
const nextBtn = await waitClientReady(page)
// Select current client row
await selectCurrentClient(page)
// Next step
await nextBtn!.click()
// Wait for download enabled
const downloadBtn = await page.waitForSelector('.el-dialog .el-button.el-button--success:not([disabled])', { timeout: 1000 })
// Choose the solution
const solutionRadio = await page.waitForSelector('.el-dialog .el-radio-group > label:nth-child(2)', { timeout: 1000 })
await solutionRadio!.click()
// Do downloading
await downloadBtn!.click()
await waitForSuccMessage(page)
}
async clearData() {
const page = await this.clickButton('clear')
const nextBtn = await waitClientReady(page)
// Select current client row
await selectCurrentClient(page)
// Next step
await nextBtn!.click()
// Wait for clear enabled
const clearBtn = await page.waitForSelector('.el-dialog .el-button.el-button--danger:not([disabled])', { timeout: 1000 })
await clearBtn!.click()
await waitForSuccMessage(page)
}
async assertCantDownloadCurr() {
// Open dialog
const page = await this.clickButton('download')
await waitClientReady(page)
// Select current client row
const currRow = await findCurrentClientRow(page)
expect(currRow).toBeFalsy()
}
}