9

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.

5
  • 1
    Maybe write a wrapper function e.g. withoutSync(() => { ... }) that turns it off at the start and back on at the end - this can then wrap whole expectation callbacks or just parts of them. Then you only set the attribute directly in that one function.
    – jonrsharpe
    Dec 14, 2017 at 17:11
  • @jonrsharpe hm, like a "context manager", nice idea, I like it. Sometimes though we need to have it off for several it()s in a row..thanks.
    – alecxe
    Dec 14, 2017 at 17:12
  • Use webdriverio instead. Then you don't have to worry about it. In all seriousness, This is the exact reason I switched from protractor to wdio. Once your framework gets large it becomes a nightmare to keep track of when the sync is turned on or not. In my experience, I actually ended up with less code when I migrated. Dec 14, 2017 at 18:11
  • 1
    I don't know anything about Protractor but with C#/NUnit there is a SetUp attribute that you can tag a function with so that it gets run before each script. Do you have a mechanism that you can set up like that so that you can intentionally turn on sync before the start of each test globally? Each test that didn't want it on would have to turn it off but that seems like a good tradeoff to ensure that it's on for each test.
    – JeffC
    Dec 14, 2017 at 19:29
  • @JeffC yeah, currently, if we turn the sync back on in the test, it is usually done in afterEach() or afterAll() of every test. Turning the sync on in a global "before each" or "after each" sounds like it should work..thanks.
    – alecxe
    Dec 14, 2017 at 19:32

2 Answers 2

9
+50

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

1
  • 1
    Oh nice, a global afterEach is probably the best idea so far. Thanks, Хотабыч!
    – alecxe
    Dec 20, 2017 at 14:34
4

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.