Added PassIfNotFound to XPathExtractionRule
Showing
1 changed file
with
43 additions
and
40 deletions
| ... | @@ -52,6 +52,11 @@ namespace WebTest.WebService.Plugin.Runtime | ... | @@ -52,6 +52,11 @@ namespace WebTest.WebService.Plugin.Runtime | 
| 52 | set { randomMatch = value; } | 52 | set { randomMatch = value; } | 
| 53 | } | 53 | } | 
| 54 | 54 | ||
| 55 | [DisplayName("Pass if not found")] | ||
| 56 | [Description("If set to true, this rule will pass the request even if no match is found.")] | ||
| 57 | [DefaultValue(false)] | ||
| 58 | public bool passIfNotFound { get; set; } | ||
| 59 | |||
| 55 | /// <summary> | 60 | /// <summary> | 
| 56 | /// The Extract method. The parameter e contains the Web test context. | 61 | /// The Extract method. The parameter e contains the Web test context. | 
| 57 | /// </summary> | 62 | /// </summary> | 
| ... | @@ -62,56 +67,53 @@ namespace WebTest.WebService.Plugin.Runtime | ... | @@ -62,56 +67,53 @@ namespace WebTest.WebService.Plugin.Runtime | 
| 62 | try | 67 | try | 
| 63 | { | 68 | { | 
| 64 | ContentHandler contentHandler = ContentHandlerFactory.GetHandler(e.Response.ContentType); | 69 | ContentHandler contentHandler = ContentHandlerFactory.GetHandler(e.Response.ContentType); | 
| 65 | if (contentHandler == null) | 70 | if (contentHandler != null) { | 
| 66 | { | 71 | if (contentHandler.IsBinary) | 
| 67 | return; | 72 | { | 
| 68 | } | 73 | contentHandler.MessageBytes = e.Response.BodyBytes; | 
| 74 | } | ||
| 75 | else | ||
| 76 | { | ||
| 77 | contentHandler.MessageString = e.Response.BodyString; | ||
| 78 | } | ||
| 69 | 79 | ||
| 70 | if (contentHandler.IsBinary) | 80 | XPathNodeIterator iterator = contentHandler.EvaluateXPath(XPathToSearch); | 
| 71 | { | ||
| 72 | contentHandler.MessageBytes = e.Response.BodyBytes; | ||
| 73 | } | ||
| 74 | else | ||
| 75 | { | ||
| 76 | contentHandler.MessageString = e.Response.BodyString; | ||
| 77 | } | ||
| 78 | 81 | ||
| 79 | XPathNodeIterator iterator = contentHandler.EvaluateXPath(XPathToSearch); | 82 | List<string> values = new List<string>(); | 
| 80 | 83 | ||
| 81 | List<string> values = new List<string>(); | 84 | int i = 0; | 
| 85 | while (iterator.MoveNext()) | ||
| 86 | { | ||
| 87 | XPathNavigator nav2 = iterator.Current.Clone(); | ||
| 82 | 88 | ||
| 83 | int i = 0; | 89 | string value; | 
| 84 | while (iterator.MoveNext()) | 90 | if (nav2.NodeType.Equals(XPathNodeType.Element)) | 
| 85 | { | 91 | { | 
| 86 | XPathNavigator nav2 = iterator.Current.Clone(); | 92 | value = nav2.InnerXml; | 
| 93 | } | ||
| 94 | else | ||
| 95 | { | ||
| 96 | value = nav2.Value; | ||
| 97 | } | ||
| 87 | 98 | ||
| 88 | string value; | 99 | values.Add(value); | 
| 89 | if (nav2.NodeType.Equals(XPathNodeType.Element)) | 100 | if (!ExtractRandomMatch && (i == Index)) | 
| 90 | { | 101 | { | 
| 91 | value = nav2.InnerXml; | 102 | // add the extracted value to the Web test context | 
| 92 | } | 103 | e.WebTest.Context.Add(this.ContextParameterName, value); | 
| 93 | else | 104 | e.Success = true; | 
| 94 | { | 105 | return; | 
| 95 | value = nav2.Value; | 106 | } | 
| 107 | i++; | ||
| 96 | } | 108 | } | 
| 97 | 109 | ||
| 98 | values.Add(value); | 110 | if (ExtractRandomMatch && (values.Count > 0)) | 
| 99 | if (!ExtractRandomMatch && (i == Index)) | ||
| 100 | { | 111 | { | 
| 101 | // add the extracted value to the Web test context | 112 | Random random = new Random(); | 
| 102 | e.WebTest.Context.Add(this.ContextParameterName, value); | 113 | e.WebTest.Context.Add(this.ContextParameterName, values[random.Next(values.Count)]); | 
| 103 | e.Success = true; | 114 | e.Success = true; | 
| 104 | return; | 115 | return; | 
| 105 | } | 116 | } | 
| 106 | i++; | ||
| 107 | } | ||
| 108 | |||
| 109 | if (ExtractRandomMatch && (values.Count > 0)) | ||
| 110 | { | ||
| 111 | Random random = new Random(); | ||
| 112 | e.WebTest.Context.Add(this.ContextParameterName, values[random.Next(values.Count)]); | ||
| 113 | e.Success = true; | ||
| 114 | return; | ||
| 115 | } | 117 | } | 
| 116 | } | 118 | } | 
| 117 | catch (Exception ex) | 119 | catch (Exception ex) | 
| ... | @@ -119,7 +121,8 @@ namespace WebTest.WebService.Plugin.Runtime | ... | @@ -119,7 +121,8 @@ namespace WebTest.WebService.Plugin.Runtime | 
| 119 | e.Message = ex.Message; | 121 | e.Message = ex.Message; | 
| 120 | } | 122 | } | 
| 121 | 123 | ||
| 122 | e.Success = false; | 124 | e.Success = passIfNotFound; | 
| 125 | e.Message += " (XPath not found)"; | ||
| 123 | return; | 126 | return; | 
| 124 | } | 127 | } | 
| 125 | } | 128 | } | ... | ... | 
- 
Please register or sign in to post a comment
