James McCaffrey wrote an article in MSDN Mag  
https://msdn.microsoft.com/en-us/magazine/ff955768.aspx
It demonstrates how to use automate the testing of an ASP.NET WebForms 
MiniCalculate page. 
The specific issue is parsing out the hidden viewstate field. The article is 
from 2010 and I believe the viewstate field size has changed, causing later 
webforms pages with the greater viewstatew field size to be incompatible with 
the original McCaffrey code. fd
Please, can someone help me get the code to work? 
Here is the IRONPYTHON harness.py code...
# harness.py
import sysimport Systemimport clr
from System import *from System.IO import *from System.Text import *from 
System.Net import *from HTMLParser import HTMLParser
clr.AddReference('System.Web')from System.Web import *
class MyParser(HTMLParser):   def __init__(self):     #super().__init__(self)   
  HTMLParser.__init__(self)     #self.seen = {}
#d = dict([('two', 2), ('one', 1), ('three', 3)])           def 
handle_starttag(self, tag, attributes):     if tag != 'input': return     a = 
dict(attributes)          if a['type'] == 'hidden':        print a['value']     
   return
def getVS(url):  wc = WebClient()  bytes = wc.DownloadData(url)  html = 
Encoding.ASCII.GetString(bytes)    print html  print 'starttag begin'  parser = 
MyParser()  return parser.feed(html)
  #start = html.IndexOf('id="__VIEWSTATE"', 0) + 24  #end = html.IndexOf('"', 
start)  #vs = html.Substring(start, end-start)  #return vs
def getEV(url):  wc = WebClient()  bytes = wc.DownloadData(url)  html = 
Encoding.ASCII.GetString(bytes)       start = 
html.IndexOf('id="__EVENTVALIDATION"', 0) + 30  end = html.IndexOf('"', start)  
ev = html.Substring(start, end-start)  return ev
try:    print '\nBegin IronPython Request-Response testing'  print sys.argv[0]  
url = 'http://localhost:64725/Default.aspx'  print '\nURL under test = ' + url 
+ '\n'  testCases = 'testCases.txt'
  numPass = numFail = 0    filePath = 
'C:\\Users\\daniel\\Downloads\\McCaffrey\\TestRun\\testCases.txt'   fin = 
open(filePath, 'r')    for line in fin:    print 
'======================================================================'    
(caseid,value1,value2,operation,decimals,action,expected) = line.split('|')     
   expected = 'value="' + expected.Trim() + '" id="TextBox3"'    data = 
'TextBox1=' + value1 + '&TextBox2=' + value2 + '&Operation=' + operation + 
'&DropDownList1=' + decimals + '&Button1=' + action      print 'Test case: ' + 
caseid    print 'Input    : ' + data    print 'Expected : ' + expected
    vs = getVS(url)    ev = getEV(url)    vs = HttpUtility.UrlEncode(vs)    ev 
= HttpUtility.UrlEncode(ev)    data = data + "&__VIEWSTATE=" + vs + 
"&__EVENTVALIDATION=" + ev     buffer = Encoding.ASCII.GetBytes(data)    req = 
HttpWebRequest.Create(url)        #print '^^^^^^^^^^^^^^^^^^^^^^^^'    #print 
req.GetResponse().Headers           req.Method = 'POST'    req.ContentType = 
'application/x-www-form-urlencoded'            print 
"**********************************"    print "buffer.Length = ",  
buffer.Length          req.ContentLength = buffer.Length
    reqst = req.GetRequestStream()    reqst.Write(buffer, 0, buffer.Length)    
#reqst.Flush()    reqst.Close()        res = req.GetResponse()    resst = 
res.GetResponseStream()    sr = StreamReader(resst)
    html = sr.ReadToEnd()    sr.Close()    resst.Close()      if 
html.IndexOf(expected) >= 0:      print 'Pass'      numPass = numPass + 1    
else:      print '**FAIL**'      numFail = numFail + 1
    print 
'======================================================================\n'
    # end main processing loop
  fin.close()
  print '\nNumber pass = ' + str(numPass)  print 'Number fail = ' + 
str(numFail)    print '\nEnd test run\n'
except System.Exception, e:  #type, value, traceback = sys.exc_info()  print 
e.Message
Thanks,
Danny Rosales
                                          
_______________________________________________
Ironpython-users mailing list
Ironpython-users@python.org
https://mail.python.org/mailman/listinfo/ironpython-users

Reply via email to