What is an Exception
An Exception event occurs during the execution of a program. that disrupts the standard movement of the program’s instructions or simple words. Any issue makes test case stop between execution is an Exception.
Difference between Error and Exception
Error “indicates serious issues reasonable request should not make an effort to catch”. An Exception “indicates conditions a reasonable application should catch”.
Whenever an error occurs while performing a statement, creates an exception subject. then the normal movement of the program halts and it will try to find handle the increased exception. The exception thing contains a lot of debugging information. such as method hierarchy, series number exception occurred, kind of exception. the process of fabricating exception and handing runtime environment called “throwing the exception”.
Exception Handling in Selenium
Exception handling refers to recognition, resolution of programming software, and communications errors. It’s the stop of code that processes the exception object and helps us by giving us a chance to act on it Exception Handling in Selenium.
Why Exception Handling is essential
1) Think about a predicament got an exception. so want to print some custom concept in logs, so that it can be understandable by the whole team.
2) Their situations want to eat up the exception and want the test to carry on with remaining execution.
3) To execute the group of steps on developing of the certain exception. I have an exception because a product has gone out of stock. that product is no longer exhibited on the site and choose another product available on the page.
4) In case want to take care of some certain kind of exception in Selenium. like ElementNotSelectableException, ElementNotVisibleException, NoSuchElementException. exclusions.
Different Exceptions in Selenium
There is a complete list of Exceptions stated on the Selenium Doc. which may or might not encounter in the span of testing.
Most common Exceptions:
1) NoSuchElementException: FindBy method can’t find the component.
2) StaleElementReferenceException :
This explains to that element is no longer showing up on the DOM page.
This explains to the execution failed. Because the command word did not complete in enough time.
Thrown to point that although an aspect is present on the DOM. it is not visible, so struggles interacted with
Thrown to point that may be the component disabled, so struggles to select.
The way to handle Exception
Try/Get: A method catches an exception by using a mixture of the try to get keywords. Try is the beginning of the block and Catch reaches the end of try stop to take care of the exceptions. A try/catch block located across the code that may create an exception. Code inside a try/catch block referred to as protected code,
Many Capture blocks: A try stop followed by many get blocks. there are many exceptions and expect more than one type of exception about the same code block. if like to handle each kind of exception with a separate block of code.
The exception inside our code Selenium Automation Construction most of the time. we printing self-written logs, capture an exception and exception back to the system. so that the test case terminated. Throw keyword used to put the exception to the runtime to take care of it.
Throws: Whenever we are tossing any exception in a method and not controlling it. then we have to use throws keyword in method personal to let caller program know the exclusions. That thrown by the method.
Many Exceptions: We can offer many exclusions in the throws clause.
Finally: The final keyword employed to make a block of code that comes after a try block. A final block of code always executes, whether an exception has occurred.
Exception Handling in Selenium
Selenium test can fail, but, not because of exceptions that thrown. In case your test is failing from exclusions. then most haven’t any exception handling. Using this method, you do not have the possibility to clean up the WebDriver object by the end of the test.