`

Selenium 19:Handling Exceptions Using Exception Handling Framework in Selenium S

 
阅读更多

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.

Exception handling in Selenium 2

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:

  1. Checked Exception
  2. Unchecked Exception
  3. Error

Class hierarchy of exception and error:

Exception handling in Selenium

#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.

ExampleFileNotFoundExceptionIOException etc.

#2) Unchecked Exception: In case of unchecked exception, compiler does not mandate to handle. Compiler ignores during compile time.

ExampleArrayIndexoutOfBoundException

#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.

ExampleAssertion errorOutOfMemoryError 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(newFileReader("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.

分享到:
评论

相关推荐

    基于Python的selenium操作:判断元素是否存在+判断元素是否可以点击.zip

    【Python的selenium操作:判断元素是否存在】 在Python的自动化测试中,Selenium是一个非常强大的工具,用于模拟用户与网页的交互。Selenium库提供了一系列API,使得我们可以控制浏览器进行各种操作,例如点击按钮...

    selenium私房菜系列

    Selenium是ThroughtWorks公司一个强大的开源Web功能测试工具系列,本系列现在主要包括以下4款: 1.Selenium Core:支持DHTML的测试案例(效果类似数据驱动测试),它是Selenium IDE和Selenium RC的引擎。 2....

    selenium开发:谷歌浏览器和驱动配套版本

    在进行自动化测试时,Selenium 是一款非常强大的工具,它允许开发者编写脚本来模拟用户对网页的操作。在本文中,我们将深入探讨Selenium与谷歌浏览器(Chrome)以及对应的Chrome驱动(ChromeDriver)的配合使用,...

    selenium framework design data driven testing

    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 ...

    Selenium IDE: PHP Formatters

    用于firefox录制的插件,是php语言的文件导出,使用firefox打开的话下载完成后直接安装。

    Selenium IDE是firefox的一个插件,可以帮助刚入门的自动化测试供测试,可实现脚本的录制、开发、回放

    Selenium IDE是firefox的一个插件,可以帮助刚入门的自动化测试供测试,在脚本语言不太熟练的情况下,可以通过Selenium IDE实现脚本的录制、开发、回放。 众所周知,「Selenium IDE」是一种记录和回放工具。现在它将...

    RFS——RobotFramework+Selenium2library安装包齐全

    在“RFS——RobotFramework+Selenium2library安装包齐全”中,我们有完整的环境来搭建和运行基于Robot Framework和Selenium2Library的自动化测试项目。 首先,让我们深入理解Robot Framework。Robot Framework是一...

    自动化测试框架RobotFramework+Selenium2

    自动化测试框架RobotFramework+Selenium2 自动化测试框架RobotFramework+Selenium2是结合RobotFramework框架和Selenium2Library实现自动化测试的解决方案。下面是该框架的详细介绍: 简介 Robot Framework是一个...

    自动化的异常,selenium.common.exceptions.StaleElementReferenceException

    然而,在实际的自动化测试过程中,我们经常会遇到一些异常情况,其中之一就是"Selenium.common.exceptions.StaleElementReferenceException"。这个异常通常在尝试访问或操作一个已经过期(stale)的元素时出现,这...

    可使用selenium3.11版本的 Firefox-v57 + GeckoDriver-v19

    在IT行业中,自动化测试是提升效率和质量的关键环节,而Selenium是一款广泛使用的网页自动化测试工具。本主题聚焦于使用Selenium 3.11版本与Firefox v57的组合,以及GeckoDriver v19的配置,尤其适用于Windows平台。...

    RobotFramework+selenium自动化测试-齐涛博客教程整理

    ### RobotFramework + Selenium 自动化测试 - 齐涛博客教程整理 #### 一、引言及基础知识 RobotFramework(简称RF)结合Selenium Library,为Web自动化测试提供了一个强大的框架。这种组合允许测试人员使用一种...

    SeleniumJava:基本的SeleniumJava Framework可以用于任何项目

    **SeleniumJava:基本的SeleniumJava Framework** Selenium Java框架是自动化测试领域的一个强大工具,它允许开发者使用Java语言来编写测试脚本,以实现对Web应用的自动化测试。这个框架基于Selenium WebDriver,一...

    selenium-java-3.141.59

    selenium-java-3.141.59.jar selenium java自动化

    《python3网络爬虫开发实战》学习笔记::selenium——xpath:Unable to locate element

    selenium+firefox在定位时遇到selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: 由于是js加载页面,想确认是否是js的原因,随后进行多次调试时发现“//div”竟然也出现了...

    Selenium-3.11.0-Java源码包

    Selenium 是一个强大的开源自动化测试框架,主要用于网页应用的测试。3.11.0 版本是 Selenium 的一个重要里程碑,提供了对多种浏览器和多种编程语言的支持,包括 Java。在这个版本中,Selenium 提供了丰富的 API 和...

    robotframework-selenium2library-1.5.0

    Robot Framework Selenium2Library 1.5.0 是一个专门针对Web自动化测试的强大的库,它构建于Robot Framework之上,提供了一套简洁而易用的接口,使得测试人员能够高效地编写自动化测试用例。Robot Framework 是一个...

    Selenium:Selenium Grid.zip

    史上最全软件测试技术全套教程,包括: Postman Selenium 单元测试 压力测试 回归测试 安全测试 性能测试 测试工具 集成测试 等流行技术的系列教程

    Selenium服务器:Selenium服务器

    Selenium服务器是一个强大的自动化测试工具,它允许开发者模拟真实用户在浏览器中的操作,进行Web应用程序的功能性和兼容性测试。在IT行业中,Selenium是广泛应用于Web应用自动化测试的框架,尤其在JavaScript、PHP...

    RobotFramework-Selenium2Library

    **RobotFramework-Selenium2Library详解** Robot Framework是一个通用的自动化框架,它支持各种测试和任务自动化,而Selenium2Library是Robot Framework中的一个关键库,专门用于Web应用程序的自动化测试。这个库是...

Global site tag (gtag.js) - Google Analytics