执行如下代码:
- newActions(driver).keyDown(Keys.CONTROL).keyDown(Keys.F5).keyUp(Keys.CONTROL).keyUp(Keys.F5).perform();
会报如下错误:
- java.lang.IllegalArgumentException:KeyDown/Upeventsonlymakesenseformodifierkeys.
Google 了一下Modifier Key:
- Each key that appears on the keyboard without requiring modifiers are sent as a keydown followed by a key up.
- If the server does not support native events and must simulate key strokes with JavaScript, it must generate keydown, keypress, and keyup events, in that order. The keypress event should only be fired when the corresponding key is for a printable character.
- If a key requires a modifier key (e.g. "!" on a standard US keyboard), the sequence is:modifierdown,keydown,keyup,modifierup, wherekeyis the ideal unmodified key value (using the previous example, a "1").
- Modifier keys (Ctrl, Shift, Alt, and Command/Meta) are assumed to be "sticky"; each modifier should be held down (e.g. only a keydown event) until either the modifier is encountered again in the sequence, or theNULL(U+E000) key is encountered.
- Each key sequence is terminated with an implicitNULLkey. Subsequently, all depressed modifier keys must be released (with corresponding keyup events) at the end of the sequence.
所以CTRL属于Modifier Key,需要这样写:
- ActionsactionObject=newActions(driver);
- actionObject.keyDown(Keys.CONTROL).sendKeys(Keys.F5).keyUp(Keys.CONTROL).perform();
运行测试通过,页面被成功强制刷新