0

I need to Press space Key using JavaScript Inside a TextBox after typing the string. How can I do it inside vugen tool (Load runner) using eval js function? can anyone guide me?

I tried inside to evaluate javascript on the object but getting an error.

 var e = new KeyboardEvent('keydown',{'keyCode':32,'which':32});
 object.dispatchEvent(e);

I am using Eval Js on object function in the tool and paste this script.

ERROR:

No Error it is not clicking the space key inside text box.

I also tried this code

var keyboardEvent = document.createEvent("KeyboardEvent");
var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? 
"initKeyboardEvent" : "initKeyEvent"; 
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
                32, // keyCodeArg : unsigned long the virtual key code, else 

                0 // charCodeArgs : unsigned long the Unicode character 
  associated with the depressed key, else 0
);
document.dispatchEvent(keyboardEvent);

For this also no Error but the code is not clicking space inside text box.

Mia
  • 448
  • 8
  • 22

3 Answers3

1

How does your server know you are pressing the space bar inside of your client.....

James Pulley
  • 5,606
  • 1
  • 14
  • 14
  • I need to press space by getting the element of text and when space is pressed i will be able to add the name in text – Mia Apr 10 '20 at 14:04
1

If you are using TruClient, you could add a eval JS step. TruClient support AUT.document and AUT.window to reference the current page of the application under test.

If you are using a eval JS step on Object, then the target element can be referenced with "object" in the code of the step.

The detail inforamtion can be found from here

Erxin
  • 1,786
  • 4
  • 19
  • 33
  • 1
    Which browser are you using. Is it TCIE? If so please create the event with the sequence document.createEvent(), event.initEvent() element.dispatchEvent(); It is a little bit redundent compare to the others. – Erxin Apr 16 '20 at 08:11
  • Could you please post the detail error message? The syntax may difference for lagacy browsers. You could test in the browser console first then paste the code into the step. – Erxin Apr 17 '20 at 05:51
  • 1
    Hi @user007, please pick the "Evaluate JS on Object" step instead of the Evaluate JS step. Then the obj should be automatic assign to the target by TruClient. – Erxin Apr 18 '20 at 13:21
  • Sorry, my mistake. The keyword obj should be changed to object. Please test again. Thank you. – Erxin Apr 19 '20 at 04:39
  • I gave in "Evaluate JS on Object" this below step var e = new KeyboardEvent('keydown',{'keyCode':32,'which':32}); object.dispatchEvent(e); Now it is saying replay succeeded but It is not pressing space inside the text box field. – Mia Apr 19 '20 at 17:41
  • Please try create the event with new KeyboardEvent('keypress', {key:" ", code:"Space", charCode:32, keyCode:32}). BTW why don't you directly record a step to press the space key? Directly dispatch the event from JS is highly depend on the implementation of the event listeners of the AUT. Some of them may not only listen the keypress event. – Erxin Apr 19 '20 at 23:21
  • thanks will try. As you say i tried with recording press space key inside text box but that keyword is not pressing space after the completion of text. It is pressing space only btw the characters. – Mia Apr 20 '20 at 03:59
  • 1
    After the recording you can modify the step argument. Make sure change the type of the argument to plain text and then add the space character where you liked. TruClient should type the space key accordingly. It's no need to dispatch the event from JS. JS event is not reliable, it will fail in most of the time due to the web application may manipulate and filter the events in the listeners. – Erxin Apr 20 '20 at 07:07
  • Since all try is not working for me I have to leave this task. Thank you very much Erxin, for your effort in helping me to get to here. – Mia Apr 21 '20 at 10:26
  • No problem at all. I have tried add the space key with step argument. It works for me locally. It is strange to know it is not work on your side. If it is a public website you could paste the link to me. I'm just curious. – Erxin Apr 21 '20 at 10:32
  • Were you able to try like adding abc@gmail.com inside a text box and press space using the step args? – Mia Apr 21 '20 at 10:45
  • @user007 Please check my latest answer. There is a short demo video to achieve the goal. – Erxin Apr 22 '20 at 00:27
1

Please check the below demo with TruClient:

Add space into an input element in TruClient

Erxin
  • 1,786
  • 4
  • 19
  • 33
  • yes, thanks the step arg is actually working in Gmail username, etc. But I think I have an issue with my text box application I am trying in cloud health adding email text box application. – Mia Apr 22 '20 at 03:43
  • here it is not pressing space after typing abc@gmail.com that is the issue so i was trying js and all. but in vain. – Mia Apr 22 '20 at 04:36
  • I'm not sure your application is using Web Component or not. If it is using this technology then the current released TruClient is not support directly interactive with this kinds of app. It will be resolved sooner or later in the future version. – Erxin Apr 22 '20 at 08:41
  • @user007 BTW. If your target is input then you don't have to dispatch the JS event. It should be able to directly assign the value property from JS code. – Erxin Apr 23 '20 at 02:27