-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathnetwork_events.spec.js
110 lines (88 loc) · 3.73 KB
/
network_events.spec.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
const assert = require("assert");
const firefox = require('selenium-webdriver/firefox');
const { Network } = require("selenium-webdriver/bidi/network");
const {until, Builder} = require("selenium-webdriver");
describe('Network events', function () {
let driver
beforeEach(async function () {
driver = new Builder()
.forBrowser('firefox')
.setFirefoxOptions(new firefox.Options().enableBidi())
.build()
})
afterEach(async function () {
await driver.quit()
})
it('can listen to event before request is sent', async function () {
let beforeRequestEvent = null
const network = await Network(driver)
await network.beforeRequestSent(function (event) {
beforeRequestEvent = event
})
await driver.get('https://www.selenium.dev/selenium/web/blank.html')
assert.equal(beforeRequestEvent.request.method, 'GET')
const url = beforeRequestEvent.request.url
assert.equal(url, await driver.getCurrentUrl())
})
it('can request cookies', async function () {
const network = await Network(driver)
let beforeRequestEvent = null
await network.beforeRequestSent(function (event) {
beforeRequestEvent = event
})
await driver.get('https://www.selenium.dev/selenium/web/blank.html')
await driver.manage().addCookie({
name: 'north',
value: 'biryani',
})
await driver.navigate().refresh()
assert.equal(beforeRequestEvent.request.method, 'GET')
assert.equal(beforeRequestEvent.request.cookies[0].name, 'north')
assert.equal(beforeRequestEvent.request.cookies[0].value.value, 'biryani')
const url = beforeRequestEvent.request.url
assert.equal(url, await driver.getCurrentUrl())
await driver.manage().addCookie({
name: 'south',
value: 'dosa',
})
await driver.navigate().refresh()
assert.equal(beforeRequestEvent.request.cookies[1].name, 'south')
assert.equal(beforeRequestEvent.request.cookies[1].value.value, 'dosa')
})
it('can redirect http equiv', async function () {
let beforeRequestEvent = []
const network = await Network(driver)
await network.beforeRequestSent(function (event) {
beforeRequestEvent.push(event)
})
await driver.get('http://www.selenium.dev/selenium/web/bidi/redirected_http_equiv.html')
await driver.wait(until.urlContains('redirected.html'), 1000)
assert.equal(beforeRequestEvent[0].request.method, 'GET')
assert(beforeRequestEvent[0].request.url.includes('redirected_http_equiv.html'))
assert.equal(beforeRequestEvent[2].request.method, 'GET')
assert(beforeRequestEvent[3].request.url.includes('redirected.html'))
})
it('can subscribe to response started', async function () {
let onResponseStarted = []
const network = await Network(driver)
await network.responseStarted(function (event) {
onResponseStarted.push(event)
})
await driver.get('https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html')
assert.equal(onResponseStarted[0].request.method, 'GET')
assert.equal(onResponseStarted[0].request.url, await driver.getCurrentUrl())
assert.equal(onResponseStarted[0].response.url, await driver.getCurrentUrl())
})
it('can subscribe to response completed', async function () {
let onResponseCompleted = []
const network = await Network(driver)
await network.responseCompleted(function (event) {
onResponseCompleted.push(event)
})
await driver.get('https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html')
assert.equal(onResponseCompleted[0].request.method, 'GET')
assert.equal(onResponseCompleted[0].request.url, await driver.getCurrentUrl())
assert.equal(onResponseCompleted[0].response.fromCache, false)
assert.equal(onResponseCompleted[0].response.status, 200)
})
})