我正在AzureDevOps网络性能测试中测试一个场景。我需要从一页的响应中提取所有ID。由于存在多个ID并且提取模式相同。如何在一个变量中获取所有ID?
如果页面响应
[ {"Id":20006,"Period":"13-Jul","Territory":"Chicagoelected":true,
"Link":"/PSR03/0026-03-a/Web/api/Document/Index/20006",
"Username":user1,"Viewer":"user1"},
{"Id":6,"Period":"July2013","Territory":"ChicagoI:false,
"Link":"/PSR03/0026-03-a/Web/api/Document/Index/6",
"Username":"user1","Viewer":"user1"},
{Id:25008,"Period":"13-Jun","Territory":"California",
"Selected":false,
"Link":"/PSR03/0026-03-a/Web/api/Document/Index/25008",
"Username":"user1","Viewer":"user1},
{Id":25007,"Period":"13-Jun","Territory":"EV,Selected":false,
"Link":"/PSR03/0026-03-a/Web/api/Document/Index/25007",
"Username":"user1","Viewer":"user1"},
{Id:25010,"Period":"13-May","Territory":"California","Selected":false,
"Link":"/PSR03/0026-03-a/Web/api/Document/Index/25010",
"Username":"user1","Viewer":"user1},
{Id":25009,"Period":"13-May","Territory":"Chicago","Selected:false,
Link":"/PSR03/0026-03-a/Web/api/Document/Index/25009",
"Username":"user1,Viewer":"user1"},
{"Id":25005,Period:"13-Mar,Territory":"Chicago","Selected":false,
"Link":/PSR03/0026-03-a/Web/api/Document/Index/25005",
Username:"user1","Viewer":"user1"},
{"Id":25006,"Period":"13-Mar","Territory":"EV","Selected":false,
"Link":"/PSR03/0026-03-a/Web/api/Document/Index/25006",
"Username":"user1","Viewer":"user1"},
{"Id":25011,"Period":"13-Feb","Territory":"Chicagoelected":false,
"Link":"/PSR03/0026-03-a/Web/api/Document/Index/25011",
"Username":"user1","Viewer:user1"}],
"LocalizationDictionary":
我正在写正则表达式 - “ Id” :( [0-9] *),“ Period” 它正在工作,并在Rubular中提供所有ID。 但是我如何在VSTS Web性能测试中实现它?
答案 0 :(得分:1)
正则表达式提取规则可能从数据中获取一个ID,或者它可能获得整个响应。我不知道内置提取规则可以根据需要获取数据。
我会写一个WebTestRequestPlugin
,其中PostRequest
方法提取并格式化所需的数据。插件本身应该从e.Response.BodyString
访问响应正文。
提取所需数据并将其格式化为一个变量的方法取决于确切的数据格式,并且由于提问者在评论中写入的信息将会很复杂,即"响应文本被正确复制。双引号实际上存在差异"。可能是由于缺少双引号,通常用于解析此类数据的库例程都不会起作用。
插件方法的大纲可能是:
public override void PostRequest(object sender, PostRequestEventArgs e)
{
string[] body= e.Response.BodyString;
string extractedIds = ...; // Extract and format the IDs from body.
e.WebTest.Context["TheContextParameter"] = extractedIds;
}
This Microsoft page提供了有关如何编写插件的详细信息。