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 }
);
};