Skip to content

Crazy blog

No money, no honey, no funny…

Category Archives: C# Test Project

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

 

Advertisements

Here is example step by step to create load test on MS Visual Studio 2010

From Test Project, choose menu Test > New Test > Load Test
1. Welcome screen Load Test display as

2. Name a scenario and choose Do not use think time

3. Enter the number of users to simulate load test

4. Choose test mix model

5. Add test exists

6. Choose Run settings, test iterations 10, Finish

7. Try to run, the error display: The load test results database could not be opened. Check that the load test results database specifies a database that contains the load test schema and that is currently available. For more information, see the Visual Studio help topic ‘About the load test result store’. The connection error was: An error occurred while attempting to create the load test results repository schema: To create the database ‘LoadTest2010’ your user account must have the either the SQL server ‘sysadmin’ role or both the ‘serveradmin’ and ‘dbcreator’ roles.

8. Update the storage type Database to None to fix that problem

9. Save and Run load test

Tags:

Today, I research about create project test Coded UI on C# (Microsoft Visual Studio Ultimate 2010). The first time, it creates difficult code, and very hard to read. But I found that I can create the test so simple, don’t need generate code test of Microsoft. Please read this post for more detail about the simple Coded UI test automation

1. Create Test Project: File > New Project > C# Test Project
2. Delete file UnitTest1.cs in Solution Explorer
3. From menu, choose Test > New Test
4. Choose Coded UI Test + name the file as: RunGMail.cs
5. Add this library

using Microsoft.VisualStudio.TestTools.UITesting.HtmlControls;
using Mouse = Microsoft.VisualStudio.TestTools.UITesting.Mouse;

6. Add this script to function CodedUITestMethod1():
BrowserWindow browser = BrowserWindow.Launch(new Uri("http://gmail.com"));
HtmlEdit txtUsername = new HtmlEdit(browser);
txtUsername.SearchProperties[HtmlEdit.PropertyNames.Id] = "Email";
txtUsername.Text = "truongkimminh";
HtmlEdit txtPassword = new HtmlEdit(browser);
txtPassword.SearchProperties[HtmlEdit.PropertyNames.Id] = "Passwd";
txtPassword.Text = "your-password";
HtmlInputButton btnLogin = new HtmlInputButton(browser);
btnLogin.TechnologyName = "Web";
btnLogin.SearchProperties.Add(HtmlInputButton.PropertyNames.Name, "signIn");
Mouse.Click(btnLogin);

7. Save and now press F5 to run Test

Tags: ,

1. Open Visual Studio 2010

2. File > New Project > Visual C# > Test > Test Project

3. Delete the UnitTest1.cs file and add a Coded UI Test by right clicking on the Project Name -> Add -> New Test

4. Add new TestMethod, then right click, choose Use Coded UI Test Builder

5. Record your test and then click Generate Code

6. Name your code then click Add and generate button

7. Run the test by placing the cursor in the function, right click in the method and click “Run Tests”

8. Finish

9. Please careful when try to edit UIMap.Designer.cs and UIMap.uitest.

File Description Can this be edited?
GMailTest.cs The default name for the file is CodedUITest1.cs. This is the file in which all the Test Methods are added. YES
UIMap.uitest An XML file has all the information about the recording including actions, objects and data. NO today. In future, we may have editor for this file.
UIMap.Designer.cs This file contains the code generated by the tool based on the recording in UIMap.uitest file (a.k.a. code behind file). NO. Any edits to this will be lost.
UIMap.cs The main class generated by the tool is UIMap. This is partial class and is split into UIMap.Designer.cs  and UIMap.cs. While UIMap.Designer.cs contains code generated by the tool and should not be edited, the UIMap.cs file is the place where you would place all your customization. More on this later. YES

Tags: