XPathExtractionRule.cs
4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//*************************************************************************************************
// XPathExtractionRule.cs
// Owner: Mahipal Kante
//
// This web test plugin extracts data from a Web Service response using XPath expression.
// The Web Service response can be plain XML or 'application/soap+msbin1'
// (binary - used by Silverlight application with WCF RIA) or JSON
//
// Copyright(c) Microsoft Corporation, 2010
//*************************************************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Microsoft.VisualStudio.TestTools.WebTesting;
using System.Xml.XPath;
using System.Xml;
using System.IO;
namespace WebTest.WebService.Plugin.Runtime
{
    [DisplayName("XPathExtractionRule")]
    [Description("Extracts a value using XPath expression")]
    public class XPathExtractionRule : ExtractionRule
    {
        // The name of the desired input field
        private string xPathValue;
        [DisplayName("XPathToSearch")]
        [Description("Extracts a value using XPath expression")]
        public string XPathToSearch
        {
            get { return xPathValue; }
            set { xPathValue = value; }
        }
        // To set a default value use:[DefaultValue(<value>)]
        private int indexValue;
        [Description("Indicates which occurrence of the string to extract. This is zero-based index.")]
        public int Index
        {
            get { return indexValue; }
            set { indexValue = value; }
        }
        private bool randomMatch;
        [DisplayName("ExtractRandomMatch")]
        [Description("If true, all matches are found and one of the matches is randomly returned.")]
        public bool ExtractRandomMatch
        {
            get { return randomMatch; }
            set { randomMatch = value; }
        }
        /// <summary>
        /// The Extract method.  The parameter e contains the Web test context.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void Extract(object sender, ExtractionEventArgs e)
        {
            try
            {
                ContentHandler contentHandler = ContentHandlerFactory.GetHandler(e.Response.ContentType);
                if (contentHandler == null)
                {
                    return;
                }
                if (contentHandler.IsBinary)
                {
                    contentHandler.MessageBytes = e.Response.BodyBytes;
                }
                else
                {
                    contentHandler.MessageString = e.Response.BodyString;
                }
                XPathNodeIterator iterator = contentHandler.EvaluateXPath(XPathToSearch);
                List<string> values = new List<string>();
                int i = 0;
                while (iterator.MoveNext())
                {
                    XPathNavigator nav2 = iterator.Current.Clone();
                    string value;
                    if (nav2.NodeType.Equals(XPathNodeType.Element))
                    {
                        value = nav2.InnerXml;
                    }
                    else
                    {
                        value = nav2.Value;
                    }
                    values.Add(value);
                    if (!ExtractRandomMatch && (i == Index))
                    {
                        // add the extracted value to the Web test context
                        e.WebTest.Context.Add(this.ContextParameterName, value);
                        e.Success = true;
                        return;
                    }
                    i++;
                }
                if (ExtractRandomMatch && (values.Count > 0))
                {
                    Random random = new Random();
                    e.WebTest.Context.Add(this.ContextParameterName, values[random.Next(values.Count)]);
                    e.Success = true;
                    return;
                }
            }
            catch (Exception ex)
            {
                e.Message = ex.Message;
            }
            e.Success = false;
            return;
        }
    }
}