Skip to content

Commit

Permalink
fix(reporting): JSON report is generated even if we couldn't capture …
Browse files Browse the repository at this point in the history
…a screenshot

The synchronisation of capturing screenshots and writing the json report to disk is now improved, so

that Serenity will not allow Protractor to finish before the reports are saved.
  • Loading branch information
jan-molak committed Jan 19, 2017
1 parent 294ad24 commit f230239
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
12 changes: 12 additions & 0 deletions spec/api/serenity-protractor/stage/photographer.spec.ts
Expand Up @@ -131,6 +131,18 @@ describe('Photographer', () => {

return expect(photoAttempted.value.photo).to.eventually.be.rejectedWith(unknownError);
});

it('Notifies the Stage Manager of any work in progress', () => {

fileSystem.store.withArgs(photoName, imageBuffer).returns(photoPath);

thePhotographer.notifyOf(new ActivityStarts(activity, now));

return expect(stageManager.allDone()).to.eventually.be.fulfilled.then(tasks => {
expect(tasks).to.have.length(1);
expect(tasks.pop()).to.be.instanceOf(Photo);
});
});
});

describe('When the Actor in the spotlight finished to perform an Activity', () => {
Expand Down
8 changes: 7 additions & 1 deletion src/serenity-protractor/plugin/serenity_protractor_plugin.ts
Expand Up @@ -15,7 +15,13 @@ export class SerenityProtractorPlugin /* implements ProtractorPlugin */ {
}

teardown() {
return Serenity.stageManager().allDone();
return Serenity.stageManager().allDone().then(
_ => _,
error => {
console.error('[SerenityProtractorPlugin] An error occurred during teardown', error); // tslint:disable-line:no-console
return Promise.reject(error);
},
);
}

private crewMembers(): StageCrewMember[] {
Expand Down
17 changes: 11 additions & 6 deletions src/serenity-protractor/stage/photographer.ts
Expand Up @@ -125,19 +125,24 @@ export class Photographer implements StageCrewMember {
let saveScreenshot = data => this.fs.store(this.naming.nameFor(data), new Buffer(data, 'base64'));

let ignoreInactiveBrowserButReportAnyOther = (error: Error) => {
if (error.message.indexOf('does not have a valid session ID') > -1) {
// todo: this needs further investigation; sometimes webdriver session dies before we can take a screenshot
if (~error.message.indexOf('does not have a valid session ID') || ~error.message.indexOf('Session ID is null')) {
// tslint:disable-next-line:no-console
console.warn(`[Photographer] Looks like there was a problem with taking a photo of ${ actor }: `, error.message);

return undefined;
}

throw error;
};

return BrowseTheWeb.as(actor).takeScreenshot()
.then(saveScreenshot)
.then(
path => new Photo(path),
return this.stage.manager.informOfWorkInProgress(
BrowseTheWeb.as(actor).takeScreenshot()
.then(saveScreenshot).then(
path => new Photo(path),
error => ignoreInactiveBrowserButReportAnyOther(error),
);
),
);
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/serenity/stage/stage_manager.ts
Expand Up @@ -12,7 +12,7 @@ export interface StageCrewMember {
export class StageManager {

private listeners: CrewMembersCommunicationChannel[] = [];
private wip: Array<Promise<any>> = [];
private wip: Array<PromiseLike<any>> = [];

constructor(private journal: Journal) {
}
Expand All @@ -30,11 +30,13 @@ export class StageManager {
}
}

informOfWorkInProgress<T>(promise: Promise<T>): void {
informOfWorkInProgress<T>(promise: PromiseLike<T>): PromiseLike<T> {
this.wip.push(promise);

return promise;
}

allDone(): Promise<any[]> {
allDone(): PromiseLike<any[]> {
return Promise.all(this.wip);
}

Expand Down

0 comments on commit f230239

Please sign in to comment.