Refactor the test code to unify the wait methods