Selenium: Does it useful to catch NoSuchElementException appears for a clickable element? - selenium

Why do we catch NoSuchElementException? It makes sense to catch the exception when we only verify the presence of an element in a particular page and not performing any actions on it. We can catch this exception and log a custom message like "Element not found"
When we are about to click an element and if it is not found in the page, selenium throws NoSuchElementException. What is the use of catching this exception instead of letting it to halt the test and failing the testcase?
Note: My subsequent steps depends on this clicking step.

One reason to catch the exception is to provide more relevant test failure output. For example, if a div element isn't found in a page of 20-30 divs, cause the selector doesn't match that specific div, you could let the exception remain uncaught with a generic error message or you could catch it and throw a different exception with error message "navbar is missing".
If it is clear from the generic error message what is wrong, or you have a test failure message that explains it, there is no reason to catch it.
A different reason to catch this exception is that it's part of skip code (skip this test if/unless). So you look for an element and if it's present you proceed with the test, if it's not - you skip it.
There may be other reasons, but these are two common ones.

There are two types of Exceptions:
1.Checked Exceptions
2.Unchecked Exceptions
Checked Exceptions are checked at compile time only, these should be handled by the programmer. Compiler will check at compile time whether these exceptions are handled or not if not compile time error occurs. Some of the checked exceptions are IOException, FileNotFoundExpection,etc.
Unchecked Exceptions:
Unchecked exceptions are not checked by compiler at the time of compilation. the exceptions which are extended by RuntimeException class are all unchecked exceptions. Some of the unchecked exceptions are AritmeticException, NullPointerException etc. In selenium we see unchecked exceptions such as NoSuchElementException, StaleElementReferenceException , NoSuchWindowException, TimeoutException etc.
In short, Checked exceptions must be caught. RuntimeException, also called unchecked exceptions, should not be caught
More info can be found on
http://toolsqa.com/selenium-webdriver/exception-handling-selenium-webdriver/
http://www.seleniumeasy.com/java-tutorials/exception-handling-in-selenium-webdriver-using-java-examples

Related

when it is correct purpose of exceptions

I am studying OOP, and I did not understood the concept of exception.
What are the correct uses of exceptions?
Why use exceptions when you already know a possible exception?
For example, I have seen a code sample where the programmer needed to access a file, and had an exception in case the file does not exist. Something like "catch(fileDoesNotExist e)".
Why not use an if to verify before take the action? And use exception only for not known issues, for logging or error messages.
The idea behind the concept of exception was to decouple the error handling code from the "normal" behaviour flow control. That lets to manage/handle the exception further up the call stack.
Historically, with structured language, error handling code (file opening error,...) was mixed within the "business" application code. It was also painful to improve the code in order to manage new error codes.
What are the correct uses of exceptions?
If it is not normal that your file doesn't exist or cannot be opened => it is considered as an exceptional situation => exception => exception handler
Why use exceptions when you already know a possible exception?
To decouple the business application code from the error handling. That eases source code readibility and maintenance.
Exception:
Exception is that interrupt(break) the normal flow of the program.It's thrown at runtime.
Exception Handling
Exception Handling is a mechanism to handle runtime errors such as ClassNotFound, IO, SQL, Remote etc
In java there are mainly two types of exception that checked and unchecked.Other than Error is there
Hierarchy of Exception classes in Java
Why use exceptions when you already know a possible exception?
basically exception handling use to mainly,we assuming in that our particular code will occur some(NullPointerException,ArrayIndexOutOfBoundsException etc..)exception.If we not Handle that,program will break.Actually that Exception it may or may not will happen.But so we need to handle normal flow of the program it occurred or not.Otherwise after that particular code section not executing.

Mule global catch exception not being called

I'm using the global exception strategy at this link
Global Catch Exception Strategy is not used
I use a ref to this in each of my flow footers and have also used defaultExceptionStrategy-ref="catchExceptionStrategy" as shown in the link. But this exception
org.mule.exception.DefaultSystemExceptionStrategy: Caught exception in Exception Strategy: null
java.util.ConcurrentModificationException
...
is not being caught by my global exception. My assumption was that this is a message exception and the flow refs would therefore direct it to the global catch. Also that the defaultExceptionStrategy-ref configuration would direct any other exceptions to the global catch.
The log you are showing proves that the exception strategy is actually called but there has been a concurrent modification exception on it. In order to help you further we would need to understand better what is in you message when the exception happens and the actual xml of your exception strategy.

How to debug exception on other thread?

I have a lot of shortlived threads that updates my program, by events fired from a socket connection. My problem is I don't know how to debug this, like how to get the proper information on where in the code the exceptions occur. Because I get for example an exception like below, and this will just be the print in the Immidiate Window. There is no pop-up window, and it does not highlight any line in the code or show even what method it was in.
What am I missing? What I can do to see these things? And what improvements does VS2010 give on this situation, if any?
A first chance exception of type 'System.InvalidOperationException' occurred in System.Core.dll
A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll
A first chance exception of type 'System.Threading.ThreadAbortException' occurred in Krs.Ats.IBNet.dll
Go to "Debug -> Exceptions...", locate the "Common Language Runtime Exceptions" and check the "Thrown" mark. Now start debugging your application. Once any of CLR exception occurs the execution will stop on that line.

Exceptions from WCF

What exceptions can be thrown from a WCF client?
I usually catch CommunicationFaultedException, CommunicationException, TimoutException and some other but from time to time new ones occur, e.g. most recently QuotaExceededException
There is no common base to catch (except Exception) so does anyone have a complete list?
This might be a good place to start: Expected Exceptions.
Why would there be a complete list? This isn't Java.
Why do you want to catch an exception you don't understand? How would you "handle" it if you don't know what it means?
Go ahead and catch exceptions to log them, if you like, but you should rethrow after you catch it. Let the exception propagate up to some code that knows what to do with it.
Just thinking outloud... one solution could be:
Add the list of exceptions(and exception casting) in your Custom exception class; for instance CException.
As soon as you catch an exception in your Exception block, throw another exception into your CException class. For instance like following:
catch(Exception ex){throw new CException("An error occured", ex);}
See this example.
The CommunicationException is the base exception for all WCF exceptions. If you catch that, you catch everything WCF related.
See the MSDN docs for CommunicationException. It will also nicely show a list of all derived classes, e.g. all more specific exceptions that can occur in WCF - quite a long list!

A First Chance Exception

've been running through the MSDN help documents to get a hang of Visual Basic. After trying out the example using timers --one drags a label and timer component into the designer and adds the following to the components subroutine
Label1.Text = My.Computer.Clock.LocalTime.ToLongTimeString
The output for the immediate window during debug is the following
A first chance exception of type
'System.InvalidCastException' occured
in Microsoft.VisualBasic.dll
A first
chance exception of type
'System.InvalidCastException' occured
in Microsoft.VisualBasic.dll
The same error occurs on a previous MSDN example using a context menu component. Should I Try...Catch...Finally this error and try to move on? Or, am I dealing with something much more serious?
When you see something about a first chance exception, it only means that an exception was caught within the code you called but does not necessarily mean that the code failed. If the code runs without causing your program to crash and returns a valid value, then do not have a problem. You will also see output in the debug window about first chance exceptions when you implement your own try/catch blocks.
In the Debug menu -> Exceptions, you can enable the debugger to stop when an Exception is first thrown, even if it would be caught later; if you want to find out what's happening, this is the easiest way to do it
In the first chance exception examine the details of the exception. You should see a stack frame/trace property. In there you should see what line the error occurs on. This should help you.
In the IDE try going to Tools > Options > Projects and Solutions > VB Defaults and setting Option Strict to 'On' - this may help catch casting problems when you compile your project rather than when you run it.
A 'first chance execption' does not necessarily mean you have a problem in your code. It could mean the IDE or the compiler or any other involved component encountered and handled an error and in the process the debugger is notified and the exception is being reported to the immediate window. This is an excellent post on the topic:
http://blogs.msdn.com/davidklinems/archive/2005/07/12/438061.aspx
A quick and easy solution for debug and diag of First Chance Exception is :
AppDomain.CurrentDomain.FirstChanceException += CurrentDomainOnFirstChanceException;
and then
private void CurrentDomainOnFirstChanceException(object sender, FirstChanceExceptionEventArgs firstChanceExceptionEventArgs)
{
if (firstChanceExceptionEventArgs.Exception is NullReferenceException)
{
// do your handling and debugging :)
}
}
Multiple First Chance Exception during the runtime can cripple the performance of your application because exception handling is expensive. Especially in web apps. You can add this handler and look at specific first chance exceptions and try to avoid/correct them.