How to check to see if there is a window alert? - javascript

Is there a way to use jquery to check to see if there is an alert popup currently engaged?
I have a form that throws an alert window if there are validation errors before submit.
I need to run a function IF this alert window does not appear, however my script file is separate from the script file that throws the alert.
I can't actually go in and edit the validation function, so I am trying to write a script that loads separately.
Ideally, I'd like to run a function that says "on click of the submit button, if no alert is detected, run this function"
Any suggestions on how to detect this alert? Thank you!

Related

How to storeConfirmation in popup SeleniumIDE

Can u help me about StoreConfirmation for popup SeleniumIde ? + example command.
please answer only selenium IDE.
Thanks
If you are talking about default javascript alert or confirmation there are two commands for it:
storeAlert
Returns:
The message of the most recent JavaScript alert
Retrieves the message of a JavaScript alert generated during the
previous action, or fail if there were no alerts.
Getting an alert has the same effect as manually clicking OK. If an
alert is generated but you do not consume it with getAlert, the next
Selenium action will fail.
Under Selenium, JavaScript alerts will NOT pop up a visible alert
dialog.
Selenium does NOT support JavaScript alerts that are generated in a
page's onload() event handler. In this case a visible dialog WILL be
generated and Selenium will hang until someone manually clicks OK.
storeConfirmation
Returns:
the message of the most recent JavaScript confirmation dialog
Retrieves the message of a JavaScript confirmation dialog generated
during the previous action.
By default, the confirm function will return true, having the same
effect as manually clicking OK. This can be changed by prior execution
of the chooseCancelOnNextConfirmation command.
If an confirmation is generated but you do not consume it with
getConfirmation, the next Selenium action will fail.
NOTE: under Selenium, JavaScript confirmations will NOT pop up a
visible dialog.
NOTE: Selenium does NOT support JavaScript confirmations that are
generated in a page's onload() event handler. In this case a visible
dialog WILL be generated and Selenium will hang until you manually
click OK.
You can see working example here : http://www.software-testing-tutorials-automation.com/2013/10/selenium-ide-what-is-use-of.html
Hope it will help you.

Is it possible to redirect in the middle of an alert box?

I have an alert box that pops up, warning the user that they will be redirected soon.
However, redirection only happens after they click OK.
I want redirection to happen while the alert box is popped up, not after. Also, there should also be enough time for the user to read the alert in the first place (I allowed 2.5s). Any advice?
Code below:
setTimeout("redir()", 2500);
alert("You are being redirected.");
function redir () {
window.location = "target.php";
}
The standard alert box halts page execution, so you can't redirect while it's showing. The user has to first press ok.
You have to come up with a custom message if you want this behavior.
You can have a look at a library called toastR https://github.com/CodeSeven/toastr
It lets you define callbacks for when a message is shown/hidden.
You could use a dialog box instead of the native alert box. For example, jQuery UI: http://jqueryui.com/dialog/
don't use an alert. display a note via a div or span on top of the page... you aren't waiting for their input/acceptance of the fact they are being redirected.

Scripts not running in FF and Chrome

I am using javascript alert message to show validation messages. In Firefox and Chrome first time working fine,second time for same alert same its asking message like "Prevent this page from creating additional dialogs" with check box. After select that check box, Next time button click scripts not executing. How to block that message?
Use a JavaScript Modal popup! eg. JQuery UI Modal Popup
This is a browser matter, so you as a developer cant do anything with that behavior.
Here is a similar question
already answered here
unfortunately you can't be sure that user has his browser settings with javascript alerts popup on ( that called with 'alert('...') function').
You should use javascript dialog plygin instead.
For example:
http://fancybox.net/
its a browser property for the client,if he doesnt want to view these alerts. you cant remove that check box and message.
if this message is shown then what the problem, leave it for the user.
why you want to force him to view these alerts,
it must be user's wish to see or not see these alerts.
for better user experience and for your purpose you can use fancybox or facebox
fancy box fiddler check this http://jsfiddle.net/BJNYr/

Selenium IDE 1.8.1 Alert box : cannot see the alert box while running the tests

I am writing the tests in selenium and Junit and importing the tests as Junit4 Remote control for web application , while I am running the tests it passes the steps but I am not able to see the alert opening but the test passes.
Why I am not able to see the alet box while running the tests??? I will be thankful to the answers.
assertEquals("Please enter valid city ", selenium.getAlert());
Under Selenium, JavaScript alerts will NOT pop up a visible alert dialog. Selenium does NOT support JavaScript alerts that are generated in a page's onload() event handler. In this case a visible dialog WILL be generated and Selenium will hang until someone manually clicks OK.
to verify your alert use this: storeAlertPresent(variableName)
Retrieves the message of a JavaScript alert generated during the previous action, or fail if there were no alerts : storeAlert ( variableName )
So now you can assert this stored alert.
And There is one more command present, If you want to click on cancel button instead of OK button: chooseCancelOnNextConfirmation()

Firefox not executing statements after submit

I have JSP where i submit a form using javascript. Its a popup window so after submission popup must be closed. I used following code:
document.forms["formname"].submit();
window.close();
Problem is - Form gets submitted successfully but window doesnt get closed.
Its work fine in IE.
I have a doubt how it is working in IE.
Javascript doesn't have any control after/until you submit the form and control is returned back to the browser. You should use some another mechanism to close the window after submission of form.
Instead of closing the window from a script running in the context of the window to be closed, try closing it from the context of the window that opened it. For example:
var popup;
function pop() {
popup = window.open('foo', 'mypopup', 'menubar=no,location=no,resizable=no,scrollbars=no,status=no');
console.log(popup);
}
function unpop() {
popup.close(); // close the popup
}
This works for me in Firefox. You can test it live here.
As you've discovered, once you submit the form, you've turned over control to the browser and there is no guarantee that any more javascript in that page will execute. It may just immediately start loading the result page. As such, you have several options:
You can make the result page close itself. So, when the result page loads after the submit, it contains its own window.close().
You can use ajax instead of a form submit to send the data to your server. Then, the lines after the ajax code sends the data will execute. To be safe, you may want to wait until the ajax call returns successfully before closing the window (depending upon what you're doing and how important it is to know whether it succeeded or not).
You can control things from the parent window that opened the form and close things from there.

Categories