http://www.softwaretestinghelp.com/exception-handling-framework-selenium-tutorial-19/
In last WebDriver tutorial we learned about 3 different types of important web elements like Web Tables, Frames and Dynamic elements and their handling mechanisms in selenium script
Before moving ahead with Framework tutorials in this Selenium training series, here in this tutorial we will learn about types of exceptions and how to handle exceptions in java and Selenium scripts. Developers/testers use exception handling framework to handle exception in selenium scripts.
Example: When selenium script fails due to wrong locator, then developer should be able to understand the reason for failure and this can be achieved easily if the exception is handled properly in the program.
Below we have described the types of exceptions and the different ways how we can use exception handling framework in selenium scripts.
Exceptions are events due to which java program ends abruptly without giving expected output. Java provides a framework where user can handle exceptions.
There are three kinds of exceptions:
- Checked Exception
- Unchecked Exception
- Error
Class hierarchy of exception and error:
#1) Checked Exception: Checked exception is handled during compile time and it gives compilation error if it is not caught and handled during compile time.
Example: FileNotFoundException, IOException etc.
#2) Unchecked Exception: In case of unchecked exception, compiler does not mandate to handle. Compiler ignores during compile time.
Example: ArrayIndexoutOfBoundException
#3) Error: When a scenario is fatal and program cannot recover then JVM throws an error. Errors cannot be handled by try catch block. Even if user tries to handle error by using Try catch block, it cannot recover from error.
Example: Assertion error, OutOfMemoryError etc.
Exception handling:
Try and Catch block:
try-catch blocks are generally used to handle exceptions. Type of exceptions is declared in catch block which is expected to come. When an exception comes in try block, immediately control moves to catch block.
Example:
1 |
try {
|
2 |
br = new BufferedReader( new FileReader( "Data" ));
|
3 |
} catch (IOException ie)
|
4 |
{
|
5 |
ie.printStackTrace();
|
6 |
}
|
There can be multiple catch blocks for one try block depending upon type of exception.
Example:
1 |
try {
|
2 |
br = new BufferedReader( new FileReader( "Data" ));
|
3 |
} catch (IOException ie)
|
4 |
{
|
5 |
ie.printStackTrace();
|
6 |
} catch (FileNotFoundException file){
|
7 |
file.printStackTrace();
|
8 |
}
|
throws Exception:
throws keyword in java is used to throw an exception rather than handling it. All checked exceptions can be thrown by methods.
Example:
1 |
public static void main(String[] args) throws IOException
|
2 |
{ |
3 |
BufferedReader br= new BufferedReader( new FileReader( "Data" ));
|
4 |
while ((line = br.readLine()) != null )
|
5 |
{
|
6 |
System.out.println(line);
|
7 |
}
|
8 |
} |
finally block:
finally block executes irrespective of execution of try catch block and it executes immediately after try/catch block completes.
Basically file close, database connection etc. can be closed in finally block.
Example:
1 |
try {
|
2 |
br = new BufferedReader( new FileReader( "Data" ));
|
3 |
} catch (IOException ie)
|
4 |
{
|
5 |
ie.printStackTrace();
|
6 |
}
|
7 |
Finally { |
8 |
br.close();
|
9 |
}
|
In the above example, BufferReader stream is closed in finally block.br.close() will always execute irrespective of execution of try and catch block.
------------
Note: finally block can exist without any catch block. It is not necessary to have a catch block always.
There can be many catch blocks but only one finally block can be used.
Throwable: Throwable is parent class for error and exception. Generally it is difficult to handle errors in java. If programmer is not sure about the type of error and exception, then it is advised to use Throwable class which can catch both error and exception.
Example:
1 |
try {
|
2 |
br = new BufferedReader( new FileReader( "Data" ));
|
3 |
} catch (Throwable t)
|
4 |
{
|
5 |
t.printStackTrace();
|
6 |
}
|
Exceptions in Selenium WebDriver:
Selenium has its own set of exceptions. While developing selenium scripts, programmer has to handle or throw those exceptions. Below are few examples of exceptions in selenium.
Examples:
ElementNotVisibleException: If selenium tries to find an element but element is not visible within page
NoAlertPresentException: If user tries to handle an alert box but alert is not present.
NoSuchAttributeException: While trying to get attribute value but attribute is not available in DOM.
NoSuchElementException: This exception is due to accessing an element which is not available in the page.
WebDriverException: Exception comes when code is unable to initialize WebDriver.
Conclusion:
Exception handling is the essential part of every java program as well as selenium script. We can build robust and optimal code by handling exception in smart ways. And it is also a best practice to handle exceptions in script which will give you a better report when program fails due to any reason.
Here we have tried to cover the process and framework of exception handling which is required to be implemented in selenium scripts.
Remember it is not mandatory to always handle exception in try-catchblock. You can also throw an exception depending upon the requirement in script.
Next Tutorial #20: In the upcoming tutorial, we would discuss about the various types of testing frameworks available. We would also study about the pros and cons of using a fledged framework approach in automation testing. We would discuss in detail about Test data driven framework.
相关推荐
博客附带源码以及selenium在Python和PyCharm中的安装方法链接,欢迎大家一起交流学习,共同进步,此方式是自己通过实际操作,而总结出来的,欢迎大家批评指正!
selenium开发:谷歌浏览器和驱动配套版本,很哪找。试了很多版本,这个是版本的浏览器和驱动是配套的,谷歌驱动只有32位,但是32和64都是通用的
Selenium是ThroughtWorks公司一个强大的开源Web功能测试工具系列,本系列现在主要包括以下4款: 1.Selenium Core:支持DHTML的测试案例(效果类似数据驱动测试),它是Selenium IDE和Selenium RC的引擎。 2....
This guide will provide a step-by-step approach to designing and building a data-driven test framework using Selenium WebDriver, Java, and TestNG. The book starts off by introducing users to the ...
用于firefox录制的插件,是php语言的文件导出,使用firefox打开的话下载完成后直接安装。
自动化的异常,selenium.common.exceptions.StaleElementReferenceException
Selenium服务器:Selenium服务器
Selenium4:Selenium 4.0相对定位器
selenium-java-3.141.59.jar selenium java自动化
selenium+firefox在定位时遇到selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: 由于是js加载页面,想确认是否是js的原因,随后进行多次调试时发现“//div”竟然也出现了...
SeleniumJava:SeleniumJava实用
Selenium-Framework:Selenium框架
SeleniumJava 基本的SeleniumJava Framework可以用于任何项目。 关键字驱动的Selenium自动化框架要求Java:1.7 + Selenium 3.x
Selenium Framework Design in Data-Driven Testing(官方源码)Selenium Framework Design in Data-Driven Testing(官方源
2018/8/19约会docker,使用Selenium docker 2017/6/21添加python版selenium演示说明:最近在搞爬虫,采用python scrappy框架,因此顺便搞一下python版本的selenium可以替代方法重新使用python封装一遍。喜欢使用...
Selenium基础Selenium WebDriver自动化和测试实践的仓库。
Selenium模拟 该存储库包含一个模拟真实Selenium Hub响应的小二进制文件。 Selenium代理(例如的主要应用程序负载测试。跑步运行以下命令以获取端口4444上正在运行的selenium-mock: # docker run --rm -it --name ...
SeleniumIDE来编写测试场景,然后利用Java语音来做各种条件、循环等工作。其实IDE配合插件用好了也很经典,尤其在IDE中使用Java_script能编写更丰富的测试用例。摆脱庞大的SeleniumServer端就从这里开始吧! 先给...
Selenium2TestingTools:Beginner’sGuide PDF
自动Selenium化:Selenium自动化百科全书