Run focus test only if document has focus

(cherry picked from commit 0b9a182ecde49adb508d76577e8f244da08a1e51)