Skip to content

Commit 29566b5

Browse files
author
DavertMik
committed
improved running bdd tests
1 parent 7ff7ca7 commit 29566b5

File tree

9 files changed

+102
-75
lines changed

9 files changed

+102
-75
lines changed

examples/custom_steps.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
module.exports = actor;
1+
import { actor } from 'codeceptjs'
2+
3+
export default actor

examples/fragments/Signin.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
let I;
1+
import { actor } from 'codeceptjs'
22

3-
module.exports = {
3+
let I
4+
5+
const signinMethods = {
46
_init() {
5-
I = actor();
7+
I = actor()
68
},
79

810
// insert your locators and methods here
9-
};
11+
}
12+
13+
export default signinMethods

examples/pages/Admin.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
let I;
1+
import { actor } from 'codeceptjs'
22

3-
module.exports = {
3+
let I
4+
5+
const adminMethods = {
46
_init() {
5-
I = actor();
7+
I = actor()
68
},
79

810
// insert your locators and methods here
9-
};
11+
}
12+
13+
export default adminMethods

examples/pages/Login.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
const I = actor();
1+
import { actor } from 'codeceptjs'
22

3-
module.exports = {
3+
const I = actor()
4+
5+
const loginMethods = {
46
login(email, password) {
5-
I.click('Sign in');
6-
I.fillField('Username or email address', email);
7-
I.fillField('Password', password);
8-
I.click('Sign in');
7+
I.click('Sign in')
8+
I.fillField('Username or email address', email)
9+
I.fillField('Password', password)
10+
I.click('Sign in')
911
},
10-
};
12+
}
13+
14+
Object.setPrototypeOf(loginMethods, class Login {}.prototype)
1115

12-
Object.setPrototypeOf(module.exports, class Login {}.prototype);
16+
export default loginMethods

examples/pages/Smth.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
const I = actor();
2-
const loginPage = require('./Login');
1+
import { actor } from 'codeceptjs'
2+
import loginPage from './Login.js'
33

4-
class Smth {}
4+
const I = actor()
55

6-
module.exports = {
6+
class Smth {}
77

8+
const smthMethods = {
89
openGitHub() {
9-
I.amOnPage('https://github.com');
10+
I.amOnPage('https://github.com')
1011
},
1112

1213
openAndLogin() {
13-
this.openGitHub();
14-
loginPage.login('[email protected]', '1234356');
14+
this.openGitHub()
15+
loginPage.login('[email protected]', '1234356')
1516
},
16-
};
17+
}
18+
19+
Object.setPrototypeOf(smthMethods, Smth.prototype)
1720

18-
Object.setPrototypeOf(module.exports, Smth.prototype);
21+
export default smthMethods

examples/step_definitions/steps.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
const assert = require('assert');
2-
const Smth = require('../pages/Smth');
1+
import assert from 'assert'
2+
import { actor } from 'codeceptjs'
3+
import Smth from '../pages/Smth.js'
34

4-
const I = actor();
5+
const I = actor()
56
// Add in your custom step files
67

78
Given('I have a defined step', () => {
89
// TODO: replace with your own step
9-
I.amOnPage('https://google.com');
10-
});
10+
console.log('Step executed: I have a defined step')
11+
})
1112

1213
Given('Open google', () => {
13-
// From "features\lawPage.feature" {"line":72,"column":7}
14-
I.amOnPage('https://www.google.ru/webhp?hl=ru&sa=X&ved=0ahUKEwjY7rWfrPjbAhUHFiwKHas5DCMQPAgD');
15-
});
14+
I.amOnPage('https://www.google.com')
15+
})
1616

1717
Given('I open GitHub', () => {
18-
Smth.openGitHub();
19-
I.dontSee('Ups');
20-
});
18+
Smth.openGitHub()
19+
I.dontSee('Ups')
20+
})
2121

2222
Then('check link', async () => {
2323
// From "features\lawPage.feature" {"line":73,"column":7}
24-
const link = await I.grabAttributeFrom({ css: '#gbw > div > div > div.gb_qe.gb_R.gb_Pg.gb_Fg > div:nth-child(2) > a' }, 'href');
25-
const response = await I.sendGetRequest(link);
26-
assert(response.statusCode === 200);
27-
I.see('Google');
28-
});
24+
const link = await I.grabAttributeFrom({ css: '#gbw > div > div > div.gb_qe.gb_R.gb_Pg.gb_Fg > div:nth-child(2) > a' }, 'href')
25+
const response = await I.sendGetRequest(link)
26+
assert(response.statusCode === 200)
27+
I.see('Google')
28+
})
2929

3030
When(/^I see "(.*)" text and "(.*)" is not "(.*)"$/, async (text, text2, text3) => {
31-
I.see(text);
32-
assert(text2 !== text3);
33-
});
31+
console.log(`Step executed: I see "${text}" text and "${text2}" is not "${text3}"`)
32+
assert(text2 !== text3)
33+
})

examples/user_helper.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
1-
const assert = require('assert');
1+
import assert from 'assert'
2+
import Helper from '@codeceptjs/helper'
23

34
class User extends Helper {
4-
_beforeSuite() {
5-
}
5+
_beforeSuite() {}
66

77
_before() {
8-
this.i = 0;
8+
this.i = 0
99
}
1010

1111
failNTimes(n) {
12-
this.i++;
12+
this.i++
1313
// this.i++;
14-
console.log(this.i, n);
15-
if (this.i < n) throw new Error('ups, error');
14+
console.log(this.i, n)
15+
if (this.i < n) throw new Error('ups, error')
1616
}
1717

1818
// add custom methods here
1919
// If you need to access other helpers
2020
// use: this.helpers['helperName']
2121
seeAuthentication() {
2222
return this.helpers.WebDriverIO.browser.cookie((err, res) => {
23-
const cookies = res.value;
23+
const cookies = res.value
2424
for (const k in cookies) {
25-
if (cookies[k].name !== 'logged_in') continue;
26-
assert.equal(cookies[k].value, 'yes');
27-
return;
25+
if (cookies[k].name !== 'logged_in') continue
26+
assert.equal(cookies[k].value, 'yes')
27+
return
2828
}
29-
assert.fail(cookies, 'logged_in', 'Auth cookie not set');
30-
});
29+
assert.fail(cookies, 'logged_in', 'Auth cookie not set')
30+
})
3131
}
3232
}
3333

34-
module.exports = User;
34+
export default User

lib/listener/helpers.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export default function () {
4242
})
4343

4444
event.dispatcher.on(event.test.before, test => {
45+
console.log('DEBUG: helpers listener received test.before for:', test.title)
4546
// schedule config to revert changes
4647
runAsyncHelpersHook('_before', test, true)
4748
recorder.catchWithoutStop(e => output.error(e))
@@ -75,4 +76,8 @@ export default function () {
7576
event.dispatcher.on(event.all.result, () => {
7677
runAsyncHelpersHook('_finishTest', {}, true)
7778
})
79+
80+
event.dispatcher.on(event.all.after, () => {
81+
runAsyncHelpersHook('_cleanup', {}, true)
82+
})
7883
}

lib/mocha/bdd.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { CucumberExpression, ParameterTypeRegistry, ParameterType } from '@cucumber/cucumber-expressions'
2+
import event from '../event.js'
23

34
let steps = {}
45
let Config
@@ -35,14 +36,14 @@ const addStep = async (step, fn) => {
3536
throw new Error(`Step '${step}' is already defined`)
3637
}
3738
steps[step] = fn
38-
39+
3940
// Use the current step file context if available (fallback for old usage)
4041
if (currentStepFile) {
4142
let relativePath = currentStepFile
42-
43+
4344
// Remove any leading './' and keep step_definitions/ path
4445
relativePath = relativePath.replace(/^\.\//, '').replace(/^.*\/(?=step_definitions)/, '')
45-
46+
4647
fn.line = `${relativePath}:3:1`
4748
} else {
4849
fn.line = 'unknown_file:1:1'
@@ -94,11 +95,11 @@ const buildParameterType = ({ name, regexp, transformer, useForSnippets, preferF
9495
}
9596

9697
// Create wrapper functions that capture the call context
97-
const createStepFunction = (stepType) => {
98+
const createStepFunction = stepType => {
9899
return (step, fn) => {
99100
// Capture the stack trace at the point where Given/When/Then is called
100101
const callStack = new Error().stack
101-
102+
102103
// Find the caller (step definition file) in the stack
103104
let callerInfo = 'unknown_file:1:1'
104105
if (callStack) {
@@ -115,7 +116,7 @@ const createStepFunction = (stepType) => {
115116
}
116117
}
117118
}
118-
119+
119120
// Instead of using global currentStepFile, pass the caller info directly to addStep
120121
return addStepWithCaller(step, fn, callerInfo)
121122
}
@@ -129,7 +130,7 @@ const addStepWithCaller = async (step, fn, callerInfo) => {
129130
throw new Error(`Step '${step}' is already defined`)
130131
}
131132
steps[step] = fn
132-
133+
133134
// Use the caller info passed directly
134135
fn.line = callerInfo
135136
}
@@ -139,17 +140,21 @@ const When = createStepFunction('When')
139140
const Then = createStepFunction('Then')
140141
const And = createStepFunction('And')
141142

142-
export {
143-
Given,
144-
When,
145-
Then,
146-
And,
147-
matchStep,
148-
getSteps,
149-
clearSteps,
150-
defineParameterType,
143+
// Before/After hooks for BDD - these are global event listeners
144+
const Before = fn => {
145+
event.dispatcher.on(event.test.started, fn)
146+
}
147+
148+
const After = fn => {
149+
event.dispatcher.on(event.test.finished, fn)
151150
}
152151

152+
const Fail = fn => {
153+
event.dispatcher.on(event.test.failed, fn)
154+
}
155+
156+
export { Given, When, Then, And, Before, After, Fail, matchStep, getSteps, clearSteps, defineParameterType }
157+
153158
export default {
154159
Given: addStep,
155160
When: addStep,

0 commit comments

Comments
 (0)