iMacros Snippets

iMacros is one of the popular web automation software developed by Ipswitch, Inc. iMacros is available as an free extension for the Mozilla Firefox, Google Chrome, and Internet Explorer web browsers. iMacros can record and replay repetitious work, which is great for regression testing and integration testing. iMacros snippets can be used for modifying the parameters after a workflow is recorded.

NOTE: I used iMacros for Firefox extension v8.9.7 following steps at iMacros wiki. Some of the snippets may not work in newer versions of iMacros for Firefox and iMacros for Chrome

!NOW

The built-in variable !NOW provides the current date and time. Date and time can be formatted using the format codes.

VERSION BUILD=8970419 RECORDER=FX
TAB T=1
URL GOTO=http://arunelias.in/wp-content/uploads/2017/11/GenerateReport.php
TAG POS=1 TYPE=INPUT:TEXT FORM=ID:demo ATTR=ID:txtName CONTENT=Tester<SP>{{!NOW:hhnnss}}
TAG POS=1 TYPE=INPUT:TEXT FORM=ID:demo ATTR=ID:dtReport CONTENT={{!NOW:mm/dd/yyyy}}
TAG POS=1 TYPE=BUTTON ATTR=TXT:Generate<SP>Report

 

Date

Current Date can be obtained from the built-in variable !NOW. I am using the JavaScript function Date() to generate date here. Using the setDate() function Tomorrows date can be generated.

VERSION BUILD=8970419 RECORDER=FX
TAB T=1
'// Todays Date in M/D/YYYY format
SET todaysdate EVAL("var d = new Date();var result = (d.getMonth()+1)+'/'+d.getDate()+'/'+(d.getYear()+1900); result;")
PROMPT {{todaysdate}}
'// Tomorrows Date in M/D/YYYY format
SET tomorrowsdate EVAL("var t = new Date(); var d = new Date(); d.setDate(t.getDate()+1); var result = (d.getMonth()+1)+'/'+d.getDate()+'/'+(d.getYear()+1900); result;")
PROMPT {{tomorrowsdate}}
'// Todays Date in MM/DD/YYYY format
SET todaysdate EVAL("var d = new Date();var result = (('0' + (d.getMonth() + 1)).slice(2))+'/'+(('0' + d.getDate()).slice(2))+'/'+(d.getYear()+1900); result;")
PROMPT {{todaysdate}}
'// Tomorrows Date in MM/DD/YYYY format
SET tomorrowsdate EVAL("var t = new Date(); var d = new Date(); d.setDate(t.getDate()+1); var result = (('0' + (d.getMonth() + 1)).slice(2))+'/'+(('0' + d.getDate()).slice(2))+'/'+(d.getYear()+1900); result;")
PROMPT {{tomorrowsdate}}

 

Random Values

Random Values for variables can be generated using the JavaScript function Math.random().

VERSION BUILD=8970419 RECORDER=FX
TAB T=1
'//Generate Random Number between 10 to 99
SET number EVAL("var randomNumber=Math.floor(Math.random()*(9910+1) + 10); randomNumber;")
PROMPT {{number}}
'//Generate 10 digit phone number
SET phoneno EVAL("randomString(10, '0123456789'); function randomString(length, chars) { var result = ''; for (var i = length; i > 0; i) result += chars[Math.round(Math.random() * (chars.length 1))]; return result; };")
PROMPT {{phoneno}}
'//Generate 5 digit alphanumeric text
SET alphanum EVAL("randomString(5, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'); function randomString(length, chars) { var result = ''; for (var i = length; i > 0; i) result += chars[Math.round(Math.random() * (chars.length 1))]; return result; };")
PROMPT {{alphanum}}

 

Select a Random Option from DDL

Random options from a Drop-Down List can be selected using the JavaScript function Math.random(). I have duplicated the options 10 times to get better randomness.

VERSION BUILD=8970419 RECORDER=FX
TAB T=1
'//Select from 3 of 4 options [exculding 1st option –select–] by random in ddlType DDL
SET ddl4options EVAL("randomString(1, '234234234234234234234234234234'); function randomString(length, chars) { var result = ''; for (var i = length; i > 0; i) result += chars[Math.round(Math.random() * (chars.length 1))]; return result; };")
TAG POS=1 TYPE=SELECT FORM=ID:form1 ATTR=ID:ddlType CONTENT=#{{ddl4options}}

 

Random Words

Random words can be generated from an array of words using the JavaScript function Math.random().

VERSION BUILD=8970419 RECORDER=FX
TAB T=1
'//Genearate Random words from an Array list
SET randomwords EVAL("var textArray = ['Secure','Site','Information','Service','Purpose','Warranty','Data','System','Referenced','Bring','Correct','Agree','Prevent'];var randomIndex = Math.floor(Math.random()*textArray.length);var result = textArray[randomIndex];randomIndex = Math.floor(Math.random()*textArray.length);result = result+'<SP>'+textArray[randomIndex];result;")
PROMPT {{randomwords}}
'//Generate 5 Random words from an Array list
SET randomwords EVAL("randomWords(5, ['Secure','Site','Information','Service','Purpose','Warranty','Data','System','Referenced','Bring','Correct','Agree','Prevent','Secure','Site','Information','Service','Purpose','Warranty','Data','System','Referenced','Bring','Correct','Agree','Prevent','Secure','Site','Information','Service','Purpose','Warranty','Data','System','Referenced','Bring','Correct','Agree','Prevent']); function randomWords(words, txtArray) { var result = ''; for (var i = words; i > 0; i) result += txtArray[Math.floor(Math.random()*txtArray.length)]+'<SP>'; return result; };")
PROMPT {{randomwords}}

 

Extract User Name or Password from a Paragraph

The Content can be extracted using the EXTRACT=TXT of TAG command. This extracted content can be searched for User Name or Password etc using the JavaScript function string.lastIndexOf(). This example uses a Username of fixed 10 character length in a span element of id=txtContent. eg. User name: HelloWorld. Run the iMacros code in this page, for the Demo.

VERSION BUILD=8970419 RECORDER=FX
TAB T=1
'//Extract Username from a Paragraph –> Hello User, Please use your User name: <<10 character username>> and login to your portal
SET !EXTRACT NULL
TAG POS=1 TYPE=SPAN ATTR=ID:txtContent EXTRACT=TXT
SET username EVAL("var str = '{{!EXTRACT}}'; var strSearch='User name: '; var strUnameloc=str.lastIndexOf(strSearch); var strUnametmp=str.substring((strUnameloc+strSearch.length),(strUnameloc+strSearch.length+10)); if (strUnametmp != 1) {strUname=strUnametmp;} else {strUname='NOT FOUND!!!';} strUname;")
PROMPT {{username}}

 

Encode and Decode in Base64 format

Following is a basic script for Encoding and Decoding in Base64 format. The Encode is useful while testing some applications which encode content before transmitting to the server. The Decode is useful for processing the Encoded Content. Decode option will generate unwanted output if the input is supplied incorrectly, this occurs because Error Correction is not implemented.

VERSION BUILD=8970419 RECORDER=FX
TAB T=1
'//Encode and Decode Entered Text in Base64 format
PROMPT "Please enter Text to Encode in Base64 format:" !VAR1
SET txtInput {{!VAR1}}
SET Encode64 EVAL("Encode64('{{txtInput}}'); function Encode64(input) {var keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; var output = ''; var chr1, chr2, chr3 = ''; var enc1, enc2, enc3, enc4 = ''; var i = 0; do {chr1 = input.charCodeAt(i++); chr2 = input.charCodeAt(i++); chr3 = input.charCodeAt(i++); enc1 = chr1 >> 2; enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); enc4 = chr3 & 63; if (isNaN(chr2)) {enc3 = enc4 = 64;} else if (isNaN(chr3)) {enc4 = 64;} output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4); chr1 = chr2 = chr3 = ''; enc1 = enc2 = enc3 = enc4 = '';} while (i < input.length); return output; };")
PROMPT {{Encode64}}
'//Decode Base64 Encoded Text.
PROMPT "Please enter Base64 Text to Decode:" !VAR1
SET base64Input {{!VAR1}}
SET Decode64 EVAL("Decode64('{{base64Input}}'); function Decode64(input) {var keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; var output = ''; var chr1, chr2, chr3 = ''; var enc1, enc2, enc3, enc4 = ''; var i = 0; do { enc1 = keyStr.indexOf(input.charAt(i++)); enc2 = keyStr.indexOf(input.charAt(i++)); enc3 = keyStr.indexOf(input.charAt(i++)); enc4 = keyStr.indexOf(input.charAt(i++)); chr1 = (enc1 << 2) | (enc2 >> 4); chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); chr3 = ((enc3 & 3) << 6) | enc4; output = output + String.fromCharCode(chr1); if (enc3 != 64) { output = output + String.fromCharCode(chr2);} if (enc4 != 64) { output = output + String.fromCharCode(chr3);} chr1 = chr2 = chr3 = ''; enc1 = enc2 = enc3 = enc4 = '';} while (i < input.length); return output;};")
PROMPT {{Decode64}}

All the scripts are merged as a single file below.