welcome to Qijie's Blog 薛其杰

You may have to test a simple web page to  make sure the web page is working fine. the page may only have few controls, like two text boxes and one button. If one textbox is set to a specific value, once you click on the button, the other text box may get a defined value, then you could check the response string for the defined value to check wether if works as expected.

 

1. Give test data and expected tag, check wether the tag is in the response string.

2. Encode specific characters by HttpUtility.UrlEncode, this class defined in System.Web.dll.

3. Get the VIEWSTATE and EVENTVALIDATION using code.

 

Code as below:

 

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using System.Net;  //webClient
//ausing System.Web.Util; //HttpUtility.UrlEncode


namespace WebNetTestFramework
{
    public class WebTestClass
    {
        public static bool ResponseContainsTag(string uri, string postdata, string expectedtag)
        {
            //Handle Post Data
            byte[] data = Encoding.ASCII.GetBytes(postdata);

            //Create instance of request
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = data.Length;
            Stream st_wr = req.GetRequestStream();
            st_wr.Write(data, 0, data.Length);
            st_wr.Flush();
            st_wr.Close();

            //Create instance of response
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            Stream st_rd = res.GetResponseStream();
            StreamReader sr = new StreamReader(st_rd);
            string responseString = sr.ReadToEnd();
            sr.Close();
            st_rd.Close();

            //Test wether contains the expected tag
            if (responseString.IndexOf(expectedtag) >= 0)
                return true;
            else
                return false;
        }

        public static string GetViewState(string uri)
        {
            try
            {
                WebClient webClt = new WebClient();
                Stream st = webClt.OpenRead(uri);
                StreamReader sr = new StreamReader(st);
                string responseString = sr.ReadToEnd();
                int startVS = responseString.IndexOf("__VIEWSTATE"0);

                string vs = string.Empty;
                if (startVS > 19)
                {
                    int startVSValue = responseString.IndexOf("value=", startVS) + 7;
                    int endVSValue = responseString.IndexOf("\"", startVSValue);
                    vs = responseString.Substring(startVSValue, (endVSValue - startVSValue));
                }
                else
                    vs = "Viewstate Not found";
                return "__VIEWSTATE="+vs;
            }
            catch
            {
                throw new Exception("Fatal error finding viewstate");
            }
        }

        public static string GetViewStateAndEventValidation(string uri)
        {
            try
            {
                WebClient webClt = new WebClient();
                Stream st = webClt.OpenRead(uri);
                StreamReader sr = new StreamReader(st);
                string responseString = sr.ReadToEnd();
                int startVS = responseString.IndexOf("__VIEWSTATE"0);

                string vs = string.Empty;
                if (startVS > 19)
                {
                    int startVSValue = responseString.IndexOf("value=", startVS)+7;
                    int endVSValue = responseString.IndexOf("\"", startVSValue);
                    vs = responseString.Substring(startVSValue, (endVSValue - startVSValue));
                }
                else
                    vs = "Viewstate Not found";

                string ev = string.Empty;
                int startEV = responseString.LastIndexOf("__EVENTVALIDATION"0) + 49;
                if (startEV > 48)
                {
                    int startEVValue = responseString.IndexOf("value=", startEV) + 7;
                    int endEVValue = responseString.IndexOf("\"", startEV);
                    ev = responseString.Substring(startEVValue, (endEVValue - startEVValue));
                }
                else
                    ev = "Eventvalidation Not found";
                return "__VIEWSTATE=" + vs + "__EVENTVALIDATOIN=" + ev;
            }
            catch
            {
                throw new Exception
                ("Fatal error finding viewstate or eventvalidation");
            }
        }

        //Transform special characters like "&"->"%26"
        public static string EncodeValue(string value)
        {
            return HttpUtility.UrlEncode(value);            
        }
    }
}

 

 

posted on 2012-02-04 19:35  零点零一  阅读(256)  评论(0编辑  收藏  举报