Thursday, July 12, 2012

Open cmd.exe in specific directory

If you need to open cmd.exe in specific directory and run a command, here is what you need to do:

var processStartInfo = new ProcessStartInfo();
processStartInfo.WorkingDirectory = @"c:\";
processStartInfo.FileName = "cmd.exe";

// set additional properties     
Process proc = Process.Start(processStartInfo);

Wednesday, July 11, 2012

Representing quotes inside string in C#


For:
Ana said "apples"!

string myString = "Ana said \"apples\"!";
string myString = @"Ana said ""apples""!";

Rules:
 - add \ before quotes you want to keep;
 - double the quotes and use @ at the begining.

Working with XML

1. Format xml file so it looks nice:


 public static String FormatXML(String XML)
        {
            String Result = "";


            MemoryStream mStream = new MemoryStream();
            XmlTextWriter writer = new System.Xml.XmlTextWriter(mStream, Encoding.Unicode);
            XmlDocument document = new XmlDocument();


            try
            {
                // Load the XmlDocument with the XML.
                document.LoadXml(XML);


                writer.Formatting = Formatting.Indented;


                // Write the XML into a formatting XmlTextWriter
                document.WriteContentTo(writer);
                writer.Flush();
                mStream.Flush();


                // Have to rewind the MemoryStream in order to read
                // its contents.
                mStream.Position = 0;


                // Read MemoryStream contents into a StreamReader.
                StreamReader sReader = new StreamReader(mStream);


                // Extract the text from the StreamReader.
                String FormattedXML = sReader.ReadToEnd();


                Result = FormattedXML;
            }
            catch (XmlException)
            {
            }


            mStream.Close();
            writer.Close();


            return Result;
        }


2. To be continued...

Wednesday, June 27, 2012

Web test plugin to encode to base64


This is a Web Performance test plugin that get's the values from 2 context parameters and encrypts them in base64, before running the call:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.WebTesting;


namespace Base64
{
    public class Encrypt64 : WebTestPlugin
    {
        [System.ComponentModel.DisplayName("Context Parameter Name")]
        [System.ComponentModel.Description("Name of the webtest context parameter")]
        public string ContextParameterName
        {
            get;
            set;
        }


        // encode to Base64 function
        static public string EncodeTo64(string toEncode)
        {
            byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
            string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
            return returnValue;
        }


        // decode from Base64 function
        static public string DecodeFrom64(string encodedData)
        {
            byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);
            string returnValue = System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
            return returnValue;
        }


        // this will be executed before the call
        public override void PreWebTest(object sender, PreWebTestEventArgs e)
        {            
            string decode64;
            decode64 = EncodeTo64(e.WebTest.Context["email"] + ":" + e.WebTest.Context["pass"]);
            e.WebTest.Context["GetBase64"] = decode64;
        }


        // this will be executed after the call
        public override void PostWebTest(object sender, PostWebTestEventArgs e)
        {
        }
    }
}

Tuesday, June 26, 2012

Custom Web Performance Rules


Create a Custom Extraction Rule for a Web Performance Test
http://msdn.microsoft.com/en-us/library/ms243179.aspx

Create a Custom Validation Rule for a Web Performance Test
http://msdn.microsoft.com/en-us/library/ms182556

Create a Web Performance Test Plug-In
http://msdn.microsoft.com/en-us/library/ms243191.aspx

Custom Extraction Rule - Extract Cookie Value
http://social.msdn.microsoft.com/Forums/en-US/vstswebtest/thread/cf2bf144-4e87-414f-8aa3-7731f9bc7b73/

Encode/Decode to Base64


//Encode

static public string EncodeTo64(string toEncode)
    {
      byte[] toEncodeAsBytes
            = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
      string returnValue
            = System.Convert.ToBase64String(toEncodeAsBytes);
      return returnValue;
    }


//Decode

    static public string DecodeFrom64(string encodedData)
    {
      byte[] encodedDataAsBytes
          = System.Convert.FromBase64String(encodedData);
      string returnValue =
         System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
      return returnValue;
    }


//Usage

      string myData = “Here is a string to encode.”;

      string myDataEncoded = EncodeTo64(myData);
      Console.WriteLine(myDataEncoded);

      string myDataUnencoded = DecodeFrom64(myDataEncoded);
      Console.WriteLine(myDataUnencoded);

      Console.ReadLine();

Link where I got this from:
http://arcanecode.com/2007/03/21/encoding-strings-to-base64-in-c/

401 - Unauthorized error in Web Performance test

Scenario:
1. In a Visual Studio Web Performance test, make call to a web service that requires credentials.
=> you will get 401 - Unauthorized error (if you did not specify credentials)

Solution:
1. Add a Header (for Basic authentication) to the web test request: Right-click on the call -> Add Header.

2. Add the following info:
Name: "Authorization"
Value: “Basic ” + encodeBase64 (“username:password”)

Use http://www.base64encode.org/ to encode username and password in the following format: "username:password".

It should look like this: