3

I require to test a functionality of an input form, where i have to validate that pressing tab key works through right input fields. I used selenium's action as well as Keys.tab

Actions new tab = new Actions(driver); newtab.SendKeys(Keys.Tab).Build().Perform();

but due to google chrome version 53.0.2785.116 , its not supporting tab key press and so i want to simulate tab key press through javascript. All the answers only follow to "what to do after" the event is called. Could anyone give me any insight in this?

EDIT: please note that i need to run this scripts in selenium web driver test. So answers relevant to same would be very helpful. I did find questions and few confusing answers like Question A Question B

I also tried the following solutionLink here but its not working. Does "keyboardEvent" not work anymore? could some one give me a workaround?

Community
  • 1
  • 1
wingskush
  • 534
  • 1
  • 6
  • 22
  • you might want to look [Robot](http://docs.oracle.com/javase/6/docs/api/java/awt/Robot.html#keyPress%28int%29) – manish Sep 23 '16 at 08:50

2 Answers2

0

Using Jquery you can try:

$("#1234").trigger({type: 'keypress', which: 9, keyCode: 9});

Using JS:

var pressTabKey = new Event('keydown');
document.getElementById('1234').addEventListener('keydown', function() { alert("hi!"); });
document.getElementById('1234').dispatchEvent(pressTabKey);

where 1234 is id of textbox.

P.K.
  • 1,746
  • 6
  • 25
  • 62
-1

In jQuery we use just like follows :

Suppose you have TextBox with Id txtName

$("[id*=txtName]").on('keydown', function(e) { 
  var keyCode = e.keyCode || e.which; 
  if (keyCode == 9) {
     e.preventDefault();
     alert('Tab Pressed');
 }
}); 
UJS
  • 853
  • 1
  • 10
  • 16
  • hey @UJS what i am asking for is specific to using JAVASCRIPT in selenium webdriver , to send tabs automated. What you suuggest is to check whether the pressed key is tab – wingskush Sep 25 '16 at 07:23
  • In jQuery ,we use to check like this only .Here to show you the Example i just putton alert .. In place of alert you can make the logic required .It is not a wrong option i had provided .If feel ,please remove -ve vote . – UJS Dec 02 '16 at 10:29