Skip to content

Instantly share code, notes, and snippets.

@tysonstewart
Created October 5, 2012 18:26
Show Gist options
  • Select an option

  • Save tysonstewart/3841524 to your computer and use it in GitHub Desktop.

Select an option

Save tysonstewart/3841524 to your computer and use it in GitHub Desktop.
Example Extraction Rule for Visual Studio Webtests
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Xml.Linq;
using Microsoft.VisualStudio.TestTools.WebTesting;
namespace Hudl.WebApp.Tests.TestsCore.ExtractionRules
{
[DisplayName("Extract Published Clip ID")]
[Description("For a CreateClip web service request, extracts the newly-created Clip ID value and inserts it into the ID collection specified by the Context parameter name.")]
public class ExtractPublishedClipId : ExtractionRule
{
private static readonly XNamespace DefaultNamespace;
static ExtractPublishedClipId()
{
DefaultNamespace = "http://tempuri.org/";
}
public override void Extract(object sender, ExtractionEventArgs e)
{
var bodyString = e.Response.BodyString;
var clipId = GetClipId(bodyString);
if (!clipId.HasValue)
{
e.Message = "Extraction of Published Clip ID failed. Result text: " + Environment.NewLine +
e.Response.BodyString;
e.Success = false;
return;
}
var clipIds = e.WebTest.Context[ContextParameterName] as ICollection<long>;
clipIds.Add(clipId.Value);
e.Success = true;
}
private static long? GetClipId(string bodyString)
{
var bodyDoc = XDocument.Parse(bodyString);
var resultElement = bodyDoc.Descendants(DefaultNamespace + "CreateClipResult").SingleOrDefault();
if (resultElement == null)
{
return null;
}
var createClipResultValue = resultElement.Value;
var clipId = Convert.ToInt64(createClipResultValue);
return clipId;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment