C#, Shared Libraries and DLLs


, , , , ,

Starting out with a basic Windows Form application, with variables declared in a public class called ‘OurColours’, the private class for each button is something like:

private void cmdRed_Click(object sender, EventArgs e)
txtSelected.Text = OurColours.red;

When the button is clicked the string in OurClass is displayed in the text box (txtSelected). But variables don’t need to exist in the same source file. They can exist in a shared object, as demonstrated in my previous post. Just as before, I’ve added a new Class Library project to the solution.


The new Class Library uses the GlobalParameters namespace, and the public class (userDefined) is copied into it:


Returning to the first project where the form was created, we add the GlobalParameters namespace to its references.


Now the code in each form button can be modified to point to variables in GlobalParameters.userDefined. e.g.

private void button4_Click(object sender, EventArgs e)
txtSelected.Text = GlobalParameters.userDefined.orange;

Writing to the Shared Object
A shared object also enables communication between forms. Within GlobalParameters.userDefined I added another variable called ‘lastSelected‘:
public static string lastSelected;

Then I added another line to each form button code, so it became like:
private void cmdBlue_Click(object sender, EventArgs e)
txtSelected.Text = GlobalParameters.userDefined.blue;
GlobalParameters.userDefined.lastSelected = txtSelected.Text + " was selected";

Finally I added another form that displays whatever string was passed to GlobalParameters.userDefined.lastSelected. This form has just one label (lblLastSelected), and the text is determined by adding the following after the InitializeComponent() line:
lblLastSelected.Text = GlobalParameters.userDefined.lastSelected;

And here is the finished article:


As a final note, we can add another class in the shared library, if ever the variables need to be grouped. To do this, simply add a new public class. The variables here are then referred to using GlobalParameters.newVariableGroup.[variable name].


Also, we might want to make the application portable, distributing it with the shared C# class as a Dynamic Link Library (DLL). This can be done by right-clicking the GlobalParameters References entry for the project, and setting its Copy Local property to ‘True’.


Download Visual Studio project…

Create a Shared C# Object


, , ,

Finally I’ve cracked the CSV file problem, sort of, by creating a file that’s shared across multiple Visual C# projects. It took some debugging and trial-and-error, but it works, and it could be applied to other situations where multiple projects need to access a shared C# class. The following method is also way easier than the CSV thing.

As the starting point, we’ll assume a solution with several projects has already been created. Right-click on the root entry in the Solution Explorer, and add a new Class Library project to the solution.


An empty C# class is automatically generated by default. The first task is to create a namespace that other projects will reference the variables from. Change the autogenerated class to a ‘public static class’, and add a few variables inside it. Personally I’m declaring everything as a ‘public static string’.

In my case, one of the variables uses the Playback.EncryptText() function, which is called from Microsoft.VisualStudio.TestTools, so that library must be imported. When done, the code should look something like:


Additionally the imports also require ‘assembly references’. Right-click on the References entry for the C# item in Solution Explorer, then select ‘Add Reference…’.


The Reference Manager will display an exhaustive list of libraries available to Visual Studio, and we want to find those with the same namespaces as the imports. For example, as I’ve imported Microsoft.VisualStudio.TestTools.UITesting, I want to select that in the Reference Manager.


At this point, we now have a C# class storing variables accessible to all the projects. The next step is to point those projects to the object.

The projects access the shared C# class/object in almost the same way they access Visual Studio’s library objects. Each therefore requirees an assembly reference to the shared C# file and pointers to its namespace.
For each project in the Solution Explorer, add a reference to the namespace. This will be listed in the Solution menu in the Reference Manager.


Finally the project’s source can be modified to actually use the namespace and access the global/public variables. e.g.


Now all that’s left to clean up by removing the old variable declarations within the individual projects.

Screw the CSV File!


, , , , , , , , ,

Having messed up some test projects in trying read values from CSV files, and emulating several online tutorials, I decided to try a different approach. A Coded UI Test project directory has UIMap.Designer.cs, which does pretty much the same thing as the CSV file, so why not edit that instead?


The easiest way of altering the parameters it contains is by double-clicking the UIMap.uitest entry in the Solution Explorer and changing the values in the Properties window.


The less convenient way of changing the parameters is by modifying the lines in UIMap.Designer.cs. It doesn’t work with passwords, though – these are stored in an encrypted form, and replacing it with a plaintext value would break the test script. Any new password must be encrypted beforehand.


Fortunately I din’t have to write a function for that, as Microsoft already provides one: Playback.EncryptText(). Simply use this, and change the variable passed to this function from the UIMap.Designer.cs.


But the whole point of adding a CSV file was to make it easier for others to change the test parameters. The next best thing is having all the variables declared near the head of UIMap.Designer.cs. Why not create another partial public class, and have the script reference that instead of this.[Method]Params class?

Just above the [GeneratedCode] in UIMap.Designer.cs, I inserted the following:


And changed the variables in the other class to reference them:


How to Load Test with Visual Studio


, , , , , , , , ,

This is very different to the coded UI tests that I covered last week, and the process isn’t quite as straightforward.

The following are required:
* Visual Studio (Ultimate)
* Visual Studio Online account or Team Foundation Server

Setting Up
A load test uses a ‘Team Foundation Server’ (TFS), which is either an individual installation or obtained by registering a Visual Studio Online account at https://visualstudio.com.
When that’s sorted, sign into the TFS by clicking the icon at the top-right of the Visual Studio environment.


The project details will appear in Team Explorer window, where the Solution Explorer normally is.


Creating a Web Performance Testing Session
A Web Performance Test Project must be set up before any load testing is done. In Visual Basic or Visual C#, navigate to ‘Test’ and select ‘Web Performance and Load Test Project’. The Solution Explorer will re-appear. Add a Web Performance Test to the project.



Visual Studio should automatically begin recording the Internet Explorer session, unless an existing project is selected and the Add Recording button is clicked. The Microsoft Web Test Recorder browser add-on must be enabled at this point.


When finished, click the Stop Recording button. The steps will be displayed in the main window, and can be replayed using the Run Test button above.
A successful run will produce output similar to the following example:


The Load Test
With a recorded Web test session, the project should be ready for a load test. This time, add a Load Test to the project. This will start the New Load Test Wizard – this is where the ‘load test scenario’ is created and the Web test scripts imported.


When the dialogue closes, a tree structure will appear in Visual Studio’s main window. With a bit of luck Visual Studio will connect to the right server, queue the test and execute it when the Run Test icon is clicked.


Analysing the Results
But how to get the metrics and flashy graphs we’re after? Simple: Just double-click the load test name to get the tree structure in the main window, then click the Open and Manage Results button at the top-left.


In the Open and Manage Load Test Results, click ‘Open…’, and the metrics will appear in Visual Studio’s main window.


Visual Studio Test Script Chaining


, , , , , ,

Building on what I’ve covered in the last post, I’ve been developing my team’s capability to initiate a batch of automated scripts, which is useful if we’re off to the pub, or need to deploy an updated application quickly.

For this to work, a ‘solution’ must be created in Visual Studio, and the project files for the scripts imported. To do this, simply create a blank solution file (Other Project Types – Visual Studio Solutions, in the New Project window).


The Solution Explorer window appears to the right of the Visual Studio IDE. We have two options here: either create new projects within the solution, or add existing project files.
Because it’s important to name the methods and ‘assertions’ properly this time, in order to pin down errors within a much larger test, I’ve created new script projects from scratch. These will be two very basic scripts – one of which is a webmail account login and logout, and the other uses the calculator application.

Right-click inside Solution Explorer and select Add — New Project.


When created, follow the same procedure as before to onfirm the first script works on its own, then right-click on the solution title to add another project. The calculator application script has ‘assertions’ added for checking the application’s output is what it should be, and that specific interface elements are displaying the right text.

Once that’s working, return to the Visual Studio IDE. The two test projects are listed in the Solution Explorer window..


The final step is to run both projects in sequence instead of individually. You’re probably going mental trying to figure out how to do this. What I did was, in the Test Explorer window, click the ‘Run All’ link. This will execute the chain of test scripts we’ve created.



Get every new post delivered to your Inbox.

Join 25 other followers