What is an Exception ?
When inputs to function are invalid or pre-condition are not meet an error is raised know as Exception.
To know more about exception we will see ValueError & DivideByZero exception in python
and later on NoSuchElementFound in selenium
To know more about exception we will see ValueError & DivideByZero exception in python
and later on NoSuchElementFound in selenium
Exception in Python
Example 1--ValueError
For the code below if the user enter string or alpha-numeric an exception is raised.
How to handle Exceptions ?
Exception can be caught using TRY & EXCEPT block statements.
Note: Exception should not be used as part of the normal code,but only for exceptional situations.
Only catch exception that you know to handle.
Solution for Example 1--Handling ValueError
Solution for Example 2--Handling DivideByZeror
Exception in Selenium
NoSuchElementFound--- exception is raised when element to be located by the following methods
fails
- find_element_by_id
- find_element_by_name
- find_element_by_tag_name
- find_element_by_css_selector
- find_element_by_xpath
- find_element_by_link_text
- find_element_by_partial_link_text
- find_element_by_class_name
Example 1--NoSuchElementFound
Handling "NoSuchElementFound" exception
Output in IDLE
Output in Window Command-line
What is Logging ?
Logging is a standard python Module.It record or track the events during the execution of a Application.Logging message are displayed or stored using Logging function and logging Levels.
- Developers use it for Debugging ie....fault investigation.
- System Admin use it for Monitoring ie... resources are used effectively.
- Help Desk use it for answering client about the problem and about the Patch or workaround to be applied.
Logging Functions
- logging.debug('This function provides detailed information ')
- logging.warning('Something unexpected happened')
- logging.error('For storing exception traces')
- logging.info('It is a confirmation that things are going according to plan')
- logging.critical('The main action to be performed fails')
Logging Levels
Given below are logging levels in the increasing order of severity.Critical is the highest level and Info is the lowest level...ie if we configure the logs for WARNING our log file will contain logs of WARNING,ERROR & CRITICAL.The default log message are WARNING
- DEBUG
- INFO
- WARNING
- ERROR
- CRITICAL
Creating a simple logger
Step 1: Import the logging Module
Step 2: Create a logger with a level of logs to be recorded
Step 3: Create a Log-Handler
Step 4: Create For-matter for Log-Handlers
Step 5: Add the log-handler to the logger
Output
warning_log.txt
info_log.txt
Example 2--Handling Exception
Contents of ex1_critical.txt
Example 3--We will consider an Example where we have to
- Navigate to www.youtube.com and Assert for the Title
- locate the searchBox
- Search for Python Tutorials
Here were store the CRITICAL logs in a file called Youtube.txt and INFO logs are seen at the console.
Example 3.1 Handling--AssertionError CRITICAL logs ---found in-'Youtube.txt'
Contents of Youtube.txt file
Example 3.2 Handling -- NoSuchElementException---CRITICAL logs ---found in-'Youtube.txt'
Contents of Youtube.txt file
Example 3.3 Finally the correct code
No comments:
Post a Comment