Selenium IDE - How to handle java-script alerts and confirmations - javascript

I am novice to automation testing and have started using Selenium IDE as the choice for automation.
I just want to know if there is any way to handle java-script alerts using IDE.
Take a scenario: I am clicking on "Delete" button and there comes a java-script alert box with OK and Cancel options, but Xpath can not identified for these elements.On the other hand, when I make a script for click event on delete button and run it using Selenium IDE, the element gets automatically deleted.
So, guys please help. Leave a reply in case my question needs to be elaborated.
Thanks in advance.

Take a look at the documentation. The main part is this:
When running under Selenium, JavaScript pop-ups will not appear. This
is because the function calls are actually being overridden at runtime
by Selenium’s own JavaScript. However, just because you cannot see the
pop-up doesn’t mean you don’t have to deal with it. To handle a
pop-up, you must call its assertFoo(pattern) function. If you fail to
assert the presence of a pop-up your next command will be blocked and
you will get an error similar to the following
[error] Error: There was an unexpected Confirmation! [Chose an option.]
A step-by-step manual is in the section about Alerts.

java-script alerts in ide can be handle using selenium commands storeAlert and
storeConfirmation what it will do is it Retrieves the message of a JavaScript confirmation or alert dialog generated during the previous action. this commands can be put at the position where the alert will occur
command:storeAlert
Target:variableName
command:storeConfirmation
Target:variableName
variable name is the variable in which the occurred alert will store
Thank you..

Related

Check if html file would open JavaScript alert dialog

I'm wondering if it is possible to, in Java, detect whether or not an HTML file would open an alert dialog if opened in the browser. Preferably headlessly. For example, a file with the below contents were parsed, it would return true.
<html><script>alert("hey")</script></html>
and the below would return true also
<html><iframe src="javascript:alert(1)" onload="alert(2)"></iframe></html>
but the below would return false because it would not open an alert dialog if it were opened in the browser (because none of the code is syntactically correct, and the part that is isn't in a tag).
<html><script>alert;,(123w)</script>alert(1)</html>
I have thought of a way to approach this problem, but it is flawed. Basically, you see if the stringalert(1) is in the file, etc.
The problem with this is that it wouldn't work in cases where that code isn't inside of script tags or tags that make it execute. An example of where it wouldn't work is: The following would return true, even though it wouldn't actually open a popup <html>alert(1)</html>.
This isn't Android by the way. Appreciate your help!
You will need to not only verify if the Alert function is there but check if the JavaScript function would even run. An example of this is if there is a script with an Alert function inside a function that never runs. The Alert function would be there but it would never run. This would give a false positive. So the in the best case you should run the JavaScript in some way to validate the code and to see if the function would ever run.
As Louis pointed out in the comments Option 2 is better in this case as you will need to account for both the DOM and JavaScript's behaviour as both can change if the Alert function runs and how it runs.
Option 1 : Run the JavaScript with Script Engine
You would need some way of separating the HTML from the JavaScript but once you have that you can do this method.
You can run the JavaScript in Java using ScriptEngine. https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/prog_guide/api.html
If you read the API there is a way to create variables and communicate between your Java Program and the JavaScript you are Running.
To capture the context of the Alert you can create a custom JavaScript function that overwrites the Alert function. Inside this custom function you can send the arguments of the function back to your Java Program.
Option 2 : Headless Browser
You can also try to use a headless browser like JBrowserDriver and as you can see you have an Alert interface with getText as a function. For async issue the headless browser has a default amount of time for waiting for the script to complete. If this default amount is not enough you can use the setScriptTimeout to handle it.
http://machinepublishers.github.io/jBrowserDriver/

Generic exception submitting form via JavaScript

I am moving an application from JBoss 7.1.q to WildFly. Parts of the navigation are done with by creating a form in JavaScript and submitting it via the submit method. All of these calls still work with the exception of one.
Instead of navigating to the page, I get a message of
A Generic Exception Occurred while accessing this page.
I grepped through the code to see if someone had created a window.onerror function to catch exceptions but could not find one. Even more odd, I added one to the js page and it failed to catch the exception.
I have waded through the JavaScript with Firebug and Chrome dev tools and from what I can see, everything looks to be OK, but clearly it is not.
I know it is not much to go on. I am looking for ideas for the next steps to triage this issue. I also find it curious that this problem did not appear until the move to WildFly but I have no clue how that could cause this issue - maybe the form posting is more strict?
Any ideas?

Programatically retrieve count of javascript errors on page

I'd like to write a test case (using Selenium, but not the point of this question) to validate that my web application has no script errors\warnings or unhanded exceptions at certain points in time (like after initializing a major library).
This information can easily be seen in the debug consoles of most browsers. Is it possible to execute a javascript statement to get this information programatically?
It's okay if it's different for each browser, I can deal with that.
not so far read about your issue (as far as I understood your problem) here
The idea be the following:
I found, however, that I was often getting JavaScript errors when the page first loaded (because I was working on the JS and was introducing errors), so I was looking for a quick way to add an assert to my test to check whether any JS errors occurred. After some Googling I came to the conclusion that there is nothing built into Selenium to support this, but there are a number of hacks that can be used to accomplish it. I'm going to describe one of them here. Let me state again, for the record, that this is pretty hacky. I'd love to hear from others who may have better solutions.
I simply add a script to my page that will catch any JS errors by intercepting the window.onerror event:
<script type="text/javascript">
window.onerror=function(msg){
$("body").attr("JSError",msg);
}
</script>
This will cause an attribute called JSError with a value corresponding to the JavaScript error message to be added to the body tag of my document if a JavaScript error occurs. Note that I'm using jQuery to do this, so this specific example won't work if jQuery fails to load. Then, in my Selenium test, I just use the command assertElementNotPresent with a target of //body[#JSError]. Now, if any JavaScript errors occur on the page my test will fail and I'll know I have to address them first. If, for some strange reason, I want to check for a particular JavaScript error, I could use the assertElementPresent command with a target of //body[#JSError='the error message'].
Hope this fresh idea helps you :)
try {
//code
} catch(exception) {
//send ajax request: exception.message, exception.stack, etc.
}
More info - MDN Documentation

Where does rails log errors in javascript?

I'm fairly new to Ruby on Rails so sorry if the answer is obvious, I couldn't find anything via search. Right now I have my view rendering _box.js.erb which simply draws a box. Inside _box.js.erb, which works correctly under normal circumstances, I introduce a simple syntax error like an unmatched parenthesis. When I load the webpage, my box doesn't show up. I look in logs/development.logs and it has no mention of my javascript syntax error. Is this error being caught somewhere? If so, how can I display it?
The error occurs on the client, not the server, so ROR cannot log it.
If you're using Firefox, install Firebug. Other browsers have JavaScript consoles, usually opened by pressing F-12 or looking on the "developer" options on the application menus.
You could also create a controller to accept javascript errors, so when there's a client side error you can just send it to a javascript error collection endpoint which in turn could take the contents of the error and add it to the Rails log or whatever log.
This old gem does that. I realize this questions is 8 years old.

handling javaScript alerts when running a selenium test?

I am am running some selenium tests(ruby) on my web page and as i enter an invalid characters in to a text box i have the JavaScript throw a alert like so
if(isNaN($(this).val()) || Number($(this).val().valueOf() <=0)){
alert("Please Enter A Number");
}
how can i handle this alert when its made and close the pop up?
i tried to use the wait_for_pop_up() and close() but i think that's only for browser pop up's and not JavaScript alerts.
any ideas?
thanks
The documentation isn't of much help but the Java Docs for Selenium RC seem to show that a getAlert() does exist here. Also this site lists these functions as well:
Processing with Selenium The following commands are available within Selenium for processing Alerts:
getAlert()
assertAlert() assertAlertNotPresent()
assertAlertPresent() storeAlert()
storeAlertPresent() verifyAlert()
verifyAlertNotPresent()
verifyAlertPresent() waitForAlert()
waitForAlertNotPresent()
waitForAlertPresent()
The …AlertPresent() and …AlertNotPresent() functions check for the existence or not of an alert – regardless of it’s content. The …Alert() functions allow the caller to specify a pattern which should be matched. The getAlert() method also exists in Selenium RC, and returns the text from the previous Alert displayed. Similar functions are also available for Confirmations
Selenium intercepts all JavaScript alert() calls (as well as confirm() and prompt()) and handles them itself. You can tell it what you want it to do, and you can find out what happened (excepting during onLoad, as Zugwalt says). You can call isAlertPresent() to check whether an alert was generated or not, and getAlert() to retrieve the text of it. Selenium will even queue them up for you, in case there is more than one (they're retrieved in order). If an alert is generated and you don't call getAlert() to retrieve it, the next Selenium operation will throw an exception. And if you call it when there isn't one, you'll get an exception.
For example (untested, and in C#, but you should get the point):
selenium.GetEval("window.alert('Hi, mom!'); window.alert('Bye, dad!')");
Assert.AreEqual("Hi, mom!", selenium.GetAlert());
Assert.AreEqual("Bye, dad!", selenium.GetAlert());
Assert.IsFalse(selenium.isAlertPresent());

Categories