5

i just added the refrence of Microsoft.VisualStudio.TestTools.UITesting in my project and i try to use the ImageComparer` class but i get an error when running this

 private void Form1_Load(object sender, EventArgs e)
    {
        Image a = Image.FromFile(@"C:\Users\itapi\Desktop\a.png");
        Image b = Image.FromFile(@"C:\Users\itapi\Desktop\b.png");
        ImageComparer.Compare(a,b);
    }

the error is

An unhandled exception of type 'System.TypeInitializationException' occurred in Microsoft.VisualStudio.TestTools.UITesting.dll

Additional information: The type initializer for 'Microsoft.VisualStudio.TestTools.UITest.Extension.UITestUtilities' threw an exception.

does anyone has any idea what's wrong here?

this is the innter excpetion

System.TypeInitializationException: The type initializer for 'Microsoft.VisualStudio.TestTools.UITest.Extension.UITestUtilities' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.VisualStudio.TestTools.UITest.WindowsStoreUtility, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
at Microsoft.VisualStudio.TestTools.UITest.Extension.UITestUtilities..cctor() --- End of inner exception stack trace --- at Microsoft.VisualStudio.TestTools.UITest.Extension.UITestUtilities.CheckForNull(Object parameter, String parameterName) at Microsoft.VisualStudio.TestTools.UITesting.ImageComparer.CompareInternal(Image actualImage, Image expectedImage, ColorDifference argbTolerance, Image& diffImage, Boolean createOutImage) at Microsoft.VisualStudio.TestTools.UITesting.ImageComparer.Compare(Image actualImage, Image expectedImage, ColorDifference argbTolerance) at Microsoft.VisualStudio.TestTools.UITesting.ImageComparer.Compare(Image actualImage, Image expectedImage) at WindowsFormsApplication4.Form1.Form1_Load(Object sender, EventArgs e) in c:\Users\itapi\OneDrive\??????\Visual Studio 2013\Projects\WindowsFormsApplication4\WindowsFormsApplication4\Form1.cs:line 30

pnuts
  • 58,317
  • 11
  • 87
  • 139
  • 2
    What is the inner exception? – israel altar Jul 07 '15 at 14:04
  • @israelaltar what do you mean? –  Jul 07 '15 at 14:08
  • Add the code in try catch and inside catch block get the inner exception. – Microsoft DN Jul 07 '15 at 14:10
  • 1
    if you catch the exception or use debugger (use watch for it) you can see that there is an inner exception. what is it? – israel altar Jul 07 '15 at 14:11
  • @israelaltar added look post –  Jul 07 '15 at 14:25
  • @MicrosoftDN added look post –  Jul 07 '15 at 14:26
  • 1
    That assembly is stored in the C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\PrivateAssemblies directory. Not very far away from where you found UITesting. Emphasis on "private", it was made to only be used from Visual Studio itself. I suppose you can copy it to solve your problem. Don't expect your testing code to port well to future VS versions. – Hans Passant Jul 07 '15 at 15:54
  • @HansPassant no this dll is in C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\PublicAssemblies ..... i dont see any problem of if.. and i do use it in the visual studio.. –  Jul 07 '15 at 19:55
  • Also running into this issue. Were you able to slve this? – TWilly Nov 23 '15 at 21:29
  • Arent the Testing Tools projects only available in Visual Studio Enterprise edition? – Kevin Minehart Dec 29 '15 at 14:08

2 Answers2

9

Could not load file or assembly 'Microsoft.VisualStudio.TestTools.UITest.WindowsStoreUtility ...

That's entirely expected. This assembly was meant to only be used from within Visual Studio. It is present in the C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\PrivateAssemblies directory, quite out of reach from your Winforms app. The CLR will never find it.

Same is true for the Microsoft.VisualStudio.TestTools.UITesting.dll assembly but you got a copy in your bin\Debug directory because you referenced it.

These assemblies were only meant to be used to create unit tests, the kind that you run with the Test > Run menu item. The MSDN how-to article for creating coded UI tests is here.

You can copy the missing assembly with XCOPY in a post build event. But using the integrated unit test feature is certainly best and the only decent way to get a minimal guarantee that this still works when you update the VS version.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Hans, you talk a lot of sense, this explanation worked for me. I am using Selenium, Windows Application Driver and I was linking test cases to TFS as per below link when I got the error. I still don't quite know why MS would think that a developer writting code that used this dll would know this was only intended to work from within Visual Studio but now I know what to do. Personally, I just referenced the dlls. I am not too concerned about future versions, the current version works. https://docs.microsoft.com/en-us/azure/devops/test/run-automated-tests-from-test-hub?view=azure-devops – Ewan Sep 05 '19 at 14:02
1

This question confuses me. Why focus on the exception instead of the problem? It appears you want to compare images. Why not ask how to compare images? A quick search will yield many results, like Fast Bitmap comparison - C#. I searched c# compare images and that was the 10th link. Others before that also have promise. What results do you want true/false, percentage match, image only and exif data mismatch ok, other?

Also, when I look at the documentation for ImageComparer.Compare on MSDN, the signature differs from the sample code in this question. The documentation seems poor on this regarding usage, and if it should be used from within Visual Studio as others have discussed.

Kory Gill
  • 6,993
  • 1
  • 25
  • 33