Selenium KeyDown for keys not in the modifier - selenium

As I know, selenium keyDown(keyUp) method is only available for keys in modifiers (SHIFT, ALT, CTRL). I need to press SPACE key a certain amount of time.
I have a questions about key event handling in Selenium keyboard interface.
1. Why selenium does not permit keydown and keyup event for other general keys ?
2. Is there any workaround to achieve this goal ?
For now, I used Robot class to implement this, but I don't want to use this.

since you didn't tell us which Selenium-binding you are using, I will show my examples in Java, you may transcode them into any binding you please:
you can press the SPACE key via the sendKeys() method:
driver.sendKeys(Keys.SPACE);
I don't know why you would want to use the keyDown() keyUp() methods for the "general keys" you are talking about, you can also use sendKeys() for most objectives:
driver.sendKeys("a");
You can use the Actions class if you want to combine normal key-actions with keyDown()/keyUp() actions
Actions action = new Actions(driver);
action.keyDown(Keys.CONTROL).sendKeys("c").keyUp(Keys.CONTROL).perform();
for example to copy something via CTRL+c...

Related

Karate-UI Automation - How to press key without necessity to be in input field (feature file)

How I press key when I am on the page?. E.g.: I need to press "ESC" key or some key combination.
In documentation there is description how do this when you are in input field - it works fine. But if I want to press the key button without using input field I am not successful (In feature file: I have tried for example this, but it did not work: driver.input(Key.ENTER) ).
Thank you.
This can be a gap in our implementation, so can you submit an issue, ideally following this process so that we have an example: https://github.com/intuit/karate/tree/develop/examples/ui-test
For the time being, see if you can work-around by firing a key-press event via JS. I am also hoping that if you target the body element it will work for the whole HTML page:
* input('body', Key.ENTER)
Reference for creating keyboard event: https://stackoverflow.com/a/12187302/143475
And some tips on JS in Karate: https://stackoverflow.com/a/60800181/143475

Simulate key with Laravel Dusk (without input element)

I have a page which consists of some disabled inputs, spans and divs.
In the background I wait for pressed keys. Background of this is some memory game.
My problem is, that it turned out to be pretty hard to test.
I know two functions to simulate input: type() and keys(). When I use keys("#someElement", "34"), I get "element not interactable". Similar happens with type() (well that's not surprising as type suggests filling some input).
How can I properly simulate a pressed key without using an active input element?
Use the underlying driver:
$browser->driver->getKeyboard()->sendKeys('a');
$browser->driver->getKeyboard()->sendKeys(\Facebook\WebDriver\WebDriverKeys::ENTER);

user press the key "0" in jogl how can I do it?

I want to show and hide an implemented object if a user press the key "0" in jogl how can I do it?
Simply add a key listener into the focused component exactly as you would do with any Java software using AWT and Swing. If you use NEWT, you'll have to use a key listener too anyway.
Then implement the method keyReleased(KeyEvent), modify the value of a boolean field when 0 is released and look at this value in GLEventListener.display(GLAutoDrawable) to determine whether to show or hide your object.

Is it better to use Actions.sendKeys() or WebElement.sendKeys() to simulate a user typing with Selenium WebDriver?

As far as I'm aware there are two ways of typing with Selenium:
new Actions(webDriver).sendKeys("text to send").perform();
webElement.sendKeys("text to send");
The Actions method seems like the most natural way of replicating a user typing as the keys are sent wherever the browser wants (I believe a method called sendKeysToActiveElement is being used under the covers). However, many tutorials instruct testers to use the WebElement method (this is in fact the only option when using SafariDriver), I assume as it is simpler.
Is the Actions method actually a better simulation of user interaction, or should I use the WebElement method for convenience?
public Actions sendKeys(java.lang.CharSequence... keys)
Sends keys to the active element. This differs from calling WebElement.sendKeys(CharSequence...) on the active element in two ways:
The modifier keys included in this call are not released.
There is no attempt to re-focus the element - so sendKeys(Keys.TAB) for switching elements should work.
for more you can refer this link : https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/interactions/Actions.html#sendKeys-java.lang.CharSequence...-
This doesn't answert the question directly but I would have thought using SendKeys in the WebElement class would have been better, since you already have the WebElement object in memory, why do you need to create an Actions object?
I have always used the WebElement.SendKeys() method and I have not found any uses to switch over to using the Actions class for sending over a regular string.
I would use the Actions class when I require more complex scenarios e.g. Need to hold down a button or drag something.
Two things:
If you want to recreate exactly the interactions of a user, it is better to use Actions, otherwise it is better to use webElement.sendkeys() because it is more practical and simple.
If you need to execute a complex action like drag and drop it is also better to use Actions.

Firefox add-on development: Register global dynamic custom keyboard shortcuts

I have been tasked with developing a Firefox add-on that is capable of registering global keyboard shortcuts (ones that will work throughout all areas of Firefox) that will open up the side-bar and execute an XMLRPC request based on previously recorded input. The idea here is that there will be many potential XMLRPC requests that the user will want to execute via a keyboard shortcut.
Currently, the add-on is capable of handling pre-defined static keyboard shortcuts via the Firefox overlay. What I would like to achieve, is to allow the user to register their own dynamic custom keyboard shortcut.
There is an add-on that currently has some of this functionality, called Keyconfig. I'm not keen on having to ask users to install a second add-on to define their own shortcuts. It also seems that using the dynamic keyboard shortcut registration method in Keyconfig would require the user to close all Firefox windows before the dynamic shortcut is made available.
What I would like to know is:
Is an XPCOM component the best way to register dynamic keyboard shortcuts from within a Firefox add-on?
Is there a way to register the keyboard shortcut so that it is immediately available to all Firefox windows, without having to close the windows beforehand?
I'm not able to answer "Is an XPCOM component the best way to register dynamic keyboard shortcuts from within a Firefox add-on?", but with the help of a work colleague, we have figured out a way to do the dynamic keyboard shortcuts and make them immediately usable.
window.onkeypress = callback;
Whilst being an egregious hack, as it will execute the callback for every keypress event that the chrome window processes; it does allow for the main Firefox chrome window to have dynamic keyboard shortcuts without necessitating a reload.
To get this to work with the keyboard shortcuts being defined in the sidebar, I've created a listener in the overlay JavaScript's init() method that listens to a custom event from the sidebar:
init: function() {
var sidebarElement = document.getElementById("sidebar");
sidebarElement.addEventListener("fooKeyboardShortcut", function shortcutKeyListener(anEvent) {
fooOverlay.shortcutMap = sidebarElement.contentWindow.foo.getShortcutKeysMap();
}, true);
},
The object in the sidebar will then fire off one of these events on any state change to the map, which will cause the event listener to assign the map to the object in the Firefox overlay. The mapping key is a composite hash of the following keypress event data members:
altKey
ctrlKey
metaKey
shiftKey
charCode
It is worth noting here, that keyCode can't be used as it seems to have the value '0' all the time; but charCode does have the correct value.
The value for each key is a callback key for the sidebar object to execute the desired XMLRPC call.
This shortcut key map is then queried for each keypress, and if there is a match; the callback key is used on a pre-registered sidebar callback