ExtractionRules.cs
3.94 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
/************************************************************************************************
* All code in this file is under the MS-RL License (https://opensource.org/licenses/MS-RL) *
* By using the code in this file in any way, you agree to the above license terms. *
* Copyright (C) LIGHTS IN LINE AB (https://www.lightsinline.se) *
* Repository, Wiki, Issue tracker and more at https://git.lightsinline.se/products/VSTT-Plugins *
* *
* Contributors *
* LIGHTS IN LINE AB *
* SWEDBANK AB *
* SKATTEVERKET *
************************************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using Microsoft.VisualStudio.TestTools.WebTesting;
using Microsoft.VisualStudio.TestTools.LoadTesting;
using System.IO;
using System.ComponentModel;
namespace LIL_VSTT_Plugins
{
/// <summary>
/// Nästlad extraction rule
/// Kombination av två extractions rules i en. Den andra söker i resultatet av den första.
/// </summary>
[DisplayName("Nested Extraction")]
[Description("(C) Copyright 2011 LIGHTS IN LINE AB\r\nKombination av två extractions där den andra söker i resultatet av den första.")]
public class NestedExtractionRule : ExtractionRule
{
// Indata
private string start1;
[DisplayName("Extraction 1 Starts With")]
[Description("")]
public string Start1
{
get { return start1; }
set { start1 = value; }
}
private string end1;
[DisplayName("Extraction 1 Ends With")]
[Description("")]
public string End1
{
get { return end1; }
set { end1 = value; }
}
private string start2;
[DisplayName("Extraction 2 Starts With")]
[Description("")]
public string Start2
{
get { return start2; }
set { start2 = value; }
}
private string end2;
[DisplayName("Exctration 2 Ends With")]
[Description("")]
public string End2
{
get { return end2; }
set { end2 = value; }
}
public override void Extract(object sender, ExtractionEventArgs e)
{
// Find the first extraction
// TODO: Du behöver spola fram till att börja sökningen av end1 EFTER start1!
string extraction1;
int s1, s2, e1, e2;
s1 = e.Response.BodyString.IndexOf(start1) + start1.Length;
e1 = e.Response.BodyString.IndexOf(end1);
// Validate
if (s1 > e.Response.BodyString.Length || (e1 - s1 < 1))
{
// Error
e.Success = false;
return;
}
extraction1 = e.Response.BodyString.Substring(s1, e1 - s1);
// Find the second extraction
// TODO: Du behöver spola fram till att börja sökningen av end2 EFTER start2!
string extraction2;
s2 = extraction1.IndexOf(start2) + start2.Length;
e2 = extraction1.IndexOf(end2);
// Validate
if (s2 > extraction1.Length || (e2 - s2 < 1))
{
// Error
e.Success = false;
return;
}
extraction2 = extraction1.Substring(s2, e2 - s2);
e.WebTest.Context.Add(this.ContextParameterName, extraction2);
e.Success = true;
}
}
}