Tuesday 30 December 2014

Logging Exception In Selenium

 

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

Exception in Python

Example 1--ValueError
For the code below if the user enter string or alpha-numeric an exception is raised.






Example 2--DivideByZero




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


Output



Solution for Example 2--Handling DivideByZeror


Output

Exception in Selenium


NoSuchElementFound--- exception is raised when element to be located by the following methods
fails
  1. find_element_by_id
  2. find_element_by_name
  3. find_element_by_tag_name
  4. find_element_by_css_selector
  5. find_element_by_xpath
  6. find_element_by_link_text
  7. find_element_by_partial_link_text
  8. 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
       
Example 1--Using the above steps we can write our first Logger, warning logs are found in 'warning_log.txt' and INFO logs in 'info_log.txt'



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
  1. Navigate to www.youtube.com and Assert for the Title
  2. locate the searchBox
  3. Search for Python Tutorials
An exception can be raised if Invalid Locator to the searchBox is given(NoSuchElementException) or if the title for the Youtube Page in Test Script is Wrong(AssertionError)
 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
NOTE:Logging configuration can be placed in seperate logging.conf file
Output

No comments:

Post a Comment