Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to not forget to turn the Protractor to Angular sync on again?

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.

like image 651
alecxe Avatar asked Dec 14 '17 17:12

alecxe


People also ask

What is protractor synchronization?

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.

Can protractor be used for non angular application?

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.


2 Answers

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

like image 89
Xotabu4 Avatar answered Oct 19 '22 21:10

Xotabu4


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);

like image 33
tehbeardedone Avatar answered Oct 19 '22 21:10

tehbeardedone