How to press and hold a key without a modifier (space bar) using selenium?

I met a problem that selenium cannot press and hold a key that is not in this list -

Keys.SHIFT, 
Keys.CONTROL, 
Keys.ALT, 
Keys.META,
Keys.COMMAND, 
Keys.LEFT_ALT, 
Keys.LEFT_CONTROL,
Keys.LEFT_SHIFT

My application shows instructions only when you press and hold the spacebar. I want to write browser tests for this.

I use ProtractorJS, but it seems to be a common limitation for such an action, everywhere in selenium, when you try to use keyDown for another key - you will get an exception from this message: "Key Down / Up events make sense for modifier keys."

here is the link to Selenium Java code: https://github.com/SeleniumHQ/selenium/blob/master/java/client/src/org/openqa/selenium/interactions/internal/SingleKeyAction.java#L48

selenium js: https://github.com/SeleniumHQ/selenium/blob/master/javascript/webdriver/actionsequence.js#L301

? .

UPDATE: Florent B. . - . .

browser.switchTo().frame('workspace');  
const SIMULATE_KEY =  
"var e = new Event('keydown');" +  
"e.keyCode = 32;" +  //spacebar keycode
"e.which = e.keyCode;" +  
"e.altKey = false;" +  
"e.ctrlKey = false;" +  
"e.shiftKey = false;" +  
"e.metaKey = false;" +  
"e.bubbles = true;" +  
"document.dispatchEvent(e);";  
browser.executeScript(SIMULATE_KEY);
+4
2

API Selenium API . :

https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol

: , , , .

Javascript:

const SIMULATE_KEY =
  "var e = new Event(arguments[0]);" +
  "e.key = arguments[1];" +
  "e.keyCode = e.key.charCodeAt(0);" +
  "e.which = e.keyCode;" +
  "e.altKey = false;" +
  "e.ctrlKey = false;" +
  "e.shiftKey = false;" +
  "e.metaKey = false;" +
  "e.bubbles = true;" +
  "arguments[2].dispatchEvent(e);";

var target = driver.findElement(By.Id("..."));

// press the key "a"
browser.executeScript(SIMULATE_KEY, "keydown", "a", target);

// release the key "a"
browser.executeScript(SIMULATE_KEY, "keyup", "a", target);
+2

, , script (, AutoIt AutoHotKey), .

, .

0

All Articles