Skip to content

Commit 8fcfb30

Browse files
committed
Code refactor
1 parent ab349a7 commit 8fcfb30

10 files changed

Lines changed: 536 additions & 454 deletions

File tree

packages/nightwatch-devtools/example/nightwatch.conf.cjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Simple import - just require the package
2-
const nightwatchDevtools = require('@wdio/nightwatch-devtools').default;
2+
const nightwatchDevtools = require('@wdio/nightwatch-devtools').default
33

44
module.exports = {
55
src_folders: ['example/tests'],
6-
6+
output_folder: false, // Skip generating nightwatch reports for this example
77
// Add custom reporter to capture commands
88
custom_commands_path: [],
99
custom_assertions_path: [],
@@ -29,4 +29,4 @@ module.exports = {
2929
globals: nightwatchDevtools({ port: 3000 })
3030
}
3131
}
32-
};
32+
}

packages/nightwatch-devtools/nightwatch.conf.cjs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ module.exports = {
2828

2929
// See https://nightwatchjs.org/guide/extending-nightwatch/adding-plugins.html
3030
plugins: [],
31-
31+
3232
// See https://nightwatchjs.org/guide/concepts/test-globals.html#external-test-globals
33-
globals_path : '',
33+
globals_path: '',
3434

3535
// Set this to true to disable bounding boxes on terminal output. Useful when running in some CI environments.
3636
disable_output_boxes: false,
@@ -54,7 +54,7 @@ module.exports = {
5454
},
5555

5656
desiredCapabilities: {
57-
browserName : 'firefox'
57+
browserName: 'firefox'
5858
},
5959

6060
webdriver: {
@@ -64,8 +64,8 @@ module.exports = {
6464
},
6565

6666
safari: {
67-
desiredCapabilities : {
68-
browserName : 'safari',
67+
desiredCapabilities: {
68+
browserName: 'safari',
6969
alwaysMatch: {
7070
acceptInsecureCerts: false
7171
}
@@ -77,8 +77,8 @@ module.exports = {
7777
},
7878

7979
firefox: {
80-
desiredCapabilities : {
81-
browserName : 'firefox',
80+
desiredCapabilities: {
81+
browserName: 'firefox',
8282
acceptInsecureCerts: true,
8383
'moz:firefoxOptions': {
8484
args: [
@@ -98,9 +98,9 @@ module.exports = {
9898
},
9999

100100
chrome: {
101-
desiredCapabilities : {
102-
browserName : 'chrome',
103-
'goog:chromeOptions' : {
101+
desiredCapabilities: {
102+
browserName: 'chrome',
103+
'goog:chromeOptions': {
104104
// More info on Chromedriver: https://sites.google.com/a/chromium.org/chromedriver/
105105
//
106106
// w3c:false tells Chromedriver to run using the legacy JSONWire protocol (not required in Chrome 78)
@@ -124,9 +124,9 @@ module.exports = {
124124
},
125125

126126
edge: {
127-
desiredCapabilities : {
128-
browserName : 'MicrosoftEdge',
129-
'ms:edgeOptions' : {
127+
desiredCapabilities: {
128+
browserName: 'MicrosoftEdge',
129+
'ms:edgeOptions': {
130130
w3c: true,
131131
// More info on EdgeDriver: https://docs.microsoft.com/en-us/microsoft-edge/webdriver-chromium/capabilities-edge-options
132132
args: [
@@ -162,7 +162,8 @@ module.exports = {
162162
// define cucumber specific options
163163
options: {
164164
//set the feature path
165-
feature_path: 'node_modules/nightwatch/examples/cucumber-js/*/*.feature',
165+
feature_path:
166+
'node_modules/nightwatch/examples/cucumber-js/*/*.feature'
166167

167168
// start the webdriver session automatically (enabled by default)
168169
// auto_start_session: true
@@ -189,9 +190,9 @@ module.exports = {
189190
// More info on configuring capabilities can be found on:
190191
// https://www.browserstack.com/automate/capabilities?tag=selenium-4
191192
desiredCapabilities: {
192-
'bstack:options' : {
193+
'bstack:options': {
193194
userName: '${BROWSERSTACK_USERNAME}',
194-
accessKey: '${BROWSERSTACK_ACCESS_KEY}',
195+
accessKey: '${BROWSERSTACK_ACCESS_KEY}'
195196
}
196197
},
197198

@@ -217,7 +218,7 @@ module.exports = {
217218
extends: 'browserstack',
218219
desiredCapabilities: {
219220
browserName: 'chrome',
220-
chromeOptions : {
221+
chromeOptions: {
221222
w3c: true
222223
}
223224
}
@@ -273,7 +274,7 @@ module.exports = {
273274
// More info on configuring capabilities can be found on:
274275
// https://docs.saucelabs.com/dev/test-configuration-options/
275276
desiredCapabilities: {
276-
'sauce:options' : {
277+
'sauce:options': {
277278
username: '${SAUCE_USERNAME}',
278279
accessKey: '${SAUCE_ACCESS_KEY}',
279280
screenResolution: '1280x1024'
@@ -297,7 +298,7 @@ module.exports = {
297298
javascriptEnabled: true,
298299
acceptSslCerts: true,
299300
timeZone: 'London',
300-
chromeOptions : {
301+
chromeOptions: {
301302
w3c: true
302303
}
303304
}
@@ -339,7 +340,7 @@ module.exports = {
339340
extends: 'selenium_server',
340341
desiredCapabilities: {
341342
browserName: 'chrome',
342-
chromeOptions : {
343+
chromeOptions: {
343344
w3c: true
344345
}
345346
}
@@ -358,4 +359,4 @@ module.exports = {
358359
}
359360
}
360361
}
361-
};
362+
}

packages/nightwatch-devtools/src/helpers/browserProxy.ts

Lines changed: 88 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ export class BrowserProxy {
5959
const originalUrl = browser.url.bind(browser)
6060
const sessionCapturer = this.sessionCapturer
6161

62-
browser.url = function(url: string) {
62+
browser.url = function (url: string) {
6363
const result = originalUrl(url) as any
6464

6565
if (result && typeof result.perform === 'function') {
66-
result.perform(async function(this: any) {
66+
result.perform(async function (this: any) {
6767
try {
6868
log.info(`Injecting script after navigation to: ${url}`)
6969
await sessionCapturer.injectScript(this)
@@ -83,7 +83,9 @@ export class BrowserProxy {
8383
* Wrap all browser commands to capture them
8484
*/
8585
wrapBrowserCommands(browser: NightwatchBrowser): void {
86-
if (this.browserProxied) return
86+
if (this.browserProxied) {
87+
return
88+
}
8789

8890
const browserAny = browser as any
8991
const allMethods = new Set([
@@ -92,19 +94,31 @@ export class BrowserProxy {
9294
])
9395
const wrappedMethods: string[] = []
9496

95-
allMethods.forEach(methodName => {
96-
if (methodName === 'constructor' || typeof browserAny[methodName] !== 'function') {
97+
allMethods.forEach((methodName) => {
98+
if (
99+
methodName === 'constructor' ||
100+
typeof browserAny[methodName] !== 'function'
101+
) {
97102
return
98103
}
99104

100-
if (INTERNAL_COMMANDS_TO_IGNORE.includes(methodName as any) || methodName.startsWith('__')) {
105+
if (
106+
INTERNAL_COMMANDS_TO_IGNORE.includes(methodName as any) ||
107+
methodName.startsWith('__')
108+
) {
101109
return
102110
}
103111

104112
const originalMethod = browserAny[methodName].bind(browser)
105113

106114
browserAny[methodName] = (...args: any[]) => {
107-
return this.handleCommandExecution(browser, browserAny, methodName, originalMethod, args)
115+
return this.handleCommandExecution(
116+
browser,
117+
browserAny,
118+
methodName,
119+
originalMethod,
120+
args
121+
)
108122
}
109123

110124
wrappedMethods.push(methodName)
@@ -126,7 +140,9 @@ export class BrowserProxy {
126140
): any {
127141
// Detect test boundaries
128142
const currentNightwatchTest = browserAny.currentTest
129-
const currentTestName = this.testManager.detectTestBoundary(currentNightwatchTest)
143+
const currentTestName = this.testManager.detectTestBoundary(
144+
currentNightwatchTest
145+
)
130146

131147
// Start test if this is its first command
132148
this.testManager.startTestIfPending(currentTestName)
@@ -138,7 +154,11 @@ export class BrowserProxy {
138154
}
139155

140156
// Check for duplicate commands
141-
const cmdSig = JSON.stringify({ command: methodName, args, src: callInfo.callSource })
157+
const cmdSig = JSON.stringify({
158+
command: methodName,
159+
args,
160+
src: callInfo.callSource
161+
})
142162
const isDuplicate = this.lastCommandSig === cmdSig
143163

144164
if (!isDuplicate) {
@@ -155,16 +175,29 @@ export class BrowserProxy {
155175

156176
// Capture command after execution
157177
const stackFrame = this.commandStack[this.commandStack.length - 1]
158-
if (stackFrame?.command === methodName && stackFrame.signature === cmdSig) {
178+
if (
179+
stackFrame?.command === methodName &&
180+
stackFrame.signature === cmdSig
181+
) {
159182
this.commandStack.pop()
160-
this.captureCommandResult(methodName, args, result, callInfo.callSource, browser, browserAny)
183+
this.captureCommandResult(
184+
methodName,
185+
args,
186+
result,
187+
callInfo.callSource,
188+
browser,
189+
browserAny
190+
)
161191
}
162192

163193
return result
164194
} catch (error) {
165195
// Capture command error
166196
const stackFrame = this.commandStack[this.commandStack.length - 1]
167-
if (stackFrame?.command === methodName && stackFrame.signature === cmdSig) {
197+
if (
198+
stackFrame?.command === methodName &&
199+
stackFrame.signature === cmdSig
200+
) {
168201
this.commandStack.pop()
169202
this.captureCommandError(methodName, args, error, callInfo.callSource)
170203
}
@@ -185,12 +218,16 @@ export class BrowserProxy {
185218
browserAny: any
186219
): void {
187220
const currentTest = this.getCurrentTest()
188-
if (!currentTest) return
221+
if (!currentTest) {
222+
return
223+
}
189224

190225
// Serialize result
191226
let serializedResult: any = undefined
192227
const isBrowserObject = result === browser || result === browserAny
193-
const isChainableAPI = result && typeof result === 'object' &&
228+
const isChainableAPI =
229+
result &&
230+
typeof result === 'object' &&
194231
('queue' in result || 'sessionId' in result || 'capabilities' in result)
195232

196233
if (isBrowserObject || isChainableAPI) {
@@ -211,16 +248,23 @@ export class BrowserProxy {
211248
}
212249

213250
// Capture and send command immediately
214-
this.sessionCapturer.captureCommand(
215-
methodName,
216-
args,
217-
serializedResult,
218-
undefined,
219-
currentTest.uid,
220-
callSource
221-
).catch((err: any) => log.error(`Failed to capture ${methodName}: ${err.message}`))
222-
223-
const lastCommand = this.sessionCapturer.commandsLog[this.sessionCapturer.commandsLog.length - 1]
251+
this.sessionCapturer
252+
.captureCommand(
253+
methodName,
254+
args,
255+
serializedResult,
256+
undefined,
257+
currentTest.uid,
258+
callSource
259+
)
260+
.catch((err: any) =>
261+
log.error(`Failed to capture ${methodName}: ${err.message}`)
262+
)
263+
264+
const lastCommand =
265+
this.sessionCapturer.commandsLog[
266+
this.sessionCapturer.commandsLog.length - 1
267+
]
224268
if (lastCommand) {
225269
this.sessionCapturer.sendCommand(lastCommand)
226270
}
@@ -236,18 +280,27 @@ export class BrowserProxy {
236280
callSource: string | undefined
237281
): void {
238282
const currentTest = this.getCurrentTest()
239-
if (!currentTest) return
240-
241-
this.sessionCapturer.captureCommand(
242-
methodName,
243-
args,
244-
undefined,
245-
error instanceof Error ? error : new Error(String(error)),
246-
currentTest.uid,
247-
callSource
248-
).catch((err: any) => log.error(`Failed to capture ${methodName}: ${err.message}`))
283+
if (!currentTest) {
284+
return
285+
}
249286

250-
const lastCommand = this.sessionCapturer.commandsLog[this.sessionCapturer.commandsLog.length - 1]
287+
this.sessionCapturer
288+
.captureCommand(
289+
methodName,
290+
args,
291+
undefined,
292+
error instanceof Error ? error : new Error(String(error)),
293+
currentTest.uid,
294+
callSource
295+
)
296+
.catch((err: any) =>
297+
log.error(`Failed to capture ${methodName}: ${err.message}`)
298+
)
299+
300+
const lastCommand =
301+
this.sessionCapturer.commandsLog[
302+
this.sessionCapturer.commandsLog.length - 1
303+
]
251304
if (lastCommand) {
252305
this.sessionCapturer.sendCommand(lastCommand)
253306
}

0 commit comments

Comments
 (0)