Welcome Serena Central users! CLICK HERE
The migration of the Serena Central community is currently underway. Be sure to read THIS MESSAGE to get your new login set up to access your account.
Highlighted
agks7 Regular Contributor.
Regular Contributor.
254 views

How to send array of parameters in web_submit_form ?

Hi!

I collect all INPUT TEXT on the page and write the value "test".

I have a code:

web_reg_save_param_attrib(
        "ParamName=inputTexts",
        "TagName=INPUT",
        "Extract=NAME",
        "Type=TEXT",
        "NotFound=warning",
        "Ordinal=ALL",
        SEARCH_FILTERS,
        "IgnoreRedirections=Yes",
        LAST);
    
    web_link("Start", 
        "Text=Start", 
        "Snapshot=t56.inf", 
        LAST);

web_submit_form("1",
        "Snapshot=t57.inf",
        ITEMDATA,
        "Name={inputTexts}", "Value=test", ENDITEM, //Array in this place
        LAST);

 

It should look something like this.

web_submit_form("1",
        "Snapshot=t57.inf",
        ITEMDATA,
        "Name={inputTexts_1}", "Value=test", ENDITEM, 

        "Name={inputTexts_2}", "Value=test", ENDITEM, 

       .........................

       "Name={inputTexts_count}", "Value=test", ENDITEM, 

        LAST);

But the data is dynamic.

I don't know how to do it here. The loop between ITEMDATA and LAST doesn't work or I don't understand something. Please help!

Labels (3)
0 Likes
9 Replies
Trusted Contributor.. vikybs Trusted Contributor..
Trusted Contributor..

Re: How to send array of parameters in web_submit_form ?

Firstly try changing in recording options General > Recording> HTML Advanced: A script containing explicit URLs only AND Record in separate steps & use concurrent groups. This will give complete arguments of web_submit_data()

If I understand correctly, you have the different number of ITEMDATA based on the previous request response. Try using text check and if condition based on the previous request response. For example:
 

   web_reg_find("Text=TEXTCHECK", "SaveCount=cCheckCount1", "Search=Body", LAST);
    
    web_link("Start", 
        "Action=https://{URL}",
        "Method=POST",
        "Text=Start", 
        "Snapshot=t56.inf", 
        LAST);

if(atoi(lr_eval_string("{cCheckCount1}"))!=0)
    {
    web_submit_form("1",
        "Snapshot=t57.inf",
        ITEMDATA,
        "Name={inputTexts1}", "Value=test", ENDITEM, //Array in this place
        LAST);

     }
    else{
    web_submit_form("1",
        "Snapshot=t57.inf",
        ITEMDATA,
        "Name={inputTexts1}", "Value=test", ENDITEM, //Array in this place
        "Name={inputTexts2}", "Value=test", ENDITEM, //Array in this place
        LAST);

     }

0 Likes
Outstanding Contributor.. JHF Remmelzwaal Outstanding Contributor..
Outstanding Contributor..

Re: How to send array of parameters in web_submit_form ?

@agks7, I sketched here already an approach. But lets make it here a little more clear with some example code:

 

 

 

#include "stdlib.h"
#include "string.h"

#define LR_VARARGS_MAX 100
typedef struct { char *a[LR_VARARGS_MAX]; } t_lr_varargs;
t_lr_varargs f_args;
int f_i=0;

#define f_args_add(X) f_args.a[f_i++] = X

// Maintain a list of malloced arguments like: "Name={inputTexts_%d}"
char *f_args_param_list[LR_VARARGS_MAX];
int f_args_param_idx = 0;
void f_args_add_paramarr(char *pattern, int i) {
	char *p = (char*)malloc(strlen(pattern)+2+1); // For one number upto 2 digits.
	sprintf(p, pattern, i);
	f_args_param_list[f_args_param_idx++] = p;
	f_args_add(p);
}
void f_args_reset() {
	int i;
	for(i=0; i<f_args_param_idx; i++ ) {
		if( f_args_param_list[i] ) {
			free( f_args_param_list[i] );
			f_args_param_list[i] = NULL;
		}
	}
	f_args_param_idx = 0;
	f_i = 0;
}
Action()
{
int i,c;
// Parameter collection code goes here.
. . .
// web_submit_form("1", <== Move this to below argument build code.
        f_args_add("Snapshot=t57.inf");
        f_args_add(ITEMDATA);
        c = lr_paramarr_len("inputTexts");
        for(i=1; i<=c; i++ ) {
                f_args_add_paramarr("Name={inputTexts_%d}", i);
                f_args_add("Value=test");
                f_args_add(ENDITEM);
        } 
        f_args_add(LAST);
web_submit_form("1", f_args); // <== Always keep to any LR-function first argument with var-args.
f_args_reset();

 

 

 

I did not test this in all details. But with this approach you should be able to resolve this.

Note: no check on going beyond LR_VARARGS_MAX etc.

Have fun.

Signature:
Reward community members who take time to respond and help.
0 Likes
agks7 Regular Contributor.
Regular Contributor.

Re: How to send array of parameters in web_submit_form ?

@JHF Remmelzwaal 

 

#include "stdlib.h"
#include "string.h"

#define LR_VARARGS_MAX 100
typedef struct { char *a[LR_VARARGS_MAX]; } t_lr_varargs;
t_lr_varargs f_args;
int f_i=0;

#define f_args_add(X) f_args.a[f_i++] = X

// Maintain a list of malloced arguments like: "Name={inputTexts_%d}"
char *f_args_param_list[LR_VARARGS_MAX];
int f_args_param_idx = 0;
void f_args_add_param(char *pattern, int i) {
    char *= (char*)malloc(strlen(pattern)+2+1); // For one number upto 2 digits.
    sprintf(p, pattern, i);
    f_args_param_list[f_args_param_idx++] = p;
}
void f_args_reset() {
    int i;
    for(i=0; i<f_args_param_idx; i++ ) {
        if( f_args_param_list[i] ) {
            free( f_args_param_list[i] );
            f_args_param_list[i] = NULL;
        }
    }
    f_args_param_idx = 0;
    f_i = 0;
}
Action()
{
int i,c;
// Parameter collection code goes here.
char arrayParamName[50]; 
int elemCnt;
int x;
    web_add_cookie("SID=0N34jFDxSBmJjt2DGpehmcXpgFEOMXfM; DOMAIN=test.youplace.net");

    web_url("test.youplace.net", 
        "URL=http://test.youplace.net/", 
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=", 
        "Snapshot=t55.inf", 
        "Mode=HTML", 
        LAST);

    web_reg_save_param_attrib(
        "ParamName=inputTexts",
        "TagName=INPUT",
        "Extract=NAME",
        "Type=TEXT",
        "NotFound=warning",
        "Ordinal=ALL",
        SEARCH_FILTERS,
        "IgnoreRedirections=Yes",
        LAST);
    
    web_link("Start test", 
        "Text=Start test", 
        "Snapshot=t56.inf", 
        LAST);

// web_submit_form("1", <== Move this to below argument build code.

        f_args_add("Snapshot=t57.inf");
        f_args_add(ITEMDATA);
        c = lr_paramarr_len("inputTexts");
        for(i=1; i<=c; i++ ) {
                f_args_add_param("Name={inputTexts_%d}", i);
                f_args_add("Value=test");
                f_args_add(ENDITEM);

    lr_output_message("loop_step_finish");

                    
        } 
        f_args_add(LAST);
 web_submit_form("1", f_args);// <== Always keep to any LR-function first argument with var-args.
f_args_reset();

    return 0;
}

I'm testing the script only on the INPUT TEXT. When there is only INPUT TEXT on the page, without RADIO and SELECT , the script should work. But there is ERROR (Error -27153: Either "Name" or at least one of "NamePfx" and "NameSfx" should be non-empty (before the "ENDITEM" argument number 5). I do not understand. Empty NAME can be due to the fact that more values are sent than there are fields for text input. Or what? I'm stupid. Help me pls. I am beginner

 

0 Likes
Outstanding Contributor.. JHF Remmelzwaal Outstanding Contributor..
Outstanding Contributor..

Re: How to send array of parameters in web_submit_form ?

@agks7, please study the editor for posting (pressing icon with 3 dots shows you the icon to enter code in a readable way).

Even when you are a beginner you can build your own 'debug' code. Here is an example that you might use:

 

void f_args_debug()
{
	int i;
	for(i=0; i<f_i; i++ ) {
		lr_log_message("%2d, '%s' => '%s'", i, f_args.a[i], lr_eval_string(f_args.a[i]));
	}
}

 

Call the function just before the lr_submit_form. It will show all the arguments, you will see that "Name=..." argument is missing.

The very first post contained an error that I corrected by editing my post in 5 min after initial post. You where just too quick. Your function 'f_args_add_param' should just contain one more statement.

Excuse for this inconvenience.

P.S.

"I'm stupid. Help me pls. I am beginner", I think that you are right: It is stupid as a beginner to think that you are stupid that you cannot resolve an issue in advanced code, you did not write 😉

Signature:
Reward community members who take time to respond and help.
0 Likes
agks7 Regular Contributor.
Regular Contributor.

Re: How to send array of parameters in web_submit_form ?

@JHF Remmelzwaal 

Thank you very much for your help. It's a huge help in speeding up my learning. I have another question. In the version with Truclient I wrote a script with JavaScript. The code processed and filled INPUT TEXT, RADIO and SELECT. Is it possible to integrate this code into Action.c ? To work with the document.getElementsBy...Like there is a "web_js_run", but I don't know how to link it.

 

JavaScript code:

//******INPUT TEXT
//Create array of all inputs on the page
var inputArray = document.getElementsByTagName("input");

//Find all input text fields and fill it by value "test"
for (var t=0;t<inputArray.length;t++)
{
  if (inputArray[t].type=="text")
    inputArray[t].value="test";
}


//*******INPUT RADIO
//Create set for radiogroup names
let set = new Set();

//Create array of all inputs on the page
var radioArray = document.getElementsByTagName("input");

//Add radiogroup names in set
for (var t=0;t<radioArray.length;t++)
{
  if (radioArray[t].type=="radio")
    set.add(radioArray[t].name);
}

//Outside loop for enum names of radiogroup
set.forEach((value, valueAgain, set) => {
 
  var rad=document.getElementsByName(value); //create array for values in radiogroup 
  
  //Inside loop for enum values in each radiogroup and Find value with maximal length
  for (var i=1, max=rad[0].value, maxi=0;i<rad.length; i++) {
        
     	//Compare nearby values
    	if (rad[i].value.length>max.length)
          {
            max=rad[i].value; //max length value
            maxi=i; //index of max length value
            
          }
        }
        rad[maxi].checked=true;//check max length value if radiogroup
}
      			)
//******SELECTS     
//Create set for "select" names
let set = new Set();

//Create array of all "select" on the page
var radioArray = document.getElementsByTagName("select");

//Add "select" names in set
for (var t=0;t<radioArray.length;t++)
{
      set.add(radioArray[t].name);
}

//Outside loop for enum names of "select"
set.forEach((value, valueAgain, set) => {
  
	//One of "Select"
  var rad=document.getElementsByName(value);

	//Inside loop for enum values in each "select" and Find value with maximal length
  for (var i=1, max="", maxi=0;i<rad[0].options.length; i++) {
        if (rad[0].options[i].value.length>max.length)
          {
            max=rad[0].options[i].value; //max length value
            maxi=i; //index of max length value
          }
        }
        rad[0].value = max;//set max length value if "select"
	}
    			)
0 Likes
agks7 Regular Contributor.
Regular Contributor.

Re: How to send array of parameters in web_submit_form ?

@JHF Remmelzwaal 

I decided to switch to JavaScript as there are strange things in this code. The code doesn't work. String comparison does not work correctly. Compare the lengths of the strings too. I do not understand. Why are RADIOS not filled as needed. Help((

 

#include "stdlib.h"
#include "string.h"

#define LR_VARARGS_MAX 100
typedef struct { char *a[LR_VARARGS_MAX]; } t_lr_varargs;
t_lr_varargs f_args;
int f_i=0;

#define f_args_add(X) f_args.a[f_i++] = X

// Maintain a list of malloced arguments like: "Name={inputTexts_%d}"
char *f_args_param_list[LR_VARARGS_MAX];
int f_args_param_idx = 0;
void f_args_add_paramarr(char *pattern, int i) {
	char *p = (char*)malloc(strlen(pattern)+2+1); // For one number upto 2 digits.
	sprintf(p, pattern, i);
	f_args_param_list[f_args_param_idx++] = p;
	f_args_add(p);
}
void f_args_reset() {
	int i;
	for(i=0; i<f_args_param_idx; i++ ) {
		if( f_args_param_list[i] ) {
			free( f_args_param_list[i] );
			f_args_param_list[i] = NULL;
		}
	}
	f_args_param_idx = 0;
	f_i = 0;
}

void f_args_debug()
{
	int i;
	for(i=0; i<f_i; i++ ) {
		lr_log_message("%2d, '%s' => '%s'", i, f_args.a[i], lr_eval_string(f_args.a[i]));
	}
}


Action()
{
	 char max[100];
	 char names_i[100];
	 char names_i1[100];
	 char values_i[100];
	 char values_i1[100];
	 
	 
     int maxi=1;
     int maxindex[20];
     int i,c,j,k,z,g;
	 int elemCnt;
	 int x;


	web_url("test.youplace.net", 
		"URL=<a href="http://test.youplace.net/" target="_blank">http://test.youplace.net/</a>", 
		"Resource=0", 
		"RecContentType=text/html", 
		"Referer=", 
		"Snapshot=t55.inf", 
		"Mode=HTML", 
		LAST);

	

	web_reg_save_param_attrib(
		"ParamName=inputTexts",
		"TagName=INPUT",
		"Extract=NAME",
		"Type=TEXT",
		"NotFound=warning",
		"Ordinal=ALL",
		SEARCH_FILTERS,
		"IgnoreRedirections=Yes",
		LAST);
	 
	 

	web_reg_save_param_attrib(
		"ParamName=inputRadioNames",
		"TagName=INPUT",
		"Extract=NAME",
		"Type=RADIO",
		"NotFound=warning",
		"Ordinal=ALL",
		SEARCH_FILTERS,
		"IgnoreRedirections=Yes",
		LAST);
	 
	 
web_reg_save_param_attrib(
		"ParamName=inputRadioValues",
		"TagName=INPUT",
		"Extract=VALUE",
		"Type=RADIO",
		"NotFound=warning",
		"Ordinal=ALL",
		SEARCH_FILTERS,
		"IgnoreRedirections=Yes",
		LAST);
	

web_reg_save_param_attrib(
		"ParamName=SelectsName",
		"TagName=SELECT",
		"Extract=NAME",
		"NotFound=warning",
		"Ordinal=ALL",
		SEARCH_FILTERS,
		"IgnoreRedirections=Yes",
		LAST);
		

	
	web_link("Start test", 
		"Text=Start test", 
		"Snapshot=t56.inf", 
		LAST);
	

		f_args_add(ITEMDATA);
                
// Working with INPUT RADIO
		g = lr_paramarr_len("inputRadioNames");
			
			sprintf(max,"{inputRadioValues_%d}", 1);
			maxi=1;

		
        
		for ( i = 1; i < g; i++) {
        
        		sprintf(names_i,"{inputRadioNames_%d}", i);
        		sprintf(names_i1,"{inputRadioNames_%d}", i+1);
        		sprintf(values_i,"{inputRadioValues_%d}", i);
        		sprintf(values_i1,"{inputRadioValues_%d}", i+1);
        		        		
        		if (strcmp(names_i,names_i1)==0){
        			
        			if (strlen(max) < strlen(values_i1)) {
        		
                    	sprintf(max, values_i1);
                    	maxi = i+1;
                	}
        		}
        	
        			else {
        				f_args_add_paramarr("Name={inputRadioNames_%d}", maxi);
                 		f_args_add_paramarr("Value={inputRadioValues_%d}", maxi);
                		f_args_add(ENDITEM);
        			
        				sprintf(max, values_i1);
                    	maxi = i+1;
        				                		
        			}
        	
        		if (i==(g-1) ) {
        			f_args_add_paramarr("Name={inputRadioNames_%d}", maxi);
            		f_args_add_paramarr("Value={inputRadioValues_%d}", maxi);
            		f_args_add(ENDITEM);
        		}
                
        	}
        
       		
        	
		
        // Working with INPUT TEXT
        c = lr_paramarr_len("inputTexts");
        
        for(i=1; i<=c; i++ ) {
        	f_args_add_paramarr("Name={inputTexts_%d}", i);
            f_args_add("Value=test");
            f_args_add(ENDITEM);
        }
        
        f_args_add(LAST);
        f_args_debug();
        
        web_submit_form("1", f_args);

        f_args_reset();


	return 0;
}

 

0 Likes
Outstanding Contributor.. JHF Remmelzwaal Outstanding Contributor..
Outstanding Contributor..

Re: How to send array of parameters in web_submit_form ?

@agks7  wrote: "Is it possible to integrate this code into Action.c ? To work with the document.getElementsBy...Like there is a "web_js_run", but I don't know how to link it"

The answer is simple NO. Understanding why is important. TruClient works on an head-less browser (so has access to the DOM). Web protocol works on the network so there is no internal representation of the DOM.

You can use "web_js_run" to execute some java script code if you like, but hard to use.

Signature:
Reward community members who take time to respond and help.
Outstanding Contributor.. JHF Remmelzwaal Outstanding Contributor..
Outstanding Contributor..

Re: How to send array of parameters in web_submit_form ?

@agks7wrote: "I decided to switch to JavaScript as there are strange things in this code. The code doesn't work. String comparison does not work correctly. Compare the lengths of the strings too. I do not understand. Why are RADIOS not filled as needed. Help(("

You might describe in words your approach (algorithm). I cannot see your intention.

But you might make one wrong assumption and that is related to LR parameter expansion.

Read carefully the LR help on the functions: lt_eval_string and lr_paramarr_idx.

Use logging function like:

lr_log_message("Compare string '%s' with '%s' gives %d", names_i, names_i1, strcmp(names_i,names_i1));

You will find what is going wrong.

Signature:
Reward community members who take time to respond and help.
agks7 Regular Contributor.
Regular Contributor.

Re: How to send array of parameters in web_submit_form ?

Thanks a lot! I will learn
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.