AngularFire / Protractor: a solution to the famous ScriptTimeoutError error message
I worked hard on the protractor.conf.js configuration file and on the .steps.ts files by trying several possible combinations:
When(/^User click on the button "([^"]*)"$/, async (id) => {
await element(by.id(id)).click();
});
When(/^User click on the button "([^"]*)"$/, function(id:string, callback){
element(by.id(id)).click().then(callback);
});
but no light at the end of the tunnel.
Until I come across a discussion on github (https://github.com/angular/angularfire2/issues/225) where I discover that the problem comes from the AngularFirestore that operates in the same execution context "zone" than Angular; AngularFirestore triggers a series of intervals and timeouts, which, in turn, prevent protractor from synchronizing with Angular.
The solution:
Well, you just have to run AngularFirestore in a different execution context than the main context, namely the angular framework. using NgZone
import { Injectable, NgZone } from '@angular/core';
import { AngularFirestore } from 'angularfire2/firestore';
@Injectable()
export class FirestoreService {
constructor(
private firestore: AngularFirestore,
private zone: NgZone) {
}
save(item: any, collection: string){
this.zone.runOutsideAngular(() => {
let shirtsCollection = firestore.collection<Item>('tshirts');
shirtsCollection.add({ name: 'item', price: 10 });
});
}
Comments
Post a Comment