Prateek.Naik
Prateek.Naik

Reputation: 556

Handlig multiple window.prompts at the same time using CasperJS

I need to rename an existing group. While creating group there was only single window prompt I used below code to handle

casper.setFilter("page.prompt", function (msg, currentValue) {
    if (msg === "Enter new group name") {
        return ID;
    }
});

Now I want to rename the created group with new name and it throws window prompt like below 1st window prompt

and after clicking on OK it throws another prompt window like below second  prompt window

How do I handle this situation?

and this is my code after @artjom B suggestion

casper.setFilter("page.prompt", function (msg, currentValue) {
    if (msg === "Enter new group name") {
        return t;
    }
});

casper.setFilter("page.confirm", function (msg) {
    return msg.indexOf("Are you sure you want to rename group "+ID1+" to "+t+"?") !== -1;
});

casper.then(function () {
    this.click("span.label:nth-child(4)");
    console.log("Clicking on Rename button");
});

Upvotes: 2

Views: 235

Answers (2)

Sanket Deshmukh
Sanket Deshmukh

Reputation: 186

This may help you. I just removed validation part from second filter.

casper.then(function () {
this.setFilter("page.prompt", function (msg, currentValue) {
    if (msg === "Rename group "+ID1) {          
        this.wait(5000)   
        this.echo("I'm Here")                                                                            
        return t;
    }
})
this.setFilter("page.confirm", function (msg) {                                        
    return true                           
})
this.click("span.label:nth-child(4)");
console.log("renamed the newly created group");
});

Upvotes: 2

Artjom B.
Artjom B.

Reputation: 61952

I suspect the second window is a confirmation dialog (confirm()), so you can additionally register the "page.confirm" filter:

casper.setFilter("page.prompt", function (msg, currentValue) {
    if (msg === "Enter new group name") {
        return ID;
    }
});

casper.setFilter("page.confirm", function (msg) {
    return msg.indexOf("Are you sure you want to rename group \"" + groupName + " to \"" + newGroupName + "\"?") !== -1;
});

The return value of the callback determines whether you confirm or not. If you don't care what the message is, then you can return true.

Additionally, it may be necessary to wait a little after you click the button which triggers the prompt() which then triggers the confirm(). Those event handlers are asynchronous and they run outside of the CasperJS control flow. So you need to let CasperJS wait a little after the click in order to let those handlers execute. casper.thenClick(selector).wait(100) should be enough.

Upvotes: 0

Related Questions