Skip to content

Commit 495813b

Browse files
author
Igor Muchychka
authored
fix: client usage in page objects (#287)
Closes #285
1 parent 773a4de commit 495813b

4 files changed

Lines changed: 110 additions & 117 deletions

File tree

lib/index.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,22 @@ module.exports = function (providedOptions) {
1515
runner.run(options)
1616
}
1717

18-
Object.defineProperty(module.exports, 'client', {
19-
get: () => {
20-
return runner.nightwatchApi.getClientApi()
21-
}
22-
})
18+
function getClientResolver (pages) {
19+
return new Proxy({}, {
20+
get: (target, name) => {
21+
if (name !== 'page') {
22+
return pages.reduce((api, pageName) => {
23+
return api.page[pageName]()
24+
}, runner.nightwatchApi.getClientApi())[name]
25+
}
26+
27+
return new Proxy({}, {
28+
get: (target, pageName) => {
29+
return getClientResolver.bind(null, pages.concat([pageName]))
30+
}
31+
})
32+
}
33+
})
34+
}
35+
36+
module.exports.client = getClientResolver([])

lib/nightwatch-api.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ const Nightwatch = {
1818
queue: require('nightwatch/lib/core/queue.js')
1919
}
2020

21-
process.on('unhandledRejection', (reason, p) => {
22-
console.error('Unhandled Rejection at: Promise', p, 'reason:', reason)
23-
})
24-
2521
module.exports = class NightwatchApi {
2622
constructor (options, colorsEnabled) {
2723
this.options = options

test/error.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,45 @@ describe('Error handling', () => {
171171
})
172172
})
173173

174+
it('should handle page objects failure', () => {
175+
return testCaseFactory
176+
.create('page-object-failure-test')
177+
.pageObject('calculator', `module.exports = {
178+
elements: {
179+
numberA: '#a',
180+
numberB: '#b',
181+
addButton: '#add',
182+
result: '#result'
183+
}
184+
}`)
185+
.feature('addition')
186+
.scenario('small numbers')
187+
.given('User is on the simple calculator page', () => client.init())
188+
.and('User enter 4 in A field', () => {
189+
const calculator = client.page.calculator()
190+
return calculator.setValue('@numberA', 4)
191+
})
192+
.and('User enter 5 in B field', () => {
193+
const calculator = client.page.calculator()
194+
return calculator.setValue('@numberB', 5)
195+
})
196+
.when('User press Add button', () => {
197+
const calculator = client.page.calculator()
198+
return calculator.click('@addButton')
199+
})
200+
.then('The result should contain 10', () => {
201+
const calculator = client.page.calculator()
202+
return calculator.assert.containsText('@result', 10)
203+
})
204+
.run()
205+
.then((result) => {
206+
result.features[0].result.status.should.be.failed
207+
result.features[0].result.scenarioCounts.should.deep.equal({failed: 1})
208+
result.features[0].scenarios[0].result.status.should.be.failed
209+
result.features[0].scenarios[0].result.stepCounts.should.deep.equal({passed: 4, failed: 1})
210+
})
211+
})
212+
174213
it('should handle errors in custom commands', () => {
175214
return testCaseFactory
176215
.create('custom-commands-test', {

test/utility.test.js

Lines changed: 52 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -52,45 +52,6 @@ describe('Utility features', () => {
5252
})
5353
})
5454

55-
it('should handle page objects failure', () => {
56-
return testCaseFactory
57-
.create('page-object-failure-test')
58-
.pageObject('calculator', `module.exports = {
59-
elements: {
60-
numberA: '#a',
61-
numberB: '#b',
62-
addButton: '#add',
63-
result: '#result'
64-
}
65-
}`)
66-
.feature('addition')
67-
.scenario('small numbers')
68-
.given('User is on the simple calculator page', () => client.init())
69-
.and('User enter 4 in A field', () => {
70-
const calculator = client.page.calculator()
71-
return calculator.setValue('@numberA', 4)
72-
})
73-
.and('User enter 5 in B field', () => {
74-
const calculator = client.page.calculator()
75-
return calculator.setValue('@numberB', 5)
76-
})
77-
.when('User press Add button', () => {
78-
const calculator = client.page.calculator()
79-
return calculator.click('@addButton')
80-
})
81-
.then('The result should contain 10', () => {
82-
const calculator = client.page.calculator()
83-
return calculator.assert.containsText('@result', 10)
84-
})
85-
.run()
86-
.then((result) => {
87-
result.features[0].result.status.should.be.failed
88-
result.features[0].result.scenarioCounts.should.deep.equal({failed: 1})
89-
result.features[0].scenarios[0].result.status.should.be.failed
90-
result.features[0].scenarios[0].result.stepCounts.should.deep.equal({passed: 4, failed: 1})
91-
})
92-
})
93-
9455
it('should handle paralell tests', () => {
9556
return testCaseFactory
9657
.create('paralell-test', { paralell: true })
@@ -277,96 +238,79 @@ describe('Utility features', () => {
277238
})
278239
})
279240

280-
it('should be able to provide Nightwatch client as step definition first parameter', () => {
281-
return testCaseFactory
282-
.create('test-nightwatch-client-as-parameter')
283-
.feature('addition')
284-
.scenario('small numbers')
285-
.given('User is on the simple calculator page', () => client.init())
286-
.and('User enter "4" in A field', (a) => client.setValue('#a', a))
287-
.and('User enter "5" in B field', (b) => client.setValue('#b', b))
288-
.when('User press Add button', () => client.click('#add'))
289-
.then('The result should contain "9"', (result) => client.assert.containsText('#result', result))
290-
.scenario('big numbers')
291-
.given('User is on the simple calculator page')
292-
.and('User enter "44" in A field')
293-
.and('User enter "55" in B field')
294-
.when('User press Add button')
295-
.then('The result should contain "99"')
296-
.run()
297-
.then((result) => {
298-
result.features[0].result.status.should.be.passed
299-
result.features[0].result.scenarioCounts.should.deep.equal({passed: 2})
300-
result.features[0].scenarios[0].result.status.should.be.passed
301-
result.features[0].scenarios[0].result.stepCounts.should.deep.equal({passed: 5})
302-
result.features[0].scenarios[1].result.status.should.be.passed
303-
result.features[0].scenarios[1].result.stepCounts.should.deep.equal({passed: 5})
304-
})
305-
})
306-
307241
it('should work together with babel', () => {
308242
return testCaseFactory
309243
.create('babel-example', { babel: true })
310244
.feature('addition')
311245
.scenario('small numbers')
312246
.given('User is on the simple calculator page', `async () => {
313247
await client.init()
314-
}`)
315-
.and('User enter 4 in A field', `async () => {
316248
await client.setValue('#a', 4)
317-
}`)
318-
.and('User enter 5 in B field', `async () => {
319249
await client.setValue('#b', 5)
320-
}`)
321-
.when('User press Add button', `async () => {
322250
await client.click('#add')
323251
}`)
324252
.then('The result should contain 9', `async () => {
325253
await client.assert.containsText('#result', 9)
326254
}`)
327-
.scenario('big numbers')
328-
.given('User is on the simple calculator page')
329-
.and('User enter 4 in A field')
330-
.and('User enter 5 in B field')
331-
.when('User press Add button')
332-
.then('The result should contain 9')
333-
.feature('subtraction')
334-
.scenario('small numbers')
335-
.given('User is on the simple calculator page')
336-
.and('User enter 9 in A field', `async () => {
337-
await client.setValue('#a', 9)
338-
}`)
339-
.and('User enter 3 in B field', `async () => {
340-
await client.setValue('#b', 3)
341-
}`)
342-
.when('User press Subtract button', `async () => {
343-
await client.click('#subtract')
344-
}`)
345-
.then('The result should contain 6', `async () => {
346-
await client.assert.containsText('#result', 6)
255+
.run()
256+
.then((result) => {
257+
result.features[0].result.status.should.be.passed
258+
result.features[0].result.scenarioCounts.should.deep.equal({passed: 1})
259+
result.features[0].scenarios[0].result.status.should.be.passed
260+
result.features[0].scenarios[0].result.stepCounts.should.deep.equal({passed: 2})
261+
})
262+
})
263+
264+
it('should enable the usage of client in page object custom commands', () => {
265+
return testCaseFactory
266+
.create('client-in-page-object-custom-commands-test')
267+
.pageObject('shared', `module.exports = {
268+
url: 'http://yahoo.com',
269+
elements: {
270+
body: 'body',
271+
a: '#a',
272+
b: '#b',
273+
add: '#add',
274+
result: '#result'
275+
}
347276
}`)
348-
.scenario('big numbers')
349-
.given('User is on the simple calculator page')
350-
.and('User enter 4 in A field')
351-
.and('User enter 5 in B field')
352-
.when('User press Subtract button')
353-
.then('The result should contain -1', `async () => {
354-
await client.assert.containsText('#result', -1)
277+
.pageObject('calculator', `const { client } = require('../../../lib/index')
278+
const shared = client.page.shared()
279+
const commands = {
280+
setA: function (value) {
281+
return shared.setValue('@a', value)
282+
},
283+
setB: function (value) {
284+
return shared.setValue('@b', value)
285+
},
286+
pressAdd: function () {
287+
return shared.click('@add')
288+
},
289+
checkResult: function (expectedResult) {
290+
return shared.assert.containsText('@result', expectedResult)
291+
}
292+
}
293+
module.exports = {
294+
url: 'http://yahoo.com',
295+
elements: {
296+
body: 'body',
297+
searchBar: 'input[name="p"]'
298+
},
299+
commands: [commands]
355300
}`)
301+
.feature('addition')
302+
.scenario('small numbers')
303+
.given('User is on the simple calculator page', () => client.init())
304+
.and('User enter 4 in A field', () => client.page.calculator().setA(4))
305+
.and('User enter 5 in B field', () => client.page.calculator().setB(5))
306+
.when('User press Add button', () => client.page.calculator().pressAdd())
307+
.then('The result should contain 9', () => client.page.calculator().checkResult(9))
356308
.run()
357309
.then((result) => {
358310
result.features[0].result.status.should.be.passed
359-
result.features[0].result.scenarioCounts.should.deep.equal({passed: 2})
311+
result.features[0].result.scenarioCounts.should.deep.equal({passed: 1})
360312
result.features[0].scenarios[0].result.status.should.be.passed
361313
result.features[0].scenarios[0].result.stepCounts.should.deep.equal({passed: 5})
362-
result.features[0].scenarios[1].result.status.should.be.passed
363-
result.features[0].scenarios[1].result.stepCounts.should.deep.equal({passed: 5})
364-
result.features[1].result.status.should.be.passed
365-
result.features[1].result.scenarioCounts.should.deep.equal({passed: 2})
366-
result.features[1].scenarios[0].result.status.should.be.passed
367-
result.features[1].scenarios[0].result.stepCounts.should.deep.equal({passed: 5})
368-
result.features[1].scenarios[1].result.status.should.be.passed
369-
result.features[1].scenarios[1].result.stepCounts.should.deep.equal({passed: 5})
370314
})
371315
})
372316
})

0 commit comments

Comments
 (0)