waitForUnless - Enhancement suggestion

9 views
Skip to first unread message

Mark Perryman

unread,
Apr 2, 2018, 9:43:22 AM4/2/18
to CasperJS
I would like a new function: waitForSelectorUnless that behaves in the same way as waitForSelector except for an extra parameter which, if the selector expression exists, stops waiting but does not process the `then` parameter.

I am using this function when navigating a menu with pages that are optional, and it feels like a useful functionality that could be included in CasperJS itself.

 This would be used as follows, e.g.

waitForSelectorUnless(
    selector, // Selector that appears only on optional page
    then, // Function that processes optional page and clicks through to following page
    unlessSelector // Selector that appears on mandatory page
);
waitForSelector(
    selector, // Selector that appears on mandatory page
    then // Function that processes mandatory page
);

  
A similar function waitForUnless would be a suitable counterpart.

A quick suggested implementation is as follows.

waitForUnless = function waitForUnless(testFx, then, unlessFx, onTimeout, timeout, details) {
    "use strict";
    return casper.waitFor(
        function () { return testFx() || unlessFx(); },
        function () { if (unlessFx()) return; else then(); },
        onTimeout,
        timeout,
        details
    );
};

function waitForSelectorUnless(selector, then, unlessSelector, onTimeout, timeout) {
    "use strict";
    return casper.waitForUnless(
        function () { return casper.exists(selector); },
        then,
        function () { return casper.exists(unlessSelector); },
        onTimeout,
        timeout,
        { selector: selector, unlessSelector: unlessSelector }
    );
};

Ken Soh

unread,
Jul 26, 2018, 1:22:44 AM7/26/18
to CasperJS
[ARCHIVE] - dropping a quick note that CasperJS, similar to PhantomJS is not actively maintained for now.
Reply all
Reply to author
Forward
0 new messages