We have a rather huge test codebase (about 10000 lines of javascript code) and in some situations, we need to turn Protractor-to-Angular synchronization off:
browser.ignoreSynchronization = true;
But, the problem is, from time to time we forget to turn the synchronization on again making all the subsequent tests fail with unclear reasons which require time and effort to debug.
Is there a way to ensure the synchronization is turned back on in a test?
We do have static code analysis based on ESLint
and perform code reviews.
1 : to represent or arrange (events) to indicate coincidence or coexistence. 2 : to make synchronous in operation. 3 : to make (motion-picture sound) exactly simultaneous with the action.
Protractor is a test framework for web applications. Even though its primary use is for performing end-to-end testing in Angular and AngularJS applications, you can also use it for regular, non-Angular websites.
I just do afterTest hook (depends on your test runner), where i set everything back to its starting state, so if i even forgot to switch back in test, or test crashed for some reason on any point - next tests wont be affected:
My code for jasmine. Protractor config:
onPrepare() {
...
afterEach(function () {
// Setting ignoreSychronization back to true, in case it was changed in tests
browser.waitForAngularEnabled(true);
// Setting back to default frame. In case test was working in iframe
browser.switchTo().defaultContent();
// This depends on your architecture. We do clean run for each test.
browser.manage().deleteAllCookies();
browser.executeScript('window.sessionStorage.clear(); window.localStorage.clear();').then(
undefined,
function (err) {
// Errors will be thrown when browser is on default data URL.
// Session and Local storage is disabled for data URLs
// This callback is needed to not crash test, and just ignore error.
});
browser.manage().timeouts().implicitlyWait(5000); // I even rewrite implicit wait back to default value, in case i touched it in tests
// Also you might want to clear indexdb storage after tests.
})
In real code i would wrap this into function called resetBrowser
or something like this. Also you can return promise from it, and then return that promise from hook - so new test wont start until promise resolved
At one point when I was struggling with this same scenario I created a helper function and called it before I navigated to a new page. You still have to "remember" to call the function but at least this gives you only one place in the code where you are handling that change.
isAngularSite(flag) {
browser.ignoreSynchronization = !flag;
}
And in my code I called it like utils.isAngularSite(false);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With