"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


[WebMethod]
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/"">
                <soap:Body>
                <AddNumbers xmlns=""http://tempuri.org/"">
                <a>input1</a>
                <b>input2</b>
                </AddNumbers>
            </soap:Body>
            </soap:Envelope>";
            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);
            request.Flush();
            request.Close();
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            Stream resst = res.GetResponseStream();
            StreamReader sr = new StreamReader(resst);
            string response = sr.ReadToEnd();
            Console.WriteLine("HTTP Response is " + response);
            Console.ReadKey();
            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");
            Console.ReadKey();
        }
    }
}

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!!

April 02, 2011

Selenium Web Test - Automation Framework - Best Practices

[Next Post for Selenium Based Test Automation Framework]

This post is based my question in stackoverflow site. These questions help us learn best practices while developing an automation framework.

Summarizing Discussion Points
  • Page Object test design pattern is a good approach in building automation framework. Based on it only I have written example posts
  • Don't use the Selenium IDE generated code as Automation Framework base
  • Build a very good underlying framework architecture and refactor it as you keep updating the suite
  • Learn Design patterns and Apply it wherever applicable
  • 3 layer test automation framework is very good. I feel convinced with this approach.
    • GUI Level Testing
    • Methods Level Testing
    • Database Checks
Posting below list of good reads on Test Automation

Integration Test Principles - Notes from the post
  • Configuration should be minimal (Pointing to a different Db to run a test)
  • Test cases should be independent (Select and run few cases)
  • Tests should be efficient
  • The test suite should clean up after itself (Good to clear logs/data)
Please feel free to check my learnings for Developing Test Automation Framework using Selenium.

Any comments / suggestions please comment on this blog post / alternatively you can connect on my email provided in homepage.



Happy Reading!!

Guidelines in Providing Test estimates

Being Agile means managing changing requirements, accommodating frequent releases, having very good QA processes to support the same. Below blog post is on communicating test Estimates. It's all hypothetical scenarios :). I'm sharing my personal opinion. Before you actually test a project you would be asked to provide test estimates for upcoming projects.

Let’s take below reply
Explanation One
  • For testing the changes in this feature, It would require 2 days
Let's see how this explanation can be probed - Peer QA might say, in my opinion, these changes would take only one day to test

Explanation two
  • This changes would require 2 days for testing
  • I would be covering below test scenarios
Let's see how this explanation can be probed - Few scenarios are missed, below scenarios can be additionally covered

Explanation three
  • This changes would require 2 days for testing
  • I would be covering below test scenarios
  • In scope - Below Cases
  • Out of Scope - A and B Features
Let's see how this explanation can be probed - Good to do regression / Sanity testing of B as they also seem to be affected

Explanation Four
  • Assumption - Change would be affecting A, B and C Features
  • In scope - Below Cases
  • Out of Scope - A and B Features
  • This changes would require 2 days for testing
  • I would be covering below test scenarios
Let's see how this explanation can be probed - This provides the context of the QA. Level of understanding, In-scope, out of scope items. I would rate it a good estimate providing the reviewer good amount of information on estimates and coverage.

Moving a step forward in this discussion would be adding dependencies/risks

Happy Reading!!