Highlighted
Super Contributor.. Super Contributor..
Super Contributor..
1118 views

JavaScript syntax help

Hello JavaScript gurus,

Our team has upgraded to 12.53 patch 4.  In this new version we can record in JavaScript in stead of C.  I am not a programmer.  My current issue is, after I created my JavaScript, I want to add a Main action transaction and call all my steps from the Main Action.  Sample below is how it looked.  I have not found what the JavaScript syntax should look like.  I have looked on the web for examples but I didn't see any.  I also tried the conversion tool HP provided and it didn't work.  Your assustance is greatly appreciated.

Main()
{
lr_start_transaction("Login");
Login();
lr_end_transaction("Login", LR_AUTO);
lr_think_time(5);
lr_start_transaction("LookupAccountInfo");
LookupAccountInfo();
lr_end_transaction("LookupAccountInfo", LR_AUTO);
lr_think_time(5);
lr_start_transaction("UpdateAccount");
UpdateAccount();
lr_end_transaction("UpdateAccount", LR_AUTO);
lr_think_time(5);
lr_start_transaction("SaveAccount");
SaveAccount();
lr_end_transaction("SaveAccount", LR_AUTO);
lr_think_time(5);
Logoff(); 

 return 0;
}

 

0 Likes
7 Replies
Highlighted
Frequent Contributor.
Frequent Contributor.

Re: JavaScript syntax help

Hello,

You can select "record"->"regenerate script" and select "options" in the dialog,

then change the script->language to javascript then regenerate the scripts.

And for transactions, LR javascript API is synchronous functions.

So if your actions are also synchronous, it just looks like:

lr.startTransaction(‘main’);

    lr.startTransaction(‘Login’);

        Login();

    lr.endTransaction(‘Login’, lr.AUTO);

    Lr.thinkTIme(5);

    …

lr.endTransaction(‘main’, lr.AUTO);

Regards,

Bingle LoadRunner(R&D)

0 Likes
Highlighted
Super Contributor.. Super Contributor..
Super Contributor..

Re: JavaScript syntax help

Bingle,

Thanks for your reply. 

When I regenerate my script and change the option the Javascript it deletes my Actions "Main" section.  When I try and manually make those changes you suggested I get the error listed below.  I did notice when I create a new actions section it start the section with " function Main() " but you instructions didn't show this text.  Your assistance is greatly appreciated.

Here is what main Action section looks like now

lr.startTransaction(‘Main’); 
  {
lr.startTransaction(‘Login’);
Login();
lr.endTransaction(‘Login’, lr.AUTO);

lr.startTransaction(‘LookupAccountInfo’);
LookupAccountInfo();
lr.endTransaction(‘LookupAccountInfo’, lr.AUTO);

lr.startTransaction(‘UpdateAccount’);
UpdateAccount();
lr.endTransaction(‘UpdateAccount’, lr.AUTO);

lr.startTransaction(‘SaveAccount’);
SaveAccount();
lr.endTransaction(‘SaveAccount’, lr.AUTO);

lr.startTransaction(‘Logoff’);
Logoff();
lr.endTransaction(‘Logoff’, lr.AUTO);

lr.endTransaction(‘Main’, lr.AUTO);

return 0;
 }

Error

Main.js(1): Error: 'SyntaxError: Unexpected token ILLEGAL'.
    Source line: 'lr.startTransaction(‘MainÂ’); '.

 

0 Likes
Highlighted
Super Contributor.. Super Contributor..
Super Contributor..

Re: JavaScript syntax help

Bingle,

Do you have any other thoughts why this isn't working?

 

Larry

0 Likes
Highlighted
Super Contributor.. Super Contributor..
Super Contributor..

Re: JavaScript syntax help

Since I am creating my own Action Section do I need to initialize all the Actions?   Feels like I am missing some steps. 

 

Larry

0 Likes
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

Re: JavaScript syntax help

Hi Larry,

Can you share a screenshot of the script after the regeneration? I suspect the Main function is missing now and the first step that runs is the start transaction but outside the context of an action.

If you wish to call to your logic from the main action you should not add new actions to the script, you should create new functions and call them. if you add new actions they are added also to the script run logic and that might cause misbehavior of the script. Meaning, the main action calls the action and then it also runs independently  as a trigger of the script run logic.

Regards,

Shlomi

0 Likes
Highlighted
Frequent Contributor.
Frequent Contributor.

Re: JavaScript syntax help

Hello Larry,

i would also like to add some information regarding script development in JavaScript.

as for now,if you want to wrtie and develop (as i can see from the example above) in JavaScrtip,

the first step that you will need to do is to intialize the scrip typs as JavaScript.

in order to do so you will have to set the script type from "Recording Options-->Script-->JavaScript"

and start recording a simple step  as navigation to a  web page.

by doing so it will intialize the script as JavaScript type.

after that,the structure should look this :

JSTree.png

note the file called "global.js"

in the extra file you will be able to add custome *.js files that contain you function and logic.

you will need to add lr.require("path to *.js" file) in the file called global,js

and you will also be able to add function/variables to that file if you would need to.

after the global will contain referance to files or your own funciton,you will be able to use them in the actions (default or new ones)

note ,that any custome code will be removed in case of regeneration of the script.

please see this simple example:

Action.pngNewAction.pngGlobalJS.pngCustJS.png

 

hope it will make some sense.

if anything else is needed,feel free to ask

 

Uriel

 

Highlighted
Frequent Contributor.
Frequent Contributor.

Re: JavaScript syntax help

Hello,

For the issue:

Main.js(1): Error: 'SyntaxError: Unexpected token ILLEGAL'.
    Source line: 'lr.startTransaction(‘MainÂ’); '

This is cause by the quotation marks changed form half width character to full width character when I pasting the code to furum. you can change it back, sorry for the inconvernient. 

For other part of creating JS script, I think you've already catch it, have you?

Regards,

Bingle

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.