WebServiceResult.cs
27.5 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
//*************************************************************************************************
// WebServiceResult.cs
// Owner: Mahipal Kante
//
// UI of the Web Test Result Tab for Web Service request/response
//
// Copyright(c) Microsoft Corporation, 2010
//*************************************************************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualStudio.TestTools.WebTesting;
using EnvDTE;
using mshtml;
using System.IO;
using System.Xml;
using Microsoft.VisualStudio.TestTools.WebTesting.Rules;
using WebTest.WebService.Plugin.Runtime;
using System.Xml.XPath;
namespace WebTest.WebService.Plugin.ResultTab
{
internal partial class WebServiceResult : UserControl
{
public WebServiceResult(Connect connect)
{
this.connect = connect;
InitializeComponent();
this.xmlViewRequest = new XmlTreeView(this.treeViewRequest);
this.xmlViewResponse = new XmlTreeView(this.treeViewResponse);
this.requestTextBox.TextChanged += new EventHandler(requestTextBox_TextChanged);
this.editRequest.CheckedChanged += new EventHandler(EditRequestCheckedChanged);
this.treeViewResponse.AfterSelect += new TreeViewEventHandler(treeViewResponse_AfterSelect);
this.ResponseTab.SelectedTab = this.ResponseTabPageTreeView;
this.RequestTab.SelectedTab = this.RequestTabPageTreeView;
AddContextMenus();
AddToolTips();
}
internal WebTestDetail WebTestDetail
{
get;
set;
}
internal void SetWebTestRequestResult(WebTestRequestResult webTestRequestResult, WebTestResultUnit webTestResultUnit)
{
if (this.xmlViewRequest.MessageEdited)
{
DialogResult result = MessageBox.Show("You have updated the Request Content. Do you want to save?", "Request Update Pending", MessageBoxButtons.YesNo);
if (result.Equals(DialogResult.Yes))
{
UpdateWebTestForRequest();
}
}
ResetUI();
this.webTestRequestResult = webTestRequestResult;
this.webTestResultUnit = webTestResultUnit;
ShowRequest(webTestRequestResult.Request);
ShowResponse(webTestRequestResult.Response);
}
private void AddContextMenus()
{
this.responseEditContextMenu = new ContextMenu();
this.requestEditContextMenu = new ContextMenu();
this.responseViewContextMenu = new ContextMenu();
this.requestViewContextMenu = new ContextMenu();
this.responseEditContextMenu.MenuItems.Add(new MenuItem("Add Extraction Rule", AddExtractionRule));
this.responseEditContextMenu.MenuItems.Add(new MenuItem("Add Validation Rule", AddValidationRule));
MenuItem m = new MenuItem("Parameterize");
m.MenuItems.Add(new MenuItem("Type Parameter", AddParameter));
m.Popup += new EventHandler(ParameterMenuClicked);
this.requestEditContextMenu.MenuItems.Add(m);
}
private void AddToolTips()
{
// Set Tooltips
ToolTip toolTip = new ToolTip();
toolTip.SetToolTip(this.editRequest, "Edit the original content of request");
toolTip = new ToolTip();
toolTip.SetToolTip(this.requestXMLEditor, "View request contents in XML Editor");
toolTip = new ToolTip();
toolTip.SetToolTip(this.responseXMLEditor, "View response contents in XML Editor");
toolTip = new ToolTip();
toolTip.SetToolTip(this.updateLinkLabel, "Update the content of original request");
}
private void EditRequestCheckedChanged(object sender, EventArgs e)
{
bool editing = false;
if (this.webTestResultUnit != null)
{
if (this.editRequest.Checked)
{
if (this.WebTestDetail != null)
{
WebTestRequest requestInWebTest =
WebTestDetail.WebTest.GetItem(this.webTestResultUnit.DeclarativeWebTestItemId)
as WebTestRequest;
if (requestInWebTest != null)
{
ShowRequest(requestInWebTest);
editing = true;
}
}
}
else
{
ShowRequest(this.webTestRequestResult.Request);
}
}
if (editing)
{
this.requestTextBox.ReadOnly = false;
this.updateLinkLabel.Visible = true;
this.treeViewRequest.LabelEdit = true;
this.treeViewRequest.ContextMenu = this.requestEditContextMenu;
}
else
{
this.requestTextBox.ReadOnly = true;
this.updateLinkLabel.Visible = false;
this.treeViewRequest.LabelEdit = false;
this.treeViewRequest.ContextMenu = this.requestViewContextMenu;
}
}
private void ResetUI()
{
this.webTestRequestResult = null;
this.webTestResultUnit = null;
this.xPathNavigator = null;
this.xPathNamespaces = null;
this.requestTextBox.Text = string.Empty;
this.responseTextBox.Text = string.Empty;
this.XPathTextBox.Text = string.Empty;
this.responseContentHandler = null;
this.requestContentHandler = null;
this.treeViewRequest.Nodes.Clear();
this.treeViewResponse.Nodes.Clear();
this.editRequest.Checked = false;
this.updateLinkLabel.Visible = false;
this.requestTreePopulated = false;
this.xmlViewRequest.MessageEdited = false;
}
private void ShowResponse(WebTestResponse webTestResponse)
{
if (webTestResponse != null && webTestResponse.BodyBytes != null)
{
responseContentHandler = ContentHandlerFactory.GetHandler(webTestResponse.ContentType);
if (responseContentHandler != null)
{
if (responseContentHandler.IsBinary)
{
responseContentHandler.MessageBytes = webTestResponse.BodyBytes;
}
else
{
responseContentHandler.MessageString = webTestResponse.BodyString;
}
this.responseTextBox.Text = responseContentHandler.XmlString;
this.xmlViewResponse.ShowXML(responseContentHandler.XmlDocument);
}
}
if (this.treeViewResponse.Nodes.Count > 0)
{
if (webTestResultUnit == null || WebTestDetail == null)
{
this.treeViewResponse.ContextMenu = this.responseViewContextMenu;
}
else
{
this.treeViewResponse.ContextMenu = this.responseEditContextMenu;
}
}
else
{
this.treeViewResponse.ContextMenu = null;
}
if (string.IsNullOrEmpty(this.responseTextBox.Text))
{
this.responseXMLEditor.Visible = false;
}
else
{
this.responseXMLEditor.Visible = true;
}
}
private void ShowRequest(WebTestRequest webTestRequest)
{
this.requestContentHandler = null;
if (webTestRequest != null && webTestRequest.Body != null)
{
BinaryHttpBody binaryHttpBody = webTestRequest.Body as BinaryHttpBody;
if (binaryHttpBody != null)
{
this.requestContentHandler = ContentHandlerFactory.GetHandler(binaryHttpBody.ContentType);
if (this.requestContentHandler != null)
{
this.requestContentHandler.MessageBytes = binaryHttpBody.Data;
}
}
StringHttpBody stringHttpBody = webTestRequest.Body as StringHttpBody;
if (stringHttpBody != null)
{
this.requestContentHandler = ContentHandlerFactory.GetHandler(stringHttpBody.ContentType);
if (this.requestContentHandler != null)
{
this.requestContentHandler.MessageString = stringHttpBody.BodyString;
}
}
if (this.requestContentHandler != null)
{
this.requestTextBox.Text = this.requestContentHandler.XmlString;
xmlViewRequest.ShowXML(this.requestContentHandler.XmlDocument);
this.requestTreePopulated = true;
}
}
this.xmlViewRequest.MessageEdited = false;
if (string.IsNullOrEmpty(this.requestTextBox.Text))
{
this.requestXMLEditor.Visible = false;
this.editRequest.Visible = false;
}
else
{
this.requestXMLEditor.Visible = true;
this.editRequest.Visible = (this.WebTestDetail != null);
}
}
private void ParameterMenuClicked(object sender, EventArgs e)
{
MenuItem menuItem = sender as MenuItem;
if (menuItem != null)
{
DeclarativeWebTest webTest = WebTestDetail.WebTest;
menuItem.MenuItems.Clear();
menuItem.MenuItems.Add(new MenuItem("Type Parameter", AddParameter));
for (int i = 1; i < this.webTestResultUnit.DeclarativeWebTestItemId; i++)
{
WebTestRequest requestInWebTest = webTest.GetItem(i) as WebTestRequest;
if (requestInWebTest != null)
{
foreach (ExtractionRuleReference reference in requestInWebTest.ExtractionRuleReferences)
{
menuItem.MenuItems.Add(new MenuItem(reference.ContextParameterName, AddParameter));
}
}
}
}
}
private void AddParameter(object sender, EventArgs e)
{
MenuItem menuItem = sender as MenuItem;
if (menuItem == null)
{
return;
}
this.xmlViewRequest.AddParameter(menuItem.Text);
}
private void requestTextBox_TextChanged(object sender, EventArgs e)
{
this.xmlViewRequest.MessageEdited = true;
}
/// <summary>
/// 1. Enable Extraction Rule if known Web Test
/// 2. Before adding extraction rule, check if there is an open unsaved document
/// 3. Prompt user to save if unsaved, and save it
/// 4. Parse Declarative Web Test from file, and add extraction rule
/// 5. Open the document again, if it was open previously
/// 6. Using TimeStamp,determine if Declarative Web Test at hand is uptodate.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AddExtractionRule(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.XPathTextBox.Text))
{
return;
}
if (WebTestDetail != null)
{
// If there are pending changes, save first, as we are going to make our
// changes to the file directly.
WebTestDetail.PrepareToSave();
WebTestRequest requestInWebTest = WebTestDetail.WebTest.GetItem(this.webTestResultUnit.DeclarativeWebTestItemId) as WebTestRequest;
if (requestInWebTest != null)
{
string contextParamName = "xpathParam";
TreeNode selectedNode = this.treeViewResponse.SelectedNode;
if (selectedNode != null)
{
if (selectedNode.Nodes.Count > 0)
{
contextParamName = selectedNode.Text;
}
else if (selectedNode.Parent != null)
{
contextParamName = selectedNode.Parent.Text;
}
}
ExtractionRuleReference extractTextReference = new ExtractionRuleReference();
extractTextReference.Type = typeof(XPathExtractionRule);
extractTextReference.ContextParameterName = contextParamName;
extractTextReference.Properties.Add(new PluginOrRuleProperty("XPathToSearch", this.XPathTextBox.Text));
extractTextReference.Properties.Add(new PluginOrRuleProperty("Index", "0"));
extractTextReference.Properties.Add(new PluginOrRuleProperty("ExtractRandomMatch", "False"));
// Added new passIfNotFound
extractTextReference.Properties.Add(new PluginOrRuleProperty("passIfNotFound", "False"));
requestInWebTest.ExtractionRuleReferences.Add(extractTextReference);
WebTestDetail.Save();
string message = string.Format(
@"The extraction rule was created, and the text is extracted to a context parameter named '{0}'. You will need to add bindings to this context parameter in the appropriate places in the script.
XPath to Evaluate: ‘{1}’
The extraction rule was created under the following request:
‘{2}’", contextParamName, this.XPathTextBox.Text, requestInWebTest.Url);
MessageBox.Show(message, "Extraction Rule Created");
}
}
}
private void AddValidationRule(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.XPathTextBox.Text))
{
return;
}
if (WebTestDetail != null)
{
TreeNode treeNode = this.treeViewResponse.SelectedNode;
TreeNode parentNode = treeNode.Parent;
if (parentNode == null)
{
return;
}
// If there are pending changes, save first, as we are going to make our
// changes to the file directly.
WebTestDetail.PrepareToSave();
WebTestRequest requestInWebTest = WebTestDetail.WebTest.GetItem(this.webTestResultUnit.DeclarativeWebTestItemId) as WebTestRequest;
if (requestInWebTest != null)
{
string expectedValue = string.Empty;
SetupResponseXpath();
// Compile a standard XPath expression
XPathExpression expr;
expr = this.xPathNavigator.Compile(this.XPathTextBox.Text);
expr.SetContext(xPathNamespaces);
XPathNodeIterator iterator = this.xPathNavigator.Select(expr);
if (iterator.MoveNext())
{
XPathNavigator nav2 = iterator.Current.Clone();
expectedValue = nav2.InnerXml;
}
ValidationRuleReference validationTextReference = new ValidationRuleReference();
validationTextReference.Type = typeof(XPathValidationRule);
validationTextReference.Properties.Add(new PluginOrRuleProperty("XPathToSearch", this.XPathTextBox.Text));
validationTextReference.Properties.Add(new PluginOrRuleProperty("Index", "0"));
validationTextReference.Properties.Add(new PluginOrRuleProperty("IgnoreCase", "True"));
validationTextReference.Properties.Add(new PluginOrRuleProperty("ExpectedValue", expectedValue));
requestInWebTest.ValidationRuleReferences.Add(validationTextReference);
WebTestDetail.Save();
string message = string.Format(
@"The validation rule was created.
XPath to Evaluate: ‘{0}’
Expected Value: '{1}'
The extraction rule was created under the following request:
‘{2}’", this.XPathTextBox.Text, expectedValue, requestInWebTest.Url);
MessageBox.Show(message, "Validation Rule Created");
}
}
}
private void ResponseTabChanged(object sender, EventArgs e)
{
}
private void RequestTabChanged(object sender, EventArgs e)
{
if (requestTreePopulated && (!this.editRequest.Checked))
{
return;
}
if (string.IsNullOrEmpty(this.requestTextBox.Text))
{
return;
}
if (this.RequestTab.SelectedTab.Equals(this.RequestTabPageTreeView))
{
if (this.requestContentHandler != null)
{
this.requestContentHandler.XmlString = this.requestTextBox.Text;
this.xmlViewRequest.ShowXML(this.requestContentHandler.XmlDocument);
}
}
else
{
if (this.requestContentHandler != null)
{
this.requestTextBox.Text = this.requestContentHandler.XmlString;
}
}
this.requestTreePopulated = true;
}
private void requestXMLEditor_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (this.requestContentHandler != null)
{
string message = string.Empty;
if (!this.RequestTab.SelectedTab.Equals(this.RequestTabPageTreeView))
{
this.requestContentHandler.XmlString = this.requestTextBox.Text;
}
this.connect.ShowInXMLEditor(this.webTestRequestResult.Request.Url + " [Request]",
this.requestContentHandler.XmlString);
}
}
private void responseXMLEditor_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (this.responseContentHandler != null)
{
this.connect.ShowInXMLEditor(this.webTestRequestResult.Request.Url + " [Response]", this.responseTextBox.Text);
}
}
private void updateLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
UpdateWebTestForRequest();
}
private void UpdateWebTestForRequest()
{
if (WebTestDetail != null && this.requestContentHandler != null)
{
// If there are pending changes, save first, as we are going to make our
// changes to the file directly.
WebTestDetail.PrepareToSave();
// Get the original request
WebTestRequest requestInWebTest =
WebTestDetail.WebTest.GetItem(this.webTestResultUnit.DeclarativeWebTestItemId)
as WebTestRequest;
if (requestInWebTest == null)
{
// error
return;
}
// Update content if edited in text box
if (!this.RequestTab.SelectedTab.Equals(this.RequestTabPageTreeView))
{
this.requestContentHandler.XmlString = this.requestTextBox.Text;
}
// Fill the request body with updated content
if (this.requestContentHandler.IsBinary)
{
BinaryHttpBody binaryHttpBody = requestInWebTest.Body as BinaryHttpBody;
if (binaryHttpBody != null)
{
// set the new binary data into http body
binaryHttpBody.Data = this.requestContentHandler.MessageBytes;
return;
}
}
else
{
StringHttpBody stringHttpBody = requestInWebTest.Body as StringHttpBody;
if (stringHttpBody != null)
{
stringHttpBody.BodyString = this.requestContentHandler.MessageString;
}
}
// Save WebTest
WebTestDetail.Save();
this.xmlViewRequest.MessageEdited = false;
}
}
#region XPathResponse
private void treeViewResponse_AfterSelect(object sender, TreeViewEventArgs e)
{
TreeNode selectedNode = this.treeViewResponse.SelectedNode;
if (selectedNode == null)
{
return;
}
TreeNode parentNode = selectedNode.Parent;
if (parentNode == null)
{
this.XPathTextBox.Text = "/" + GetXPathSubExpression(selectedNode.Text);
return;
}
if (selectedNode.Nodes.Count == 0)
{
selectedNode = parentNode;
parentNode = parentNode.Parent;
}
string xPath = string.Empty;
bool addedDoubleSlashes = false;
while (parentNode != null)
{
int index = 1;
if (parentNode.Nodes.Count > 1)
{
for (int i = 0; i < selectedNode.Index; i++)
{
if (parentNode.Nodes[i].Text.Equals(selectedNode.Text))
{
index++;
}
}
string xpathExpression = GetXPathSubExpression(selectedNode.Text);
if (index > 1)
{
xPath = "/" + xpathExpression + "[" + index + "]" + xPath;
}
else
{
xPath = "/" + xpathExpression + xPath;
}
addedDoubleSlashes = false;
}
else
{
if (string.IsNullOrEmpty(xPath))
{
xPath = "/" + GetXPathSubExpression(selectedNode.Text);
}
else if (!addedDoubleSlashes)
{
// Ignore intermediate single elements to reduce XPath length
xPath = "/" + xPath;
addedDoubleSlashes = true;
}
}
selectedNode = parentNode;
parentNode = parentNode.Parent;
}
if (!addedDoubleSlashes)
{
xPath = "/" + xPath;
}
this.XPathTextBox.Text = xPath;
}
private string GetXPathSubExpression(string name)
{
if (name.Contains(':'))
{
return name;
}
else
{
return string.Format("*[local-name()='{0}']", name);
}
}
private void XPathButton_Click(object sender, EventArgs e)
{
try
{
SetupResponseXpath();
// Compile a standard XPath expression
XPathExpression expr;
expr = this.xPathNavigator.Compile(this.XPathTextBox.Text);
expr.SetContext(xPathNamespaces);
XPathNodeIterator iterator = this.xPathNavigator.Select(expr);
int i = 1;
while (iterator.MoveNext())
{
XPathNavigator nav2 = iterator.Current.Clone();
string value = string.Empty;
if (nav2.NodeType.Equals(XPathNodeType.Element))
{
value = nav2.InnerXml;
}
else
{
value = nav2.Value;
}
if (i < iterator.Count)
{
string displayText = string.Format("Result[{0} of {1}]:{2}{3}{4}{5}{6}Do you want to display next?",
i,
iterator.Count,
Environment.NewLine,
Environment.NewLine,
value,
Environment.NewLine,
Environment.NewLine);
DialogResult result = MessageBox.Show(displayText, "XPath Result", MessageBoxButtons.YesNoCancel);
if (!result.Equals(DialogResult.Yes))
{
return;
}
}
else
{
string displayText = string.Format("Result[{0} of {1}]:{2}{3}{4}",
i,
iterator.Count,
Environment.NewLine,
Environment.NewLine,
value);
MessageBox.Show(displayText, "XPath Result");
return;
}
i++;
}
MessageBox.Show("No Results to Display", "XPath Result");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "XPath Result");
}
}
private void SetupResponseXpath()
{
if (xPathNavigator == null && this.responseContentHandler != null)
{
this.xPathNavigator = new XPathDocument(
new StringReader(this.responseContentHandler.XmlString)
).CreateNavigator();
this.xPathNamespaces = new XmlNamespaceManager(this.xPathNavigator.NameTable);
while (this.xPathNavigator.MoveToFollowing(XPathNodeType.Element))
{
IDictionary<string, string> whatever = this.xPathNavigator.GetNamespacesInScope(XmlNamespaceScope.All);
foreach (KeyValuePair<string, string> key in whatever)
{
this.xPathNamespaces.AddNamespace(key.Key, key.Value);
}
}
}
}
#endregion
private WebTestRequestResult webTestRequestResult;
private WebTestResultUnit webTestResultUnit;
private ContextMenu responseEditContextMenu;
private ContextMenu requestEditContextMenu;
private XmlTreeView xmlViewRequest;
private XmlTreeView xmlViewResponse;
private ContextMenu responseViewContextMenu;
private ContextMenu requestViewContextMenu;
private bool requestTreePopulated = false;
private XPathNavigator xPathNavigator = null;
private XmlNamespaceManager xPathNamespaces = null;
private ContentHandler responseContentHandler;
private ContentHandler requestContentHandler;
private Connect connect;
}
}