-1

这是我想测试加载和提取数据的 html 代码

<body>
    <h1>Hello StackOverflow</h1>

    <input type="hidden" name="Secret" value="66"/>
    <input type="hidden" name="Field[66].value" value="333" />
</body>

我需要333价值。为此,我需要从中提取值66Secret然后使用该值推断字段的名称(此处Field[66].value)。我怎样才能做到这一点?

我尝试使用两个Extract Attribute Value提取规则,但因为有相同的响应,第一个值还没有在上下文中。

4

1 回答 1

0

解决方案是按照此处Custom Extraction Rule的说明创建一个,并使用正则表达式来解析两次 html 正文

提取方法:

public override void Extract(object sender, ExtractionEventArgs e)
{
    var val1 = GetInputValueByName(e.Response.BodyString, "Secret");
    var val2 = GetInputValueByName(e.Response.BodyString, $"Field[{val1}].value");

    if (!string.IsNullOrEmpty(val2 ))
    {
        e.Success = true;
        e.WebTest.Context.Add(this.ContextParameterName, val2);
    }
    else
    {
        // If the extraction fails, set the error text that the user sees
        e.Success = false;
        e.Message = String.Format(CultureInfo.CurrentCulture, "Not Found: {0}", Name);
    }

}


private string GetInputValueByName(string html, string name)
{
    Match match = Regex.Match(html, "<input[^>]*name=\"" + Regex.Escape(name) + "\"[^>]*value=\"([^\"]*)\"");
    if (match.Success)
    {
        return match.Groups[1].Value;
    }
    return string.Empty;
}
于 2018-01-10T14:39:11.210 回答