Skip to content

Crazy blog

No money, no honey, no funny…


controlleragent

service running

group

RegExCustomExtractor:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.WebTesting;

namespace Core
{
[DisplayName(“RegularExpressionExtractor”)]
[Description(“Extract from Any page – index default is 1”)]
public class RegExCustomExtractor : ExtractionRule
{
public string regEx { get; set; }
public string index { get; set; }
public string expectValue { get; set; }

public override void Extract(object sender, ExtractionEventArgs e)
{
int i = 1;
if (String.IsNullOrWhiteSpace(regEx) == true)
{
Fail(e, “RegEx is invalid”);
}
if (String.IsNullOrWhiteSpace(index) != true)
{
i = Int32.Parse(index);
}
if (String.IsNullOrWhiteSpace(expectValue) == true)
{
expectValue = “”;
}
try
{
Regex regex = new Regex(regEx);
var v = regex.Match(e.Response.BodyString);
string s = v.Groups[i].ToString().Trim();

if (expectValue.Length < 1)
{
e.WebTest.Context.Add(ContextParameterName, s);
e.Success = true;
return;
}
else
{
if (s.Equals(expectValue) && s.Length > 0)
{
e.WebTest.Context.Add(ContextParameterName, s);
e.Success = true;
}
else
{
Fail(e, “Expect: ” + expectValue + “, but actual is: ” + s);
}
}

}
catch (Exception ex)
{
Fail(e, ex.ToString());
}

}

private void Fail(ExtractionEventArgs e, string p)
{
e.Success = false;
e.Message = p;
}
}
}

 

GenerateExternalNumber:

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using Microsoft.VisualStudio.TestTools.WebTesting;

namespace Core
{
public class GenerateExternalNumber : WebTestRequestPlugin
{

[Description(“If True, the random number will be recalculated before each request is made. The default is false.”)]
[DefaultValue(true)]
public bool UpdateBeforeRequest { get; set; }

[Description(“Name of the Context Paramter that will stroe the random number.”)]
public string ContextParameter { get; set; }

//[Description(“Parameter Name”)]
//public string ParameterName { get; set; }

//[Description(“Parameter Value Pattern”)]
//public string ParameterValuePattern { get; set; }

public override void PreRequest(object sender, PreRequestEventArgs e)
{
base.PreRequest(sender, e);

string value = DateTime.UtcNow.ToString(“yyMMddHHmmssfff”, CultureInfo.InvariantCulture) + new Random().Next(1,100);

e.WebTest.Context[ContextParameter] = value;
}

public override void PreRequestDataBinding(object sender, PreRequestDataBindingEventArgs e)
{
if (UpdateBeforeRequest)
{
string value = DateTime.UtcNow.ToString(“yyMMddHHmmssfff”, CultureInfo.InvariantCulture) + new Random().Next(1, 100);

e.WebTest.Context[ContextParameter] = value;
}
}
}
}

 

Validation:

using System;
using System.Diagnostics;
using System.Globalization;
using Microsoft.VisualStudio.TestTools.WebTesting;

namespace Core
{
public class SaveSOValidate : ValidationRule
{
public override string RuleName
{
get { return “Custom Save SO Validation”; }
}

public override string RuleDescription
{
get { return “Validates that the new SO created successfully.”; }
}

public override void Validate(object sender, ValidationEventArgs e)
{
bool validated = false;

if (e.Response.BodyString.Contains(“SUCCESS”))
{
validated = true;
}

e.IsValid = validated;
if (!validated)
{
e.Message = “Create SO failed.”;
}
}
}
}

Run webservice as SOAPUI

  1. Enter link to that request
  2. Header > SOAPAction = link
  3. Enter String Body
  4. Add Request Plug-ins (Context Parameter Value)
  5. If use Datasource: {{DataSourceAccountSTJ.tblAccount.Uname}}
  6. Add data source, choose table and Access Mode = Random
  7. Done

SQLCOMD /S DICMINHTRUONG\SQLEXPRESS -U sa -P 123456 -i loadtestresultrepository.sql