-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpoll.ts
More file actions
194 lines (163 loc) · 7.88 KB
/
Copy pathpoll.ts
File metadata and controls
194 lines (163 loc) · 7.88 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
import { idfy } from './generic'
import { expect } from '@playwright/test'
// Only works inside a poll
export async function fastForward(page: any, times = 1) {
await expect(page.locator('#poll-header-multiple-choices')).toBeVisible()
const ffButton = page.getByRole('button', { name: 'Fast Forward' })
for (let i = 0; i < times; i++) {
await page.waitForTimeout(500)
if (!(await ffButton.isVisible())) await page.locator('#poll-header-multiple-choices').click()
// If the poll has reached its final phase there is no Fast Forward button left;
// stop instead of hanging while waiting for a button that will never appear.
try {
await expect(ffButton).toBeVisible({ timeout: 3000 })
} catch {
break
}
await ffButton.click()
}
await page.locator('#poll-header-multiple-choices').click()
}
// Reload until the poll timeline reaches the expected phase. The phase advances
// server-side (via fastForward on another page), so a single reload races the
// change under load — reload-and-recheck instead of a fixed wait or networkidle.
export async function waitForPhase(page: any, phase: RegExp) {
await expect(async () => {
await page.reload()
await expect(
page.locator('#poll-timeline').filter({ hasText: phase }),
).toBeVisible({ timeout: 5000 })
}).toPass({ timeout: 30000 })
}
// Only works inside a group. One could use goToGroup before this
export async function createPoll(page: any, { title = 'Test Poll', date = false, phase_time = 1 } = {}) {
//Create a Poll
const createPostButton = page.getByRole('button', { name: 'Create a post' })
await expect(createPostButton).toBeVisible()
await createPostButton.click()
await expect(page.getByText('PollThread')).toBeVisible()
await page.getByLabel('Title').click()
await page.getByLabel('Title').fill(title)
await page.getByLabel('Description').fill('Test Description')
if (date) await page.getByLabel('Date Poll').check()
await page.getByRole('button', { name: 'Display advanced time settings' }).click()
await page.locator('fieldset').filter({ hasText: 'Public? Yes No' }).getByLabel('Yes').check()
await page.locator('fieldset').filter({ hasText: 'Fast Forward? Yes No' }).getByLabel('Yes').check()
await page.getByRole('spinbutton').fill(phase_time.toString())
await page.getByRole('button', { name: 'Post' }).click()
await expect(page.getByText('Poll Created')).toBeVisible()
await expect(page.getByText('Could not create Poll')).not.toBeVisible()
await page.waitForTimeout(500)
await expect(page.getByRole('heading', { name: title })).toBeVisible()
}
export async function goToPost(page: any, { title = 'Test Poll' }) {
await page.getByRole('button', { name: 'Home' }).click()
await page.getByPlaceholder('Search polls').click()
await page.getByPlaceholder('Search polls').fill(title)
await expect(
page.getByRole('button', { name: title, exact: true }).first(),
).toBeVisible()
await page.getByRole('button', { name: title, exact: true }).first().click()
await expect(page.getByRole('heading', { name: title })).toBeVisible()
// expect(await page.locator('#poll-thumbnail-140').getByRole('link', { name: 'Test Poll' })).toBeVisible();
}
export async function areaVote(page: any, { area = 'Default' } = {}) {
await page
.locator(`[id="tag-${idfy(area)}"]`)
.getByRole('radio')
.check()
await page.getByRole('button', { name: 'Submit' }).click()
await expect(page.getByText('Successfully voted for area')).toBeVisible()
}
//Only works in proposal phase
export async function createProposal(page: any, { title = 'Test Proposal', description = 'Test Description' } = {}) {
if (!(await page.getByRole('button', { name: 'Add Proposal' }).isDisabled()))
await page.getByRole('button', { name: 'Add Proposal' }).click()
// TODO: Move these tests into a proper propsal test so the cancel functionality isn't needlessly tested
// await page.getByRole('textbox', { name: 'Title' }).click()
// await page.getByRole('textbox', { name: 'Title' }).fill(title)
// await page.locator('#proposal-textarea').click()
// await page.locator('#proposal-textarea').fill(title)
// await page.getByRole('button', { name: 'Cancel' }).click()
// await page.getByRole('button', { name: 'Add Proposal' }).click()
const titleBox = page.getByRole('textbox', { name: 'Title' })
await expect(titleBox).toBeVisible()
await titleBox.fill(title)
await page.locator('#proposal-textarea').fill(description)
await page.getByRole('button', { name: 'Confirm' }).click()
await expect(page.getByText('Successfully added proposal').first()).toBeVisible()
}
export async function predictionStatementCreate(
page: any,
proposal = { title: 'Proposal Title' },
prediction = { title: 'Prediction Title' },
) {
await expect(page.locator('#poll-timeline').filter({ hasText: 'Phase 3. Prediction statements creation' })).toBeVisible()
await page.waitForTimeout(200)
const visible = await page.getByText('To make a consequence').isVisible()
if (visible)
await page
.locator(`#${idfy(proposal.title)}-selection`)
.first()
.click()
await expect(page.getByText('To make a consequence, please')).not.toBeVisible()
await page.getByRole('textbox', { name: 'Title' }).fill(prediction.title)
await page.getByRole('textbox', { name: 'Description' }).fill('Prediction 1')
await page.locator('.date-time-field > input').nth(0).fill('2000-01-01 00:00:00')
await page.locator('#poll-structure').click()
await page.getByRole('button', { name: 'Submit' }).click()
await expect(page.getByText('Successfully created').first()).toBeVisible()
await page
.locator(`#${idfy(proposal.title)}-selected`)
.first()
.click()
}
//Prediction Betting
export async function predictionProbability(
page: any,
proposal = { title: 'Proposal Title' },
prediction = { title: 'Prediction Title', vote: 1 },
) {
await expect(page.locator('#poll-timeline').filter({ hasText: 'Current: Phase 4. Consequence' })).toBeVisible()
await page
.locator(`#${idfy(proposal.title)}`)
.first()
.locator('button', { hasText: 'See More' })
.click()
await page.locator(`#track-container- > div:nth-child(${2 + prediction.vote})`).click()
await expect(page.getByText('Probability successfully sent').nth(0)).toBeVisible()
}
// Works for both delegate and normal voting
export async function vote(page: any, proposal = { title: 'Proposal Title', vote: 1 }) {
expect(proposal.vote >= 0 && proposal.vote <= 5, 'Vote must be between 0 and 5')
const track = page.locator(`#track-container-${idfy(proposal.title)}`)
// Wait for a voting phase (delegate or non-delegate) and the proposal's vote
// track to render, instead of a fixed sleep. Generous timeout: on CI the
// backend is slower to advance phases / propagate proposals than locally.
await expect(page.locator('#poll-timeline').filter({
hasText: /Delegate voting|Voting for non-delegates/
})).toBeVisible({ timeout: 15000 })
await expect(track).toBeVisible({ timeout: 15000 })
await track
.locator(`> div:nth-child(${2 + proposal.vote})`)
.first()
.click()
// await page.locator('#track-container-test-proposal > div:nth-child(4)').click();
// await page.locator("#proposals-section").screenshot({ path: 'tests/voting.png', fullPage: true });
// expect(await page.locator("#proposals-section")).toHaveScreenshot('tests/voting.png');
// await page.reload();
// await page.waitForLoadState('networkidle');
// expect(await page.locator("#proposals-section")).toHaveScreenshot('tests/voting.png');
}
export async function results(page: any) {
await expect(page.locator('#poll-timeline').filter({ hasText: 'Results' })).toBeVisible()
await expect(page.getByText('Results', { exact: true })).toBeVisible()
//TODO: no need for canvas when there have been 0 votes
await expect(page.locator('canvas')).toBeVisible()
await page.locator('canvas').click({
position: {
x: 43,
y: 92,
},
})
}