A number of behat fails have been creeping in with the customfield code
as behat is moving onto the next step before the current step finishes.
This change introduces some additional pending JS tracking to prevent
this.
This commit is part of work on Custom fields API,
to minimize commit history in moodle core the work of a team of developers was split
into several commits with different authors but the authorship of individual
lines of code may be different from the commit author.