LoadRunner Code Snippets
Jump to navigation
Jump to search
Detect a Pattern in a Page
//------------------------------------------------------------------------- // Warning - You are already logged on to the system in the following sessions: web_reg_find("Text=You are already logged on to the system", "SaveCount=warning_count", LAST); lr_think_time(2); web_text_link("Log On", "Snapshot=t3.inf", DESCRIPTION, "Text=Log On", ACTION, "UserAction=Click", LAST); lr_end_transaction("login", LR_AUTO); sprintf(msgbuf, "Logged in..."); userLog(msgbuf); //------------------------------------------------------------------------- if (atoi(lr_eval_string("{warning_count}")) > 0) { sprintf(msgbuf, "Warning - You are already logged on to the system"); userLog(msgbuf); web_text_link("Continue", "Snapshot=t35.inf", DESCRIPTION, "Text=Continue", ACTION, "UserAction=Click", LAST); } //------------------------------------------------------------------------- lr_think_time(3); lr_start_transaction("CreateBid"); web_text_link("CreateBid", "Snapshot=t4.inf", DESCRIPTION, "Text=Create Bid", "FrameName=menu", ACTION, "UserAction=Click", LAST); lr_end_transaction("CreateBid", LR_AUTO); sprintf(msgbuf, "CreateBid done..."); userLog(msgbuf); //------------------------------------------------------------------------- // <input type="hidden" name="MessLine" value="No longer possible to process any bids for this bid invitation"> lr_think_time(3); web_reg_find("Text=value=\"No longer possible to process any bids for this bid invitation", "SaveCount=alert_count", LAST); lr_start_transaction("select_93"); web_text_link("select_93", "Snapshot=t5.inf", DESCRIPTION, "Text=93", "FrameName=IACFrame", ACTION, "UserAction=Click", LAST); lr_end_transaction("select_93", LR_AUTO); sprintf(msgbuf, "select_93 done..."); userLog(msgbuf); //------------------------------------------------------------------------- if (atoi(lr_eval_string("{alert_count}")) > 0) { sprintf(msgbuf, "No longer possible to process any bids for this bid invitation"); userLog(msgbuf); return 0; }
Find and Extract Multiple Instances in a Page
Here we are looking for invoices in an invoice list...
//----- At 'Invoice Search Screen' ----------------------------------------- web_reg_find("Text=XXXX", LAST); web_reg_save_param("pageID", "LB=input type=\"hidden\" name=\"pageID\" value=\"", "RB=\"", LAST); // Look for lines like "<!------------------------ Invoice 427738 ---" web_reg_save_param("invoice", "LB=!------------------------ Invoice ", "RB= ---", "ORD=ALL", LAST); web_submit_data("app_5", "Action=https://www.performiq.com.au/app", "Method=POST", "RecContentType=text/html", "Referer=https://www.performiq.com.au/app", "Snapshot=t22.inf", "Mode=HTTP", ITEMDATA, "Name=pageID", "Value={pageID}", ENDITEM, "Name=lastName", "Value=A", ENDITEM, "Name=firstName", "Value=", ENDITEM, ... "Name=btnSearch", "Value=Search", ENDITEM, LAST); //----- At 'Invoice List' page - <Go> to view invoice ---------------------- no_invoices = atoi(lr_eval_string('{invoice_count}')); for (i = 1; i < no_invoices; i++) { sprintf(invoice_param_str, "{invoice_%s}", i); sprintf(invoice_no, lr_eval_string(invoice_param_str)); ViewInvoice(invoice_no) } //----- Back at 'Invoice List' page ----------------------------------------
Page Content Detection Logic
#include "web_api.h" CodeExpenses() { int unsubmitted = 0; int do_submit = FALSE; int next = 0; int no_expenses = 0; int expenses_remaining = 0; int submit_cnt = 0; int ignore = 0; sprintf(msgbuf, ">>>>> New ITERATION using [%s] <<<<<", lr_eval_string("{Username}")); userLog(msgbuf); //----- At 'Invoice List' page -------------------------------------------- web_reg_find("Text=XXXX", LAST); lr_start_transaction("Login"); web_submit_data("Login Page", "Action={Protocol}://{System}/app", "Method=POST", "RecContentType=text/html", "Referer={Protocol}://{System}/app?pageID=0&LanguageID=31&Licensee=xxxxx", "Snapshot=t5.inf", "Mode=HTTP", ITEMDATA, "Name=pageID", "Value=0", ENDITEM, "Name=LanguageID", "Value=31", ENDITEM, "Name=Username", "Value={Username}", ENDITEM, "Name=Password", "Value={Password}", ENDITEM, "Name=OK", "Value= OK ", ENDITEM, LAST); lr_end_transaction("Login", LR_AUTO); sprintf(msgbuf, " Username: [%s]", lr_eval_string("{Username}")); userLog(msgbuf); sprintf(msgbuf, "Host - %s Name - %s", lr_eval_string("{System}"), lr_eval_string("{Username}")); userLog(msgbuf); //----- At 'Invoice Search Screen' ---------------------------------------- web_reg_find("Text=XXXX", LAST); // Look for lines like "<!------------------------ Invoice 427738 ---" web_reg_save_param("pageID", "LB=input type=\"hidden\" name=\"pageID\" value=\"", "RB=\"", LAST); web_reg_save_param("invoice", "LB=----- Invoice ", "RB= -----", "ORD=ALL", LAST); web_reg_save_param("InvoiceID", "LB=input type=\"hidden\" name=\"InvoiceID\" value=\"", "RB=\"", LAST); lr_start_transaction("InvoiceList"); web_submit_data("TO: 'Invoice List'", "Action={Protocol}://{System}/app", "Method=POST", "RecContentType=text/html", "Referer={Protocol}://{System}/app", "Snapshot=t9.inf", "Mode=HTTP", ITEMDATA, "Name=pageID", "Value=0", ENDITEM, "Name=btnOk", "Value=OK", ENDITEM, LAST); lr_end_transaction("InvoiceList", LR_AUTO); sprintf(msgbuf, " InvoiceID: [%s]", lr_eval_string("{InvoiceID}")); userLog(msgbuf); lr_think_time( 5 ); //----- At 'Invoice List' page - <Go> to view invoice --------------------- web_reg_find("Text=XXXX", LAST); web_reg_save_param("pageID", "LB=input type=\"hidden\" name=\"pageID\" value=\"", "RB=\"", LAST); // This lives here - <input type="hidden" name="ExpenseID" value="1111111"/>\r\n web_reg_save_param("ExpenseID", "LB=input type=\"hidden\" name=\"ExpenseID\" value=\"", "RB=\"", LAST); lr_start_transaction("View_InvoiceDetails"); web_submit_data("View_InvoiceDetails", "Action={Protocol}://{System}/app", "Method=POST", "RecContentType=text/html", "Referer={Protocol}://{System}/app", "Snapshot=t14.inf", "Mode=HTTP", ITEMDATA, "Name=pageID", "Value={pageID}", ENDITEM, "Name=InvoiceID", "Value={InvoiceID}", ENDITEM, "Name=ddInvoiceAction", "Value=View", ENDITEM, "Name=btnGo", "Value=Go", ENDITEM, LAST); lr_end_transaction("View_InvoiceDetails", LR_AUTO); sprintf(msgbuf, " ExpenseID: [%s]", lr_eval_string("{ExpenseID}")); userLog(msgbuf); lr_think_time( 5 ); //-------------------------------------------------------------------------- web_reg_find("Text=XXXX", LAST); web_reg_save_param("pageID", "LB=input type=\"hidden\" name=\"pageID\" value=\"", "RB=\"", LAST); web_reg_save_param("AccountHolderID", "LB=input type='hidden' name='AccountHolderID' value='", "RB='", LAST); web_reg_save_param("ExpenseCount", "LB=input type=\"hidden\" name=\"ExpenseCount\" value=\"", "RB=\"", LAST); web_reg_save_param("ExpenseNumber", "LB=input type=\"hidden\" name=\"ExpenseNumber\" value=\"", "RB=\"", LAST); web_reg_find("Text=Submitted", "SaveCount=submitted_count", LAST); web_reg_find("Text=Information Only", "SaveCount=infoonly_count", LAST); web_reg_find("Text=Committed", "SaveCount=committed_count", LAST); lr_start_transaction("CodingFrontend"); web_submit_data("TO: 'Coding Frontend'", "Action={Protocol}://{System}/app", "Method=POST", "RecContentType=text/html", "Referer={Protocol}://{System}/app", "Snapshot=t18.inf", "Mode=HTTP", ITEMDATA, "Name=pageID", "Value={pageID}", ENDITEM, "Name=InvoiceID", "Value={InvoiceID}", ENDITEM, "Name=ExpenseID", "Value={ExpenseID}", ENDITEM, "Name=Action", "Value=Details", ENDITEM, "Name=btnGo", "Value=Go", ENDITEM, LAST); lr_end_transaction("CodingFrontend", LR_AUTO); sprintf(msgbuf, " InvoiceID: [%s]", lr_eval_string("{InvoiceID}")); userLog(msgbuf); sprintf(msgbuf, " AccountHolderID: [%s]", lr_eval_string("{AccountHolderID}")); userLog(msgbuf); sprintf(msgbuf, " ExpenseCount: [%s]", lr_eval_string("{ExpenseCount}")); userLog(msgbuf); /* * We are now in Coding screen and about to use wizard */ lr_think_time( 10 ); //------ Enter basic data and then run the Wizard ------------------------- if (atoi(lr_eval_string("{submitted_count}")) > 0) { // Already submitted so lets press NEXT! unsubmitted = 0; } else { unsubmitted = 1; } if (atoi(lr_eval_string("{infoonly_count}")) > 0) { sprintf(msgbuf, "----- Information Only -----"); userLog(msgbuf); ignore = 1; unsubmitted = 0; } if (atoi(lr_eval_string("{committed_count}")) > 0) { sprintf(msgbuf, "----- Committed -----"); userLog(msgbuf); ignore = 1; unsubmitted = 0; } sprintf(msgbuf, "Unsubmitted flag == %d", unsubmitted); userLog(msgbuf); no_expenses = atoi(lr_eval_string("{ExpenseCount}")); expenses_remaining = atoi(lr_eval_string("{ExpenseCount}")); submit_cnt = 0; while (expenses_remaining > 0) { sprintf(msgbuf, " ExpenseNumber: [%s]", lr_eval_string("{ExpenseNumber}")); userLog(msgbuf); sprintf(msgbuf, " ExpenseID: [%s]", lr_eval_string("{ExpenseID}")); userLog(msgbuf); next = TRUE; do_submit = FALSE; while (next) { if (unsubmitted) { sprintf(msgbuf, "Running the Wizard on this expense:"); userLog(msgbuf); web_reg_find("Text=XXXX", LAST); web_reg_save_param("pageID", "LB=input type=\"hidden\" name=\"pageID\" value=\"", "RB=\"", LAST); /* web_reg_save_param("ExpenseNumber", "LB=input type=\"hidden\" name=\"ExpenseNumber\" value=\"", "RB=\"", LAST); */ web_reg_save_param("LastQuestionID", "LB=input type=\"hidden\" name=\"LastQuestionID\" value=\"", "RB=\"", LAST); web_reg_save_param("LastQuestionTypeID", "LB=input type=\"hidden\" name=\"LastQuestionTypeID\" value=\"", "RB=\"", LAST); /* WizardAnswer is and array of Radio Buttons --------------------------------- web_reg_save_param("WizardAnswer", "LB=input type=\"hidden\" name=\"WizardAnswer\" value=\"", "RB=\"", LAST); */ web_reg_find("Text=You cannot use the Coding Wizard", "SaveCount=submitted_count", LAST); lr_continue_on_error(1); lr_start_transaction("ToWizard"); web_submit_data("TO: Wizard'", "Action={Protocol}://{System}/app", "Method=POST", "RecContentType=text/html", "Referer={Protocol}://{System}/app", "Snapshot=t22.inf", "Mode=HTTP", ITEMDATA, "Name=pageID", "Value={pageID}", ENDITEM, ... "Name=btnWizard", "Value=Wizard", ENDITEM, LAST); lr_end_transaction("ToWizard", LR_AUTO); lr_continue_on_error(0); if (atoi(lr_eval_string("{submitted_count}")) > 0) { // Already submitted so lets press NEXT! sprintf(msgbuf, "[To Wizard] -> Expense already submitted - Cannot re-run wizard. Selecting NEXT one!"); userLog(msgbuf); unsubmitted = 0; } else { sprintf(msgbuf, "[To Wizard] -> OK"); userLog(msgbuf); unsubmitted = 1; do_submit = TRUE; next = 0; } } else { sprintf(msgbuf, "Selecting 'NEXT' expense:"); userLog(msgbuf); web_reg_save_param("pageID", "LB=input type=\"hidden\" name=\"pageID\" value=\"", "RB=\"", LAST); web_reg_save_param("ExpenseNumber", "LB=input type=\"hidden\" name=\"ExpenseNumber\" value=\"", "RB=\"", LAST); web_reg_save_param("ExpenseID", "LB=input type='hidden' name='ExpenseID' value='", "RB='", LAST); web_reg_find("Text=ExpenseStatus3\">Submitted", "SaveCount=submitted_count", LAST); web_reg_find("Text=ExpenseStatus5\">Information", "SaveCount=infoonly_count", LAST); web_reg_find("Text=ExpenseStatus8\">Committed", "SaveCount=comitted_count", LAST); lr_continue_on_error(1); lr_start_transaction("NextExpense"); web_submit_data("TO: 'Next Expense'", "Action={Protocol}://{System}/app", "Method=POST", "RecContentType=text/html", "Referer={Protocol}://{System}/app", "Snapshot=t22.inf", "Mode=HTTP", ITEMDATA, "Name=pageID", "Value={pageID}", ENDITEM, ... "Name=btnNext", "Value=Next", ENDITEM, LAST); lr_end_transaction("NextExpense", LR_AUTO); lr_continue_on_error(0); if (atoi(lr_eval_string("{infoonly_count}")) > 0) { sprintf(msgbuf, "----- Information Only -----"); userLog(msgbuf); ignore = 1; unsubmitted = 0; } if (atoi(lr_eval_string("{committed_count}")) > 0) { sprintf(msgbuf, "----- Committed -----"); userLog(msgbuf); ignore = 1; unsubmitted = 0; } if (atoi(lr_eval_string("{submitted_count}")) > 0) { // Already submitted so lets press NEXT! sprintf(msgbuf, "[NEXT] -> Expense already submitted. Selecting NEXT one!"); userLog(msgbuf); unsubmitted = 0; } else { sprintf(msgbuf, "[NEXT] -> Expense not submitted"); userLog(msgbuf); unsubmitted = 1; } lr_think_time( 1 ); expenses_remaining = atoi(lr_eval_string("{ExpenseCount}")) - atoi(lr_eval_string("{ExpenseNumber}")); if (!unsubmitted && (expenses_remaining == 0)) { next = 0; } sprintf(msgbuf, " ExpenseNumber: [%s] --> %d remaining", lr_eval_string("{ExpenseNumber}"), expenses_remaining); userLog(msgbuf); sprintf(msgbuf, " ExpenseID: [%s]", lr_eval_string("{ExpenseID}")); userLog(msgbuf); } } if (do_submit) { // Go through the wizard... sprintf(msgbuf, " LastQuestionID: [%s]", lr_eval_string("{LastQuestionID}")); userLog(msgbuf); sprintf(msgbuf, " LastQuestionTypeID: [%s]", lr_eval_string("{LastQuestionTypeID}")); userLog(msgbuf); lr_think_time( 5 ); //----- In the Wizard --------------------------------------------- web_reg_find("Text=XXXX", LAST); web_reg_save_param("pageID", "LB=input type=\"hidden\" name=\"pageID\" value=\"", "RB=\"", LAST); lr_start_transaction("Wizard_1"); web_submit_data("Wizard_1", "Action={Protocol}://{System}/app", "Method=POST", "RecContentType=text/html", "Referer={Protocol}://{System}/app", "Snapshot=t26.inf", "Mode=HTTP", ITEMDATA, "Name=pageID", "Value={pageID}", ENDITEM, ... "Name=btnNext", "Value=Next", ENDITEM, LAST); lr_end_transaction("Wizard_1", LR_AUTO); lr_think_time( 1 ); //----------------------------------------------------------------- web_reg_find("Text=XXXX", LAST); web_reg_save_param("pageID", "LB=input type=\"hidden\" name=\"pageID\" value=\"", "RB=\"", LAST); lr_start_transaction("Wizard_2"); web_submit_data("Wizard_2", "Action={Protocol}://{System}/app", "Method=POST", "RecContentType=text/html", "Referer={Protocol}://{System}/app", "Snapshot=t30.inf", "Mode=HTTP", ITEMDATA, "Name=pageID", "Value={pageID}", ENDITEM, ... "Name=btnNext", "Value=Next", ENDITEM, LAST); lr_end_transaction("Wizard_2", LR_AUTO); lr_think_time( 1 ); //----------------------------------------------------------------- web_reg_find("Text=XXXX", LAST); web_reg_save_param("pageID", "LB=input type=\"hidden\" name=\"pageID\" value=\"", "RB=\"", LAST); lr_start_transaction("Wizard_3"); web_submit_data("Wizard_3", "Action={Protocol}://{System}/app", "Method=POST", "RecContentType=text/html", "Referer={Protocol}://{System}/app", "Snapshot=t34.inf", "Mode=HTTP", ITEMDATA, "Name=pageID", "Value={pageID}", ENDITEM, ... "Name=btnNext", "Value=Next", ENDITEM, LAST); lr_end_transaction("Wizard_3", LR_AUTO); lr_think_time( 1 ); //----------------------------------------------------------------- web_reg_find("Text=XXXX", LAST); web_reg_save_param("pageID", "LB=input type=\"hidden\" name=\"pageID\" value=\"", "RB=\"", LAST); lr_start_transaction("Wizard_4"); web_submit_data("Wizard_4", "Action={Protocol}://{System}/app", "Method=POST", "RecContentType=text/html", "Referer={Protocol}://{System}/app", "Snapshot=t38.inf", "Mode=HTTP", ITEMDATA, "Name=pageID", "Value={pageID}", ENDITEM, ... "Name=btnNext", "Value=Next", ENDITEM, LAST); lr_end_transaction("Wizard_4", LR_AUTO); lr_think_time( 1 ); //----------------------------------------------------------------- web_reg_find("Text=XXXX", LAST); web_reg_save_param("pageID", "LB=input type=\"hidden\" name=\"pageID\" value=\"", "RB=\"", LAST); lr_start_transaction("Wizard_5"); web_submit_data("Wizard_5", "Action={Protocol}://{System}/app", "Method=POST", "RecContentType=text/html", "Referer={Protocol}://{System}/app", "Snapshot=t42.inf", "Mode=HTTP", ITEMDATA, "Name=pageID", "Value={pageID}", ENDITEM, ... "Name=btnNext", "Value=Next", ENDITEM, LAST); lr_end_transaction("Wizard_5", LR_AUTO); lr_think_time( 1 ); //----- Finish in the Wizard -------------------------------------- web_reg_find("Text=XXXX", LAST); web_reg_save_param("pageID", "LB=input type=\"hidden\" name=\"pageID\" value=\"", "RB=\"", LAST); lr_start_transaction("Wizard_6"); web_submit_data("Wizard_6", "Action={Protocol}://{System}/app", "Method=POST", "RecContentType=text/html", "Referer={Protocol}://{System}/app", "Snapshot=t46.inf", "Mode=HTTP", ITEMDATA, "Name=pageID", "Value={pageID}", ENDITEM, ... "Name=btnFinish", "Value=Finish", ENDITEM, LAST); lr_end_transaction("Wizard_6", LR_AUTO); lr_think_time( 1 ); // Should now see Submitted (with yellow background) on summary page! unsubmitted = 0; } lr_think_time( 2 ); } //------ Close the page and back out --------------------------------------- web_reg_find("Text=XXXX", LAST); web_reg_save_param("pageID", "LB=input type=\"hidden\" name=\"pageID\" value=\"", "RB=\"", LAST); lr_start_transaction("Close_ExpenseCodingScreen"); web_submit_data("Close_ExpenseCodingScreen", "Action={Protocol}://{System}/app", "Method=POST", "RecContentType=text/html", "Referer={Protocol}://{System}/app", "Snapshot=t50.inf", "Mode=HTTP", ITEMDATA, "Name=pageID", "Value={pageID}", ENDITEM, ... "Name=btnClose", "Value=Close", ENDITEM, LAST); lr_end_transaction("Close_ExpenseCodingScreen", LR_AUTO); /* * Now have a yellow 'S!!!! */ lr_think_time( 1 ); //-------------------------------------------------------------------------- web_reg_find("Text=XXXX", LAST); web_reg_save_param("pageID", "LB=input type=\"hidden\" name=\"pageID\" value=\"", "RB=\"", LAST); lr_start_transaction("Close_InvoiceDetails"); web_submit_data("Close_InvoiceDetails", "Action={Protocol}://{System}/app", "Method=POST", "RecContentType=text/html", "Referer={Protocol}://{System}/app", "Snapshot=t54.inf", "Mode=HTTP", ITEMDATA, "Name=pageID", "Value={pageID}", ENDITEM, ... "Name=btnClose", "Value=Close", ENDITEM, LAST); lr_end_transaction("Close_InvoiceDetails", LR_AUTO); lr_think_time( 1 ); //-------------------------------------------------------------------------- web_reg_find("Text=XXXX", LAST); web_reg_save_param("pageID", "LB=input type=\"hidden\" name=\"pageID\" value=\"", "RB=\"", LAST); lr_start_transaction("Close_InvoiceList"); web_submit_data("Close_InvoiceList", "Action={Protocol}://{System}/app", "Method=POST", "RecContentType=text/html", "Referer={Protocol}://{System}/app", "Snapshot=t59.inf", "Mode=HTTP", ITEMDATA, "Name=pageID", "Value={pageID}", ENDITEM, ... "Name=btnClose", "Value=Close", ENDITEM, LAST); lr_end_transaction("Close_InvoiceList", LR_AUTO); lr_think_time( 1 ); //-------------------------------------------------------------------------- web_reg_find("Text=XXXX", LAST); lr_start_transaction("Close_FlashPage"); web_submit_data("Close_FlashPage", "Action={Protocol}://{System}/app", "Method=POST", "RecContentType=text/html", "Referer={Protocol}://{System}/app", "Snapshot=t63.inf", "Mode=HTTP", ITEMDATA, "Name=pageID", "Value={pageID}", ENDITEM, "Name=btnOk", "Value=OK", ENDITEM, LAST); lr_end_transaction("Close_FlashPage", LR_AUTO); lr_think_time( 1 ); //-------------------------------------------------------------------------- web_reg_find("Text=XXXX", LAST); lr_start_transaction("Logoff"); web_url("Log off", "URL={Protocol}://{System}/app/Logout?LanguageID=31&Licensee=xxxx", "Resource=0", "RecContentType=text/html", "Referer={Protocol}://{System}/app", "Snapshot=t68.inf", "Mode=HTTP", LAST); lr_end_transaction("Logoff", LR_AUTO); lr_think_time( 1 ); //-------------------------------------------------------------------------- return 0; } // CodeExpenses