zoukankan      html  css  js  c++  java
  • Post test data to ASP.NET website

    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);            
            }
        }
    }
  • 相关阅读:
    Spark RDD概念学习系列之不同角度看RDD
    Spark RDD概念学习系列之典型RDD的特征
    Spark RDD概念学习系列之RDD的本质特征
    Spark RDD概念学习系列之RDD接口
    kettle的下载、安装和初步使用(Ubuntu 16.04平台下)(图文详解)
    Neo4j下执行cypher-shell时,Connection refused问题解决?
    Ubuntu14.04下沙盒数据导入到 Neo4j 数据库(图文详解)
    Ubuntu16.04下沙盒数据导入到 Neo4j 数据库(图文详解)
    Neo4j沙盒实验申请过程步骤(图文详解)
    Ubuntu14.04下Mongodb数据库可视化工具安装部署步骤(图文详解)(博主推荐)
  • 原文地址:https://www.cnblogs.com/qixue/p/2338250.html
Copyright © 2011-2022 走看看