test_search.py

<---Back

Select Code Highlighting Style:

Bright | SEASHELL | Darkness

Select Font Size:

Small | Normal | Large
../www/get_script_html_files/strip/test_search.py
    1 #!/usr/bin/python2
    2 
    3 #######################################
    4 #
    5 # This script runs end-to-end testcase for search
    6 #
    7 # Notes to students:
    8 #
    9 # 1. Purpose of this script is to illustrate how fragile end-to-end automation is.
   10 # 2. This script will show you only GENERAL PRINCIPLE about how to automate regression testing
   11 # 3. If you ever decide to launch full scale automation project, put a lot of thinking if it's worth it.
   12 #
   13 # Version: 1.1
   14 #
   15 #######################################
   16 
   17 #import everything from lib.py
   18 
   19 from lib import *
   20 
   21 #import everything from qalib.py - this is our library for test functions
   22 
   23 from qalib import *
   24 
   25 #get dictionary of values from web form
   26 
   27 form = cgi.FieldStorage()
   28 
   29 #run_flag is a flag. If run_flag == 1, we run test
   30 
   31 try:
   32     run_flag = form['run_flag'].value
   33 except:
   34     run_flag = 0
   35 
   36 #function to run test
   37 
   38 def get_actual_result():
   39 
   40     #create temp filename
   41 
   42     tmp_filename = "/tmp/"+str(time.time())+".txt"
   43 
   44     #specify keyword to search
   45 
   46     keyword = 'moon'
   47 
   48     #create url string
   49 
   50     url = "http://www.sharelane.com/cgi-bin/search.py?keyword=%s" % keyword
   51 
   52     #create command line for execution
   53 
   54     exe_line = "wget -q -O %s '%s';cat %s;rm %s" % (tmp_filename,url,tmp_filename,tmp_filename)
   55 
   56     #execute command line
   57 
   58     rs = commands.getoutput(exe_line)
   59 
   60     return rs
   61 
   62 #function to display first page
   63 
   64 def page_one(message = ''):
   65 
   66     html = """
   67 
   68     <center>
   69 
   70     <h2>Test Search</h2>
   71 
   72     <p><font color='red'><b>%s</b></font>
   73 
   74     <hr>
   75 
   76     <p><b>EXPECTED RESULT:</b>
   77 
   78     <p><a href = '../test_search_ER.txt'>test_search_ER.txt</a>
   79 
   80     <hr>
   81 
   82     <p><b>ACTUAL RESULT:</b>
   83 
   84     <form action='./test_search.py'>
   85 
   86     <input type='hidden' name='run_flag' value='1'>
   87 
   88     <p><input type='submit' value='Run Test'>
   89 
   90     </form>
   91 
   92     </center>
   93 
   94 """ % message
   95 
   96     return html
   97 
   98 #function to display second page
   99 
  100 def page_two():
  101 
  102     #get string with actual results
  103     #we'll use 'a_' to start variable names of actual stuff
  104     #and 'e_' to start variable names of expected stuff
  105 
  106     a_string = get_actual_result()
  107 
  108     html = """
  109 
  110     <center>
  111 
  112     <h3>Test Results</h3>
  113 
  114     <table border=1 align=center cellpadding=5>
  115 
  116     <tr>
  117 
  118         <td align=center>EXPECTED</td>
  119         <td align=center>ACTUAL</td>
  120         <td>RESULT</td>
  121 
  122     </tr>
  123 
  124     <tr>
  125 
  126         <td align=center><b>Keyword</b></td>
  127         <td align=center><b>Found? (y/n)</b></td>
  128 
  129     </tr>"""
  130 
  131     #get expected results
  132 
  133     e_list = []
  134 
  135     file = open('../www/test_search_ER.txt','r')
  136 
  137     for i in file.readlines():
  138 
  139         #remove unprintable chars at the end of the line
  140         i = re.sub('\r','',i)
  141         i = re.sub('\n','',i)
  142 
  143         e_list.append(i)
  144 
  145     file.close()
  146 
  147     for i in e_list:
  148 
  149         is_found = 0
  150 
  151         #check if word from expected results is found on html page
  152 
  153         if re.findall(i,a_string):
  154 
  155             is_found = 1
  156 
  157             bool_letter = 'y'
  158             result      = 'PASS'
  159             bg_color    = 'greenyellow'
  160 
  161         if is_found == 0:
  162 
  163             bool_letter     = 'n'
  164             result      = 'FAIL'
  165             bg_color    = 'red'
  166 
  167 
  168         #create html
  169 
  170         html = html + """
  171 
  172                 <tr>
  173 
  174                     <td align=center>%s</td>
  175                     <td align=center>%s</td>
  176 
  177                     <td align=center bgcolor = '%s'>%s</td>
  178 
  179                 </tr>""" %  (i,bool_letter,bg_color,result)
  180 
  181     html = html + "</center></table><hr>"
  182 
  183     #show raw test data
  184 
  185     a_string = re.sub('<','&lt;',a_string)
  186     a_string = re.sub('>','&gt;',a_string)
  187 
  188     html = html + "<p><b>Test Data:</b><p>HTML source: %s" % (str(a_string))
  189 
  190 
  191     return html
  192 
  193 print 'Content-Type: text/html\n\n'
  194 
  195 
  196 if int(run_flag) == 0:
  197 
  198     html = page_one()
  199 
  200 else:
  201 
  202     html = page_two()
  203 
  204 html = get_qa_html(html)
  205 
  206 print html
  207