Prateek.Naik
Prateek.Naik

Reputation: 556

How to handle browser prompts in CasperJS

The scenario is, when I click a button browser would show an alert which accepts user input field with OK and cancel buttons. Now please tell me how to handle this type of alert. As we know CasperJS doesn't displays the alert windows. This is the prompt window

This is the casperJS code

casper.then(function () {
        this.click('#new-asset > a:nth-child(1)');
        casper.setFilter("page.prompt", function(msg, currentValue) {
            if (msg === "Choose a filename for your asset") {
                return "Firsr.txt";
            }
        });
    }); 

Upvotes: 2

Views: 1060

Answers (1)

Artjom B.
Artjom B.

Reputation: 61952

You can easily solve this by using a Filter in CasperJS. The appropriate one is page.prompt:

// put somewhere before the prompt appears
casper.setFilter("page.prompt", function(msg, currentValue) {
    if (msg === "What's your name?") {
        return "Chuck";
    }
});

Such a dialog is called a prompt (window.prompt()) which is distinct from an window.alert() or window.confirm().

Upvotes: 3

Related Questions