Bug 981363 - Wait or cursor to be cleared in tests. Some cleanup and improvements. r=yzen