Dynamically activating QMenu entries

Last edited on

Example

The example below demonstrates retrieving and activating the entries in a QMenu.

function main()
{
    app = "addressbook";
    path = OS.getenv("SQUISH_PREFIX") + "/examples/qt4/addressbook";
    s = '"' + OS.getenv("SQUISH_PREFIX") + '/bin/squishserver" --config addAUT ' + app + ' "' + path + '"';
    if (OS.name == "Windows") {
        s = '"' + s;
    }
    OS.system(s);
    startApplication(app);
    
    fm_qaction_name = "{text='File' type='QAction' visible='true'}";
    fm_qaction = waitForObject(fm_qaction_name);
    fm_qmenu = fm_qaction.menu();
    actions = fm_qmenu.actions();
    for (var i = 0; i < actions.size(); ++i) {
        action = actions.at(i);
        
        if (action.isSeparator()) {
            test.log("Ignoring separator");
            continue;
        }
        
        item_name = action.text.toString().replace("&", "");
        
        if (!action.enabled) {
            test.log("Ignoring disabled: File | " + item_name);
            continue;
        }
        
        if (item_name == "Open...") {
            test.log("Ignoring: File | " + item_name);
            continue;
        }
        
        test.log("Opening menu File");
        activateItem(fm_qaction);
        snooze(1);
        
        test.log("Activating: File | " + item_name);
        item = waitForObjectItem(fm_qmenu, item_name, 3000);
        activateItem(item);
        snooze(1);
        
        if (item_name == "Quit") {
            test.log("AUT quit; breaking out of loop to avoid accessing stale objects");
            break;
        }
    }
}