/
TakePhotosOfFailures.spec.ts
119 lines (97 loc) · 4.93 KB
/
TakePhotosOfFailures.spec.ts
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import 'mocha';
import { EventRecorder, expect, PickEvent } from '@integration/testing-tools';
import { isPresent } from '@serenity-js/assertions';
import { Duration, Wait } from '@serenity-js/core';
import { ActivityRelatedArtifactGenerated, ActivityStarts } from '@serenity-js/core/lib/events';
import { CorrelationId, Photo } from '@serenity-js/core/lib/model';
import { Stage } from '@serenity-js/core/lib/stage';
import { BrowseTheWeb, By, PageElement, Photographer, TakePhotosOfFailures } from '@serenity-js/web';
import { create } from '../create';
import { Perform } from '../fixtures';
describe('Photographer', () => {
describe('when instructed to take a photo of failed interactions,', () => {
let photographer: Photographer,
stage: Stage,
recorder: EventRecorder;
beforeEach(() => {
const sut = create();
stage = sut.stage;
recorder = sut.recorder;
photographer = new Photographer(new TakePhotosOfFailures(), stage);
stage.assign(photographer);
});
it('does nothing if everything goes well', () =>
expect(stage.theActorCalled('Betty').attemptsTo(
Perform.interactionThatSucceeds(),
)).to.be.fulfilled.then(() => stage.waitForNextCue().then(() => {
expect(recorder.events).to.have.lengthOf(2); // Interaction starts and finishes
})));
it('takes a photo when a problem occurs', () =>
expect(stage.theActorCalled('Betty').attemptsTo(
Perform.interactionThatFailsWith(Error),
)).to.be.rejected.then(() => stage.waitForNextCue().then(() => {
PickEvent.from(recorder.events)
.next(ActivityRelatedArtifactGenerated, event => {
expect(event.name.value).to.match(/Betty fails due to Error$/);
expect(event.artifact).to.be.instanceof(Photo);
});
})));
it('takes a photo when a timeout is reached', () =>
expect(
stage.theActorCalled('Betty')
.attemptsTo(
Wait.upTo(Duration.ofMilliseconds(100))
.until(PageElement.located(By.css('#invalid')), isPresent())
)
).to.be.rejected.then(() =>
stage.waitForNextCue().then(() => {
PickEvent.from(recorder.events)
.next(ActivityRelatedArtifactGenerated, event => {
expect(event.name.value).to.match(/Betty waits up to 100ms/);
expect(event.artifact).to.be.instanceof(Photo);
});
}))
);
it(`correlates the photo with the activity it is concerning`, () =>
expect(stage.theActorCalled('Betty').attemptsTo(
Perform.interactionThatFailsWith(Error),
)).to.be.rejected.then(() => stage.waitForNextCue().then(() => {
let activityId: CorrelationId;
PickEvent.from(recorder.events)
.next(ActivityStarts, event => {
activityId = event.activityId;
})
.next(ActivityRelatedArtifactGenerated, event => {
expect(event.activityId).to.equal(activityId);
});
})));
it('takes only one picture, even though nested tasks might all be marked as failing', () =>
expect(stage.theActorCalled('Betty').attemptsTo(
Perform.taskWith(
Perform.taskWith(
Perform.interactionThatFailsWith(TypeError),
),
),
)).to.be.rejected.then(() => stage.waitForNextCue().then(() => {
PickEvent.from(recorder.events)
.next(ActivityRelatedArtifactGenerated, event => {
expect(event.name.value).to.match(/Betty fails due to TypeError$/);
expect(event.artifact).to.be.instanceof(Photo);
});
})));
it(`includes the browser context in the name of the emitted artifact`, async () => {
const Betty = stage.theActorCalled('Betty');
await expect(stage.theActorCalled('Betty').attemptsTo(
Perform.interactionThatFailsWith(Error),
)).to.be.rejected;
await stage.waitForNextCue();
const capabilities = await BrowseTheWeb.as(Betty).browserCapabilities();
PickEvent.from(recorder.events)
.next(ActivityRelatedArtifactGenerated, event => {
expect(event.name.value).to.equal(
`${ capabilities.platformName }-${ capabilities.browserName }-${ capabilities.browserVersion }-Betty fails due to Error`,
);
});
});
});
});