Highlighted
Absent Member.. Absent Member..
Absent Member..
2069 views

Need JS code to press F2 button in Ajax Tru client

Jump to solution

Hi ,

I need to press F2 button in the application to get into Edit mode , found no option to press keys from F1-F12.

Executed  below code using Evaluate JS and no action was taken place.

Suggest any option where i can press F2 by executing JS.

 var keyboardEvent = document.createEvent("KeyboardEvent");

var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? "initKeyboardEvent" : "initKeyEvent";

 

 

keyboardEvent[initMethod](

                   "keydown", // event type : keydown, keyup, keypress

                    true, // bubbles

                    true, // cancelable

                    window, // viewArg: should be window

                    false, // ctrlKeyArg

                    false, // altKeyArg

                    false, // shiftKeyArg

                    false, // metaKeyArg

                    113, // keyCodeArg : unsigned long the virtual key code, else 0

                    0 // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0

);

document.dispatchEvent(keyboardEvent);

 

Thanks in advance

 

0 Likes
1 Solution

Accepted Solutions
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

Re: Need JS code to press F2 button in Ajax Tru client

Jump to solution

Please use the code below inside Evaluate JavaScript step. The code you shared in the beginning of this post simulates only keydown. In order to fully simulate the user key stroke you need to add also keypress and keyup

 

var keyboardEvent = document.createEvent("KeyboardEvent");
var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? "initKeyboardEvent" : "initKeyEvent";


keyboardEvent[initMethod](
"keydown", // event type : keydown, keyup, keypress
true, // bubbles
true, // cancelable
window, // viewArg: should be window
false, // ctrlKeyArg
false, // altKeyArg
false, // shiftKeyArg
false, // metaKeyArg
113, // keyCodeArg : unsigned long the virtual key code, else 0
0 // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0
);
document.dispatchEvent(keyboardEvent);

keyboardEvent[initMethod](
"keypress", // event type : keydown, keyup, keypress
true, // bubbles
true, // cancelable
window, // viewArg: should be window
false, // ctrlKeyArg
false, // altKeyArg
false, // shiftKeyArg
false, // metaKeyArg
113, // keyCodeArg : unsigned long the virtual key code, else 0
0 // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0
);
document.dispatchEvent(keyboardEvent);


keyboardEvent[initMethod](
"keyup", // event type : keydown, keyup, keypress
true, // bubbles
true, // cancelable
window, // viewArg: should be window
false, // ctrlKeyArg
false, // altKeyArg
false, // shiftKeyArg
false, // metaKeyArg
113, // keyCodeArg : unsigned long the virtual key code, else 0
0 // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0
);
document.dispatchEvent(keyboardEvent);

View solution in original post

0 Likes
9 Replies
Highlighted
Absent Member.
Absent Member.

Re: Need JS code to press F2 button in Ajax Tru client

Jump to solution

Hi

 

I used you code with two small differences –

  1. I used “Evaluate JS on object” and selected an object which was a sibling of the the one with the “keydown” handler (can also be the same object with the handler on it)
  2. Changed last line to “object.dispatchEvent(keyboardEvent);”

 

Worked for me on LoadRunner 12.53 TruClient Chrome 

If it is not working well for you then please –

  • Specify a browser and LoadRunner version
  • Show a code snippet from your application (or a “logical equivalent”)
0 Likes
Highlighted
Absent Member.. Absent Member..
Absent Member..

Re: Need JS code to press F2 button in Ajax Tru client

Jump to solution

Hi Kraus,

 

Thanks for your reply.

I applied the code suggested by you but nothing happening except code execution.

We are using lr 12.53 and tried both Chrome and Firefox.

Sceanrio is we need to select multiple rows in an excel like application and need to press F2 , so that the first row will come to edit mode.

I need that F2 should be pressed when i execute the code.

 

 

 

0 Likes
Highlighted
Absent Member.
Absent Member.

Re: Need JS code to press F2 button in Ajax Tru client

Jump to solution

Hi

In what way does your application make sure it is F2? Is it checking the keyCode of the keyboard event?

Thanks

Moshe

0 Likes
Highlighted
Absent Member.. Absent Member..
Absent Member..

Re: Need JS code to press F2 button in Ajax Tru client

Jump to solution

Hi,

Application prompts us  to press F2 to go to edit mode  to edit the selected rows.

I dont have much insight on how application was build for this functionality.

 

0 Likes
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

Re: Need JS code to press F2 button in Ajax Tru client

Jump to solution

Please use the code below inside Evaluate JavaScript step. The code you shared in the beginning of this post simulates only keydown. In order to fully simulate the user key stroke you need to add also keypress and keyup

 

var keyboardEvent = document.createEvent("KeyboardEvent");
var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? "initKeyboardEvent" : "initKeyEvent";


keyboardEvent[initMethod](
"keydown", // event type : keydown, keyup, keypress
true, // bubbles
true, // cancelable
window, // viewArg: should be window
false, // ctrlKeyArg
false, // altKeyArg
false, // shiftKeyArg
false, // metaKeyArg
113, // keyCodeArg : unsigned long the virtual key code, else 0
0 // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0
);
document.dispatchEvent(keyboardEvent);

keyboardEvent[initMethod](
"keypress", // event type : keydown, keyup, keypress
true, // bubbles
true, // cancelable
window, // viewArg: should be window
false, // ctrlKeyArg
false, // altKeyArg
false, // shiftKeyArg
false, // metaKeyArg
113, // keyCodeArg : unsigned long the virtual key code, else 0
0 // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0
);
document.dispatchEvent(keyboardEvent);


keyboardEvent[initMethod](
"keyup", // event type : keydown, keyup, keypress
true, // bubbles
true, // cancelable
window, // viewArg: should be window
false, // ctrlKeyArg
false, // altKeyArg
false, // shiftKeyArg
false, // metaKeyArg
113, // keyCodeArg : unsigned long the virtual key code, else 0
0 // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0
);
document.dispatchEvent(keyboardEvent);

View solution in original post

0 Likes
Highlighted
Absent Member.. Absent Member..
Absent Member..

Re: Need JS code to press F2 button in Ajax Tru client

Jump to solution

Hi Sholmi,

Thnaks for thge reply.

Tried the below code , no action is taking place still.

0 Likes
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

Re: Need JS code to press F2 button in Ajax Tru client

Jump to solution

Hi,

I tried this code on  http://unixpapa.com/js/testkey.html and it works correctly.

Maybe you need to run in Evaluate JavaScrpt on Object or talk to the developers of your application to get help.

Regards,

Shlomi

 

0 Likes
Highlighted
Absent Member.. Absent Member..
Absent Member..

Re: Need JS code to press F2 button in Ajax Tru client

Jump to solution

Hi Sholmi,

Thanks for your posts.

It worked when i changed the last line to to document.activeElement.dispatchEvent(keyboardEvent);

 

0 Likes
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

Re: Need JS code to press F2 button in Ajax Tru client

Jump to solution

Excellent!!

Glad to hear

 

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.