Obscure test failure after using keyDown with protractor

Posted by Gjermund Bjaanes on July 31, 2015

Lately I have been writing a lot of protractor tests, both privately and on the job. I recently got the displeasure of hunting down an insanely obscure bug which literally showed itself several minutes after the problem actually occurred.

I had several spec files, which ran just fine separat, but failed with very wierd errors.

Only after inserting probably a billion browser.sleep(500) everywhere did I notice that one of the tests deselected an element in a structure, instead of selecting it.

It was selected before the test was trying to select it, but instead of just nothing happening (which is what usually happens when you click an already selected element), it deselected it.

 

Turns out that in some other test did this:

browser.actions().mouseMove(element).keyDown(protractor.Key.CONTROL).click().perform();

Innocent enough I though, but it turns out that keyDown is never released. So protractor ran with the CONTROL key pressed the entire time after that line.

Simple solution is to just use keyUp after you are done doing everything you need.

browser.actions().mouseMove(element).keyDown(protractor.Key.CONTROL).click().keyUp(protractor.Key.CONTROL).perform();

Follow me on Twitter: @gjermundbjaanes