Exception as the word indicates, is a special or uncommon case.
We can encounter exceptions due to network stability issues, Internet issues, server stability etc. We might get exceptions due to insufficient waiting time or incorrect syntax, parameters etc.
Exceptions are events due to which java program ends abruptly without giving expected output. Java provides a framework where a user can handle exceptions.
The process of handling Exceptions is called Exception Handling.
Types of Exceptions
- Checked
- Unchecked
- Error
Checked Exception: Checked exception is handled during compile time and it gives the compilation error if it is not caught and handled during compile time.
Example: FileNotFoundException, IOException etc.
Unchecked Exception: In case of the unchecked exception, a compiler does not mandate to handle. The compiler ignores during compile time.
Example: ArrayIndexoutOfBoundException
Error: When a scenario is fatal and the program cannot recover then JVM throws an error. Errors cannot be handled by the try-catch block. Even if the user tries to handle the error by using Try catch block, it cannot recover from the error.
Example: Assertion error, OutOfMemoryError etc.
Exceptions in Selenium
- NoSuchElementException
The exception occurs when WebDriver is unable to find and locate elements. Usually, this happens when tester writes incorrect element locator in the findElement(By, by) method.
- NoSuchWindowException
This is thrown when WebDriver tries to switch to an invalid window.
- NoSuchFrameException
When WebDriver is trying to switch to an invalid frame we get this exception.
- NoAlertPresentException
NoAlertPresentException is thrown when WebDriver tries to switch to an alert, which is not available.
- InvalidSelectorException
This subclass of NoSuchElementException class occurs when a selector is incorrect or syntactically invalid. This exception occurs commonly when XPATH locator is used.
- ElementNotVisibleException
ElementNotVisibleException class is a subclass of ElementNotInteractableException class. This exception is thrown when WebDriver tries to perform an action on an invisible web element, which cannot be interacted with. That is, the web element is in a hidden state.
- TimeoutException
This exception occurs when a command completion takes more than the wait time. Waits are mainly used in WebDriver to avoid the exception ElementNotVisibleException.
Sometimes test page might not load completely before next command in the program. If WebDriver tries to find an element in the webpage before the page completely loads, then exception ElementNotVisibleException is thrown. To avoid this exception, waits commands are added.
However, if the components don’t load even after the wait, the exception org.openqa.selenium.TimeoutException will be thrown.
- InvalidElementStateException
Indicates that aWebElement
is in a state that means actions cannot be performed with it.
For Example: if the textbox is disabled and if you try to perform type operation then it will throw an exception.
Ways to Handle:
– Always try to perform the required operation based on element state
– If it is clickable element then perform click and if it supports type event then perform sendkeys
– If the element is disabled then enable it first and then perform operations
– In our case, if we want to pass the data forcefully then we can do this using JavascriptExecutor
- Illegalstateexception
The root cause of java.lang.illegalstateexception is we have not specified the path of the driver with the system property.
- StaleElementException
StaleElementReferenceException is thrown when an object for a particular web element was created in the program without any problem and however, this element is no longer present in the window. This can happen if there was a
– Navigation to another page
– DOM has refreshed
– A frame or window switch
Solution:
– You can refresh the page and again try for the same element.
– Sometimes it takes the time to attach element on Dom so you can retry using for loop and try catch.
- ElementNotClickableException at point(x,y)
This exception can be encountered due to below reasons. Plus this exception mostly happens in Chrome.
– Chrome does not calculate the exact location of element
– Chrome always click in the middle of Element
Solution:
– Update chromedriver
– Get the coordinate then click on the link or button using x/y coodinates using javascriptExecutor
Most Basic Exception in Java
Null Pointer Exception
NullPointerException is a runtime exception and it is thrown when the application try to use an object reference which has a null value.
public class Tester {
public static void main(String[] args) {
Object ref = null;
ref.toString(); // this will throw a NullPointerException
}
}