"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

April 22, 2011

.NET 2.0 Web Services Functional Test Automation

I'm reading book .Net Test automation recipies. If I had got this book 3 years back this could have helped me alot in automation. Never too late. I love this book. Thanks to the author for his great work. I used to use tool WebServiceStudio for Web Services Testing since 2006 (Long time back :))
  • I have attempted creating a simple asp.net webservice
  • Testing the same using Httpwebrequest POST
This is a skeleton only, The next post I'm planning to cover
  • Trying same example for WCF Service
  • Implement Data Driven Testing for this example using Nunit
  • Logging and Reporting
 Step 1 - Create a simple web service

Step 2 - Below sample project would be created, Modify the code for addition of two numbers

public int AddNumbers(int a, int b)
    return a+b;

Step 3 - View in Browser, You will see the SOAP Request

Step 4 - Created a Console Application

Copy - Paste below code in the Console App. Provide the correct modified URL for the webapp.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Data;
using System.IO;
namespace WebServiceAutomation
    class Program
        static void Main(string[] args)
            Console.WriteLine("Calling AddSum webmethod");
            int input1 = 10;
            int input2 = 20;
            int expectedresult = 30;
            int actualresult;
            string postData = @"<?xml version=""1.0"" encoding=""utf-8""?>
                <soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
                <AddNumbers xmlns=""http://tempuri.org/"">
            postData = postData.Replace("input1",input1.ToString());
            postData = postData.Replace("input2",input2.ToString());
            byte[] buffer = Encoding.ASCII.GetBytes(postData);
            string url = "http://localhost:1033/Service1.asmx?op=AddNumbers";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType =  "text/xml";
            req.ContentLength = buffer.Length;
            req.Timeout = 5000;
            Stream request = req.GetRequestStream();
            request.Write(buffer, 0, buffer.Length);
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            Stream resst = res.GetResponseStream();
            StreamReader sr = new StreamReader(resst);
            string response = sr.ReadToEnd();
            Console.WriteLine("HTTP Response is " + response);
            int start = response.IndexOf("<AddNumbersResult>");
            int end = response.IndexOf("</AddNumbersResult>");
            string Actualresult = response.Substring(start+18, end - start-18);
            Console.WriteLine("Expectd Result is " + expectedresult +"\n");
            Console.WriteLine("Actual Result is " + Actualresult + "\n");

Step 5 - Output Window After executing the Console App

Explanation for the code
  • If you want to interact more directly with a Web server, the HttpWebRequest and HttpWebResponse add additional methods that make this easier
  • HttpWebRequest and HttpWebResponse class is inside the System.NET namespace, and this two classes is designed to communicate by using the Http Protocol
  • HttpWebRequest can be used to send HTTP request to the server
  • Stream Class – msdn link  
  • The Stream class is abstract
    Means that same code can be used to read from or write to any kind of secondary storage (aka the backing store)
  • The StreamWriter class is derived from an abstract class called TextWriter, which writes characters to a stream or file
    The StreamReader class is derived from an abstract class called TextReader, which reads characters from a stream or file
  • StreamWriter is quite a bit different than StreamReader
    • StreamReader converts text to string or char[ ]
    • It does not do "text to numeric/object" conversions
    • You have to manually parse and convert the input
  • StreamWriter automatically converts all types of binary data to human-readable text for display
  • Stream Reader
    • Read a line from the file using the ReadLine() method
    • Returns a string if OK, null on end-of-file
    • Stops reading when newline or "\r\n" encountered
    • End-of-line is discarded
    • Read entire file into a single string using ReadToEnd()
More Reads

SOAP Vs REST (Representative State Transfer (REST))
  1. REST stands for Representational State Transfer, this basically means that each unique URL is a representation of some object
  2. It embraces a stateless client-server architecture in which the web services are viewed as resources and can be identified by their URLs. Source - Link
  3. REST has no WSDL interface definition
  4. REST is over HTTP, but SOAP can be over any transport protocols such HTTP, FTP, STMP, JMS etc.
  5. SOAP is using soap envelope, but REST is just XML
  6. SOAP RPC: Verb based. GetUser(string userId), AddUser(User u)
  7. REST: Noun based. Ex- http://mysite/users/dsevenhttp://mysite/users/newUserForm
  8. REST does NOT require a separate page for each data item, just a separate address.

Good Read - REST vs. SOAP – The Right WebService
Soap Headers Authentication in Web Services
CodeSnip: Handling SOAP Exceptions in Web Services
Interview Question: Compare two web services type SOAP and RESTful (SOAP Vs RESTful)
XML comparison tutorial using XMLUnit
Consuming Twitter APIs

Hope it Helps.
Happy Reading!!

No comments: