Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make protractor press the ESCAPE key?

I've tried this:

browser.actions().sendKeys(protractor.Key.ESCAPE).perform();

but nothing happens.

I'm using this with SPACE Key and this works fine.

browser.actions().sendKeys(protractor.Key.SPACE).perform();
like image 758
Krausz Lóránt Szilveszter Avatar asked Mar 31 '17 08:03

Krausz Lóránt Szilveszter


3 Answers

There could be several reasons for it, but first of all, according to the documentation of Protractor you need to execute the sendKeys() on an ElementFinder, so if the focus is on for example an input field you can do this

element(by.css('#-your-input-id')).sendKeys(protractor.Key.ESCAPE);

You can also do it on the body like this ($ = shorthand for by.css):

$('body').sendKeys(protractor.Key.ESCAPE);

Secondly there could be a problem with the UserInteraction API of your webdriver. In the past there were a lot of problems with the Firefox and Safari driver and with some versions of Chromedriver.

Hope this helps

like image 59
wswebcreation Avatar answered Oct 20 '22 22:10

wswebcreation


If you want to use the same code as you have return, you can use this:

browser
    .actions()
    .sendKeys(protractor.Key.ESCAPE)
    .perform();

I hope this would work for you.

Since the sendKeys and Key is implemented using Keyboard class, the key values will differ for the OS May be.

The links below will give the name of the keys you have to give for the sendKeys:
https://www.autoitscript.com/autoit3/docs/appendix/SendKeys.htm https://autohotkey.com/docs/commands/Send.htm

Hope this helped.

like image 44
krishnarajanr Avatar answered Oct 20 '22 23:10

krishnarajanr


Maybe you must send the ESC Key to any element? At my app the following assignment works fine for a cancel button.

element(by.xpath('...')).sendKeys(protractor.Key.ESCAPE);
like image 3
Huluvu424242 Avatar answered Oct 21 '22 00:10

Huluvu424242