Repository navigation
Expand file tree
/
Copy pathexample.js
More file actions
100 lines (87 loc) · 3.42 KB
/
Copy pathexample.js
File metadata and controls
100 lines (87 loc) · 3.42 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
/**
* Example + config-sweep harness for @wdio/selenium-devtools (Mocha runner).
*
* Walk the live/trace ladder by editing ONLY the DevTools.configure({...})
* block below. The suite carries a passing pair, an always-failing test (for
* retain-on-failure), a wait that times out (a failed wait row), and a flaky
* fail-then-pass test with a per-test retry (for on-first-retry / attempt
* capture).
*
* Run from the package root: pnpm example:mocha
*/
import { strict as assert } from 'node:assert'
import { Builder, By, until } from 'selenium-webdriver'
import { DevTools } from '@wdio/selenium-devtools'
const MODE = ['live', 'telemetry'].includes(process.env.DEVTOOLS_MODE)
? process.env.DEVTOOLS_MODE
: 'trace'
// ── Config ladder — change this block per rung ──────────────────────────────
// 1 live: { mode: 'live' }
// 2 trace: { mode: 'trace' }
// 3 per-test:{ mode: 'trace', traceGranularity: 'test' }
// 4 fail: { mode: 'trace', traceGranularity: 'test', tracePolicy: 'retain-on-failure' }
// 5 retry: { mode: 'trace', traceGranularity: 'test', tracePolicy: 'on-first-retry' }
DevTools.configure({
// Trace by default (regen); DEVTOOLS_MODE=live flips to live for live-parity recording.
mode: MODE,
// Live and telemetry modes warn that they ignore these, so set them only for trace.
...(MODE === 'trace'
? { traceGranularity: 'session', tracePolicy: 'retain-on-first-failure' }
: {}),
headless: true
})
const BASE_URL = (
process.env.DEVTOOLS_EXAMPLE_BASE_URL || 'https://the-internet.herokuapp.com'
).replace(/\/+$/, '')
const LOGIN_URL = `${BASE_URL}/login`
// Survives Mocha's in-process retry so the flaky test fails once, then passes.
let flakyAttempts = 0
describe('selenium-devtools smoke test', function () {
let driver
before(async function () {
driver = await new Builder().forBrowser('chrome').build()
})
after(async function () {
if (driver) {
await driver.quit()
}
})
it('loads the login page and reads the heading', async function () {
await driver.get(LOGIN_URL)
await driver.sleep(1500)
const heading = await driver.wait(until.elementLocated(By.css('h2')), 10000)
const text = await heading.getText()
assert.equal(text, 'Login Page')
})
it('navigates and reads the page title', async function () {
await driver.get(BASE_URL)
await driver.sleep(1500)
const title = await driver.getTitle()
assert.match(title, /The Internet/)
})
it('fails on a wrong heading (retain-on-failure target)', async function () {
await driver.get(LOGIN_URL)
await driver.sleep(1000)
const heading = await driver.wait(until.elementLocated(By.css('h2')), 10000)
const text = await heading.getText()
assert.equal(text, 'This Is Not The Heading')
})
it('times out waiting for an element the page never renders (failed wait target)', async function () {
await driver.get(LOGIN_URL)
await driver.wait(
until.elementLocated(By.css('#devtools-never-rendered')),
1000
)
})
it('flaky: fails the first attempt, then passes (retry target)', async function () {
this.retries(1)
await driver.get(LOGIN_URL)
await driver.sleep(1000)
flakyAttempts += 1
if (flakyAttempts === 1) {
throw new Error('intentional first-attempt failure — should retry')
}
const title = await driver.getTitle()
assert.match(title, /The Internet/)
})
})