Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clicking 'OK' on alert or confirm dialog through jquery/javascript?

I was thinking of writing some UI tests in backbone.js and jquery. They may not be the best way to do it but it's something that I was thinking about - to automate the tests without record and playback - through plain code.

The only thing that made me scratch my head using this approach is this: In some 'use-case flow' (of the execution) confirm/alert dialogs would show up. I'd like to click 'Ok' and continue the flow - is this even doable through plain javascript code? How?

Note: I do know GUI testing libraries exist, but I want to know how to do it using just jQuery/javascript code, if at all possible.

like image 208
PhD Avatar asked Oct 09 '11 19:10

PhD


People also ask

How add confirm in jQuery?

confirmBox. dialog ({ autoOpen: true, modal: true, buttons: { 'Yes': function () { $(this). dialog('close'); $(this). find(".

How do I change the OK and Cancel on confirm box?

confirm() method, which contains the confirmation message string, ok, and cancel button. The programmer can't change the confirm box style and button label if they use the default confirm box.

How do you ask the user to accept something by clicking on a OK button or not accept by clicking on a cancel button?

The confirm() method displays a dialog box with a message, an OK button, and a Cancel button. The confirm() method returns true if the user clicked "OK", otherwise false .

Can we use alert in jQuery?

alert() Sometimes you need to go back to basics and use the . alert() function in jQuery to see what a variable is or check if your JavaScript is actually working at all. Probably one of the most underated functions, but certainly the most used, is jquery.


2 Answers

As far as I know if you use a standard alert() call you cannot trigger an "OK" click because the alert call blocks the normal JS event loop.

However you should be able to replace window.alert and window.confirm with your own function that does nothing:

window.alert = function() {
    console.log.apply(console, arguments);
};

Place these at the top of your JS before anything else is loaded and any subsequent calls to alert() or confirm() will call these instead.

like image 158
Alnitak Avatar answered Oct 22 '22 00:10

Alnitak


You want something like:

<script type="text/javascript">
var oldConfirm = confirm;
var oldAlert = alert;

confirm = function() {
    return true;
};
alert = function() {
    return true;
}

var response = confirm("Is this OK?");

if (response) {
    alert("Yay");
}
else {
    alert("Boo");
}

confirm = oldConfirm;
alert = oldAlert;
</script>
like image 28
John Kurlak Avatar answered Oct 22 '22 02:10

John Kurlak