-
Notifications
You must be signed in to change notification settings - Fork 306
/
selenium.js
69 lines (56 loc) · 2.03 KB
/
selenium.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
const { addHook, channel } = require('./helpers/instrument')
const shimmer = require('../../datadog-shimmer')
const ciSeleniumDriverGetStartCh = channel('ci:selenium:driver:get')
const RUM_STOP_SESSION_SCRIPT = `
if (window.DD_RUM && window.DD_RUM.stopSession) {
window.DD_RUM.stopSession();
return true;
} else {
return false;
}
`
const IS_RUM_ACTIVE_SCRIPT = 'return !!window.DD_RUM'
const DD_CIVISIBILITY_RUM_FLUSH_WAIT_MILLIS = 500
const DD_CIVISIBILITY_TEST_EXECUTION_ID_COOKIE_NAME = 'datadog-ci-visibility-test-execution-id'
// TODO: can we increase the supported version range?
addHook({
name: 'selenium-webdriver',
versions: ['>=4.11.0']
}, (seleniumPackage, seleniumVersion) => {
// TODO: do not turn this into async. Use promises
shimmer.wrap(seleniumPackage.WebDriver.prototype, 'get', get => async function () {
let traceId
const setTraceId = (inputTraceId) => {
traceId = inputTraceId
}
const getResult = await get.apply(this, arguments)
const isRumActive = await this.executeScript(IS_RUM_ACTIVE_SCRIPT)
const capabilities = await this.getCapabilities()
ciSeleniumDriverGetStartCh.publish({
setTraceId,
seleniumVersion,
browserName: capabilities.getBrowserName(),
browserVersion: capabilities.getBrowserVersion(),
isRumActive
})
await this.manage().addCookie({
name: DD_CIVISIBILITY_TEST_EXECUTION_ID_COOKIE_NAME,
value: traceId
})
return getResult
})
shimmer.wrap(seleniumPackage.WebDriver.prototype, 'quit', quit => async function () {
const isRumActive = await this.executeScript(RUM_STOP_SESSION_SCRIPT)
if (isRumActive) {
// We'll have time for RUM to flush the events (there's no callback to know when it's done)
await new Promise(resolve => {
setTimeout(() => {
resolve()
}, DD_CIVISIBILITY_RUM_FLUSH_WAIT_MILLIS)
})
}
await this.manage().deleteCookie(DD_CIVISIBILITY_TEST_EXECUTION_ID_COOKIE_NAME)
return quit.apply(this, arguments)
})
return seleniumPackage
})