Skip to content

Utilities

nilaya123 edited this page Nov 26, 2018 · 11 revisions

Table of contents

Slack Integration

We created a utility script(post_test_reports_to_slack.py) to post the test reports on Slack channel. You can generate Slack incoming webhook URL by referring our blog Post pytest test results on Slack and use the generated incoming webhook URL in the utility script.

To generate a test report in .txt or .log format, use '> test_report.log' argument in the py.test command:

For Example, To run the test use the command -> pytest -m -v -S Y > pytest_report.log

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 ssh_conf.py configuration 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 conf 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.

GMail

A simple IMAP util is used to interact with IMAP servers. You can connect to your IMAP host, login to the email, fetch latest messages in the inbox, search for a subject and return the latest unique ids of the emails, fetch the email body for a given uid etc., All the parameters needed for this script are maintained in gmail_conf.py file.

#---EXAMPLE USAGE---
if __name__=='__main__':
    #Fetching conf details from the conf file
    imap_host = conf_file.imaphost
    username = conf_file.gmail_user
    password = conf_file.gmail_app_password

    #Initialize the email object
    email_obj = Email_Util()

    #Connect to the IMAP host
    email_obj.connect(imap_host)
    
    #Login
    if email_obj.login(username,password):
        print ("PASS: Successfully logged in.")
    else:
        print ("FAIL: Failed to login")

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")