-
-
Notifications
You must be signed in to change notification settings - Fork 737
/
Copy pathsession_test.js
238 lines (210 loc) · 7.24 KB
/
session_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
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
const assert = require('assert')
const { event } = codeceptjs
Feature('Session')
Scenario('simple session @Puppeteer @Playwright', ({ I }) => {
I.amOnPage('/info')
session('john', () => {
I.amOnPage('/login')
I.dontSeeInCurrentUrl('/info')
I.see('Email')
})
I.dontSee('Email')
I.seeInCurrentUrl('/info')
})
Scenario('screenshots reflect the current page of current session @Puppeteer @Playwright', async ({ I }) => {
I.amOnPage('/')
I.saveScreenshot('session_default_1.png')
session('john', () => {
I.amOnPage('/info')
I.saveScreenshot('session_john_1.png')
})
I.saveScreenshot('session_default_2.png')
session('john', () => {
I.saveScreenshot('session_john_2.png')
})
const [default1Digest, default2Digest, john1Digest, john2Digest] = await I.getSHA256Digests([
`${output_dir}/session_default_1.png`,
`${output_dir}/session_default_2.png`,
`${output_dir}/john_session_john_1.png`,
`${output_dir}/session_john_2.png`,
])
// Assert that screenshots of same page in same session are equal
I.expectEqual(default1Digest, default2Digest)
I.expectEqual(john1Digest, john2Digest)
// Assert that screenshots of different pages in different sessions are not equal
I.expectNotEqual(default1Digest, john1Digest)
})
Scenario('Different cookies for different sessions @Playwright @Puppeteer', async ({ I }) => {
const cookiePage = 'https://www.microsoft.com/en-au/'
const cookieName = 'MUID'
const cookies = {}
I.amOnPage(cookiePage)
session('john', () => {
I.amOnPage(cookiePage)
})
session('mary', () => {
I.amOnPage(cookiePage)
})
cookies.default = (await I.grabCookie(cookieName)).value
I.say(`${cookieName}: ${cookies.default}`)
session('john', async () => {
cookies.john = (await I.grabCookie(cookieName)).value
I.say(`${cookieName}: ${cookies.john}`)
})
session('mary', async () => {
cookies.mary = (await I.grabCookie(cookieName)).value
I.say(`${cookieName}: ${cookies.mary}`)
})
await I.seeInCurrentUrl('en-au')
assert(cookies.default)
assert(cookies.john)
assert(cookies.mary)
I.expectNotEqual(cookies.default, cookies.john)
I.expectNotEqual(cookies.default, cookies.mary)
I.expectNotEqual(cookies.john, cookies.mary)
})
Scenario('should save screenshot for sessions @Puppeteer @Playwright', async function ({ I }) {
await I.amOnPage('/form/bug1467')
await I.saveScreenshot('original.png')
await I.amOnPage('/')
session('john', async () => {
await I.amOnPage('/form/bug1467')
event.dispatcher.emit(event.test.failed, this)
})
const fileName = clearString(this.title)
const [original, failed] = await I.getSHA256Digests([`${output_dir}/original.png`, `${output_dir}/john_${fileName}.failed.png`])
// Assert that screenshots of same page in same session are equal
await I.expectEqual(original, failed)
// Assert that screenshots of sessions are created
const [main_original] = await I.getSHA256Digests([`${output_dir}/${fileName}.failed.png`])
await I.expectNotEqual(main_original, failed)
})
Scenario('should throw exception and close correctly @Puppeteer @Playwright', ({ I }) => {
I.amOnPage('/form/bug1467#session1')
I.checkOption('Yes')
session('john', () => {
I.amOnPage('/form/bug1467#session2')
I.checkOption('No1')
I.seeCheckboxIsChecked({ css: 'input[value=No]' })
})
I.seeCheckboxIsChecked({ css: 'input[value=Yes]' })
I.amOnPage('/info')
}).fails()
Scenario('async/await', ({ I }) => {
I.amOnPage('/form/bug1467#session1')
I.checkOption('Yes')
session('john', async () => {
I.amOnPage('/form/bug1467#session2')
I.checkOption('No')
I.seeCheckboxIsChecked({ css: 'input[value=No]' })
})
I.seeCheckboxIsChecked({ css: 'input[value=Yes]' })
})
Scenario('exception on async/await @Puppeteer @Playwright', ({ I }) => {
I.amOnPage('/form/bug1467#session1')
I.checkOption('Yes')
session('john', async () => {
I.amOnPage('/form/bug1467#session2')
I.checkOption('No')
I.seeCheckboxIsChecked({ css: 'input[value=Yes]' })
})
I.seeCheckboxIsChecked({ css: 'input[value=Yes]' })
}).throws(/to be checked/)
Scenario('should work with within @Puppeteer @Playwright', ({ I }) => {
I.amOnPage('/form/bug1467')
session('john', () => {
I.amOnPage('/form/bug1467')
within({ css: '[name=form1]' }, () => {
I.checkOption('Yes')
I.seeCheckboxIsChecked({ css: 'input[name=first_test_radio]' })
})
})
within({ css: '[name=form2]' }, () => {
I.checkOption('Yes')
I.seeCheckboxIsChecked({ css: 'input[name=first_test_radio]' })
})
I.seeCheckboxIsChecked({ css: 'form[name=form2] input[name=first_test_radio]' })
I.dontSeeCheckboxIsChecked({ css: 'form[name=form1] input[name=first_test_radio]' })
session('john', () => {
I.seeCheckboxIsChecked({ css: 'form[name=form1] input[name=first_test_radio]' })
I.dontSeeCheckboxIsChecked({ css: 'form[name=form2] input[name=first_test_radio]' })
})
})
Scenario('change page emulation @Playwright', async ({ I }) => {
const assert = require('assert')
I.amOnPage('/')
session(
'mobile user',
{
viewport: { width: 300, height: 500 },
},
async () => {
I.amOnPage('/')
const width = await I.executeScript('window.innerWidth')
I.expectEqual(width, 300)
},
)
})
Scenario('emulate iPhone @Playwright', async ({ I }) => {
const { devices } = require('playwright')
if (process.env.BROWSER === 'firefox') return
const assert = require('assert')
I.amOnPage('/')
session('mobile user', devices['iPhone 6'], async () => {
I.amOnPage('/')
const width = await I.executeScript('window.innerWidth')
assert.ok(width > 950 && width < 1000)
})
})
xScenario('should use different base URL @Puppeteer @Playwright', ({ I }) => {
// nah, that's broken
I.amOnPage('/')
I.see('Welcome to test app')
session('john', { url: 'https://github.com' }, () => {
I.amOnPage('/')
I.dontSee('Welcome to test app')
I.see('GitHub')
})
I.see('Welcome to test app')
})
xScenario('should start firefox', async ({ I }) => {
// requires firefox :)
I.amOnPage('/form/bug1467#session1')
I.checkOption('Yes')
session('john', { browser: 'firefox' }, async () => {
I.amOnPage('/form/bug1467#session2')
I.checkOption('No')
I.seeCheckboxIsChecked({ css: 'input[value=No]' })
const isChrome = await I.executeScript(() => !!window.chrome)
assert(!isChrome)
})
I.seeCheckboxIsChecked({ css: 'input[value=Yes]' })
const isChrome = await I.executeScript(() => !!window.chrome)
assert(isChrome)
})
Scenario('should return a value in @Puppeteer @Playwright', async ({ I }) => {
I.amOnPage('/form/textarea')
const val = await session('john', () => {
I.amOnPage('/info')
return I.grabTextFrom({ css: 'h1' })
})
I.fillField('Description', val)
I.click('Submit')
I.see('[description] => Information')
})
Scenario('should return a value @Puppeteer @Playwright in async', async ({ I }) => {
I.amOnPage('/form/textarea')
const val = await session('john', async () => {
I.amOnPage('/info')
return I.grabTextFrom({ css: 'h1' })
})
I.fillField('Description', val)
I.click('Submit')
I.see('[description] => Information')
})
function clearString(str) {
if (!str) return ''
/* Replace forbidden symbols in string
*/
return str.replace(/ /g, '_')
}