Support subdocuments.
Use setTimeout for sleeping. Reordered tests to remove conflicts between capturing warnings.