Skip to content

Utilities

Rohan Dudam edited this page Jul 26, 2024 · 11 revisions

Table of contents

Wrapit

Wrappers to hold decorator functions like exception handler, screenshot etc.,

Screenshot

Our framework uses a decorator to take screenshots. These decorators are called before every method in Page Objects. A screenshot folder is created for every test script inside the parent screenshot directory. We use a wrapper @Wrapit._screenshot before a test method to take screenshots. The below code sets the name in the text field predefined with the decorator.

@Wrapit._screenshot
def set_name(self,name):
   "Set the name on the form"
    result_flag = self.set_text(self.name_field,name)

Exeception Handler

This is a decorator to capture exceptions. We use a wrapper @Wrapit._exceptionHandler before a test method to handle the exception. The use of this decorator is it throws an exception when an error occurs and prevents tests from failing.

 @Wrapit._exceptionHandler
 def goto_menu_link(self,my_link,expected_url_string=None):
     "Navigate to a link: Hover + Click or just Click"
      split_link = my_link.split('>')

Secure Shell(SSH)

As part of automation, you may have to sometimes log in to a remote server and perform certain operations. We have written a utility script which will SSH into a remote server and executes SSH commands. We implemented this using Python Paramiko module. All the parameters are read from the .env file. We are reading the parameters like login credentials, key, commands & file paths etc., To use this script, simply fill these parameter details in the env_conf and rename to .env file and make a call to this script from your tests as shown below. The output variables stdout and stderr can be used for further processing if needed.

#---USAGE EXAMPLES
if __name__=='__main__':
    print("Start of %s"%__file__)
     
    #Initialize the ssh object
    ssh_obj = Ssh_Util()

    #Sample code to execute commands
    if ssh_obj.execute_command(ssh_obj.commands) is True:
        print("Commands executed successfully\n")
    else:
        print ("Unable to execute the commands")

For more details, you can refer to our detail guide "SSH using python Paramiko" for more information.

Compare CSV files

A util to compare two CSV files. This script contains methods to compare the actual and expected CSV files. It compares the row count and gives the mismatched data details.

#---USAGE EXAMPLES
if __name__=='__main__':
    print ("Start of %s"%__file__)

    #Fill in the file1 and file2 paths
    file1 = 'Add path for the first file here'
    file2 = 'Add path for the second file here'
    
    #Initialize the csv object
    csv_obj = Csv_Compare()

    #Sample code to compare csv files
    if csv_obj.is_equal(file1,file2) is True:
        print ("Data matched in both the csv files\n")
    else:
        print ("Data mismatch between the actual and expected csv files")  

Compare Excel files

A util to compare two excel files. This script uses openxl module and contains methods to compare the actual and expected excel files. It gives information about the row and column mismatch is any.

#---USAGE EXAMPLES
if __name__=='__main__':
    print ("Start of %s"%__file__)
    # Enter the path details of the xl files here
    file1 = 'Add path to the first xl file'
    file2 = 'Add path to the second xl file'
  
    #Initialize the excel object
    xl_obj = Excel_Compare()

    #Sample code to compare excel files
    if xl_obj.is_equal(file1,file2) is True:
        print ("Data matched in both the excel files\n")
    else:
        print ("Data mismatch between the actual and expected excel files")