Python tips for Automation

Why python for Automation?
if we write shell scripts for automation, it wont work on windows. if we write batch scripts it wont work on Unix platforms. so lets choose language which is platform independent, fast and development time is less.
It is none other than Python :) . Python is used not only for Scripting. it is programming language can be used for GUI , sockets, scripting, web programming :) .

I have been automating many test cases for performance testing team . It saves 20 hours of time per week. Going in future , routine tasks will be automated using Python :) . no need of hiring testers to do routine tasks which keep repeating every week or every month .

common things we use while writing Python scripts for automation .

1) coping files from one location to other.
2) check whether file exists
3) check which OS it is
4) invoking shell script or other executable from Python script
5) reading the Configuration files which are of Windows Config files .INI format  like below
   [boot]
timeout=30
quick=yes


the Answers are below

1a) import shutil
      shutil.copy(src,dest)

2a) import os
       file="/tmp/file1.txt"
      os.access(file, os.F_OK)

3a)
   import sys
  print(sys.platform)

4a)
    import subprocess
   p=subprocess.Popen(command,shell=True)
        p.wait() '''waits until process is done '''

5a) 
here is the program called ConfigChanger.py which takes 2 files as command line args. 
invoke as  $python ./ConfigChanger  file1.INI file2.INI . This changes file1.INI according to file2.INI . the common attributes present in file2.INI will be moved to file1.INI and new attributes present in file1.INI will not be changed . its like set operations file1 n file2 in file1 and file1 - file2 in file1. 

import ConfigParser
import sys 

if(len(sys.argv) < 3):
    print "\n python ./NQSConfigParser.py "
config=ConfigParser.ConfigParser() # source file2.INI
config.read(sys.argv[2]);   

config1=ConfigParser.ConfigParser() # destination file1.INI
config1.read(sys.argv[1]); 


list_sections=[ ];
i=0
j=0
index=()
Source={}
Dest={}
#element=[ ];
key=" "
values=" "

sec=config.sections() # source
sec1=config1.sections() 
tmpfile="%s_tmp"%(sys.argv[1])
nqsconfig_tmp=open(tmpfile,"wb") #destination file
for section in sec1:
    
    Dest_items=config1.items(section)
    if section in sec:
        Source_items=config.items(section)
    print "\n[%s]" %section
    section_write="[%s]\n" %section
    nqsconfig_tmp.write(section_write)
    
    for f in Source_items:
        key=str(f[0]).upper()
        value=f[1::]
        values=" "
        for val in value:
            values = values + val
        
        if values.find(";")== -1:
            values= values+";"    
        Source[key]=values
        

    for f in Dest_items:
        key=str(f[0]).upper()
        value=f[1::]
        values=" "
        for val in value:
            values = values + val
            if values.find(";")== -1:
                values= values+";"
        Dest[key]=values
        if key in Source:
            attribute="%s = %s\n" %(key, Source[key])
            nqsconfig_tmp.write(attribute)
        else:
            attribute="%s = %s\n" %(key, Dest[key])
            nqsconfig_tmp.write(attribute)

Dest={}
Source={}
nqsconfig_tmp.close()

No comments: