简体   繁体   中英

How to define / existing javascript function in iMacro

I am new to iMacro so please correct me if my approach with iMacro is incorrect.

I have created some javascript functions that are helpful in testing certain conditions on the DOM. The problem is i am not able to include javascript functions ( from External js file as well as defining on the .js script of imacro ) into my test file and call the functions from js lib during test case execution.

Have you tried using iimGetLastExtract()

You can create a .js file that calls imacros. those macros can extract information that you need from the DOM. using iimGetLastExtract() you can extract those values and test it in the js

when you are in FF you can actually choose a .js file to run from the imacros menu.

Here is an example of an old .js file i used to do just such a thing. im looping through the values of a dynamically created drop down and doing things based on its value. (if a macro fails it will a value less than 0: error codes )

var i = 1;
var notDone = 1;
while(notDone > 0)  {
    //select the board from available
    iimSet('name', boardname);
    iimPlay('selectBoard.iim');
    //pick the next display view and capture the view name
    iimSet('index',i);
    notDone = iimPlay('assignDisplay.iim');

    var displayName = iimGetLastExtract(1),
        inputName = iimGetLastExtract(2),
        label = '';

    if((displayName == '[None]' && displayName == '[None]')  || !notDone) {
        break;
    }
    label = (displayName === '[None]') ? inputName : displayName;
    if(prefix) label = prefix + label;
    if(suffix) label += suffix;
    iimSet('name', label);
    iimSet('btn',btn);
    iimPlay('assignLabelInput.iim');
    i++;
}

If you arent familiar with some of the things in there. im using iimSet() to set variables in the imacros. iimPlay to play that macro. and in the macro itself, here is an example of extracting information

TAG POS=1 TYPE=SELECT FORM=NAME:form1 ATTR=ID:dropdownid EXTRACT=TXT

edit Here is a silly example that will hopefully show the use of js and the imacros extract functionality The code


example.js

var allAnswerVotes=[];
var runningTotal = 0;
var working = true;
var i = 2;
while(working) {
    iimSet('i', i);
    iimPlay('getVotes.iim');

    var extract = iimGetLastExtract(1);
    if(extract === '#EANF#') {
        working = false;
        continue; //hault this iteration;
    }

    var numVotes = parseInt(extract, 10);
    allAnswerVotes.push(numVotes);
    runningTotal += numVotes;
    i++;    //increment i to get the next vote

}
alert('The highest vote is '+ Math.max.apply(null, allAnswerVotes)+', with an average of '+ Math.ceil(runningTotal / allAnswerVotes.length));

getVotes.iim

TAG POS={{i}} TYPE=SPAN ATTR=class:*vote-count-post* EXTRACT=TXT

The explanation


First thing to do is make sure that both of these files (example.js, getVotes.iim) are located in the same folder in order for example.js to run getVotes.iim correctly. next you just have to navigate to any StackOverflow thread, select example.js from the imacros menu (f8 to open the menu) and push play (or simply double click example.js)

the macro will find the i-th position span with class containing "vote-count-post" and return the text of that span. i is a parameter that is passed in by the js. we will start at i = 2, so the second vote span (we are skipping the vote for the question and only counting the votes for answers). the js will continue to call getVotes.iim until getVotes returns an extract value of "#EANF#" which is the return value when the macro cant find the specified tag (ie when there are no votes on the current page). #EANF# will kick us out of our loop and then you get the alert with some basic math on the votes we tallied.

The extracting of votes here is pretty silly but I'm just demonstrating a basic example of how to use imacros EXTRACT in js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM