rafalf
rafalf

Reputation: 435

Wait for angular using python

I have found this answer however I'm not testing angularjs app but angular 2. Testing AngularJS with Selenium

looking for a similar piece of js code to execute to wait until angular finishes.

I came up with an idea of a decorator that I'll use to wait before touching the app but I don' know what the script would like, I took a look into clientsidescripts.js but I am not an Angular developer and it seems that it gets ng1hooks and testability... so maybe I could extra some bits and pieces and put them into a js file and execute it e.g. like this guy https://github.com/kpodl/pytractor/blob/master/src/pytractor/protractor/extracted/waitForAngular.js but again this is from 2015.

def angular_wait_required(f):

        def wrapped_f(self, *args, **kwargs):
            self.driver.set_script_timeout(30)
            self.driver.execute_async_script(WAIT_FOR_SCRIPT)
            f(self, *args, **kwargs)
        return wrapped_f

Upvotes: 0

Views: 811

Answers (1)

rafalf
rafalf

Reputation: 435

WAIT_FOR_SCRIPT = """
        callback = arguments[arguments.length - 1];
        try {
            var testabilities = window.getAllAngularTestabilities();
            var count = testabilities.length;
            var decrement = function() {
            count--;
            if (count === 0) {
              callback('completed');
                }
            };
            testabilities.forEach(function(testability) {
                testability.whenStable(decrement);
            });
         } catch (err) {
            callback(err.message);
         }
        """

this is what i came up with in the end

Upvotes: 1

Related Questions