locked
Why is FileName null in the UIMap.Saving event in my UITestExtensionPackage RRS feed

  • Question

  • I wrote a CodedUI plug-in by inheriting from UITestExtensionPackage. It accesses the UIMap class by using the UITest.Saving Event. Now I want to find out the location of the UIMap file (in order to check if a certain NuGet Package is installed in that project).

    In the event handler both the sender parameter and the UITest property in the EventArgs refer to the same UITest object containing one UIMap. But whatever I do the FileName property of the UIMap is always null. The property Id is set and returns the name of the UIMap File minus the file extension. But without the file path of the project there is no way for me to get the information of the project that I need.

    Anyone got an idea why FileName is null or what to do about it?

    using Microsoft.VisualStudio.TestTools.UITest.Common;
    using Microsoft.VisualStudio.TestTools.UITest.Common.UIMap;
    using Microsoft.VisualStudio.TestTools.UITest.Extension;
    using System;
    
    [assembly: Microsoft.VisualStudio.TestTools.UITest.Extension.UITestExtensionPackageAttribute(
                        "MyPluginPackage",
                        typeof(MyNamespace.MyPluginPackage))]
    
    namespace MyNamespace
    {
        public class MyPluginPackage : UITestExtensionPackage
        {
            private bool _initialized = false;
    
            private void OnUITestSave(object sender, UITestEventArgs e)
            {
                UITest uiTest = e.UITest;
                if (uiTest != null && uiTest.Maps != null)
                {
                    foreach (UIMap map in uiTest.Maps)
                    {
                        DoSomethingWithFile(map.FileName);
                    }
                }
            }
    
            public override void Dispose()
            {
                if (_initialized)
                {
                    _initialized = false;
                    UITest.Saving -= new EventHandler<UITestEventArgs>(OnUITestSave);
                }
            }
    
            public override object GetService(Type serviceType)
            {
                if (!_initialized)
                {
                    _initialized = true;
                    UITest.Saving += new EventHandler<UITestEventArgs>(OnUITestSave);
                }
    
                return null;
            }
    
            public override string PackageName
            {
                get { return "MyPluginPackage"; }
            }
    
            public override string PackageDescription
            {
                get { return "My description."; }
            }
    
            public override Version PackageVersion
            {
                get { return new Version(1, 0); }
            }
    
            public override string PackageVendor
            {
                get { return "My company"; }
            }
    
            public override Version VSVersion
            {
                get { return new Version(14, 0); }
            }
        }
    }


    • Edited by Waldinator Thursday, August 25, 2016 2:02 PM Inserted sample code
    Tuesday, August 23, 2016 7:38 AM

All replies

  • Hi Waldinator,

    I have found the same thread in stackoverflow:

    http://stackoverflow.com/questions/39080761/why-is-filename-null-in-the-uimap-saving-event-in-my-uitestextensionpackage

    According to the codes from the link, I have built it in my side, which occurred some errors. When I build the codes and “using Microsoft.VisualStudio.TestTools.UITest.Extension”, then I need to implement some methods. But “GetService(TypeserviceType)” does not exist in Microsoft.VisualStudio.TestTools.UITest.Extension. Which namespace you have used for class “UITestExtensionPackage”?

    If possible, could you please share a sample or complete code that could help me to repro your issue.

    Sincerely,

    Oscar


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Wednesday, August 24, 2016 7:44 AM
  • Hi Oscar and thanks for your reply.

    The code from StackOverflow is from me. Looks like I forgot some stuff in my code example. I updated it to a working version.

    Thanks in advance,

    Waldi.


    • Edited by Waldinator Wednesday, August 24, 2016 2:16 PM corrected grammar
    Wednesday, August 24, 2016 2:04 PM
  • Hi Waldinator,

    According to your code and based on Gautam Goenka’s blog from the following link. Please refer to following scenario to troubleshot your issue:

    1). I suggest you could use Trace.WriteLine() method to output the file name to observe whether the file name can be output or not.

    In my side, I use Trace.WriteLine() to output the map.FileName. And I checked the UITestLog.html that there were no any logs about map.FileName in it. In your side, you could through UITestLog to find whether the map.FileName has been output.

    2). We could debug the extension, you could add a breakpoint in DoSomethingWithFile(). Then to find whether the breakpoint could be hit. How to debug it please refer to the part of Debugging the extension from the following link.

    Reference:

    https://blogs.msdn.microsoft.com/gautamg/2010/01/05/2-hello-world-extension-for-coded-ui-test/

    Sincerely,

    Oscar


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.



    • Edited by Oscar_Wu Thursday, August 25, 2016 9:47 AM
    Thursday, August 25, 2016 9:46 AM
  • Hi Oscar,

    thanks for your reply, but I already troubleshooted the issue before I created this question. 

    I just checked it again and still: UIMap.FileName is always null.

    Look at this image:

    http://i.imgur.com/X4Bh7G8.png

    Was it also null when you were troubleshooting?

    Thanks in Advance for you aid,

    Waldi

    Thursday, August 25, 2016 2:01 PM
  • Hi Waldinator,

    After debugging your code, I found that Map.FileName is also null in my side. Then I call another API Map.ID, then it returned the file name that I want.

    Please refer to the following code I used:

                if (uiTest != null && uiTest.Maps != null)
                {
                    foreach (UIMap map in uiTest.Maps)
                    {
    
                        Trace.WriteLine(map.FileName+map.Id+"This is filename");                
                        Trace.WriteLine("=============================");
                    }
                }

    And refer to the log screenshot below:

    I think the MyMap string is I want, because in my coded UI project I have named the Map.uitest as MyMap.uitest. But when I call FileName, which return nothing. so I think we could ues "ID" property to get file name of map.uitest.

    Sincerely,

    Oscar


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.


    • Edited by Oscar_Wu Saturday, August 27, 2016 3:24 PM
    Saturday, August 27, 2016 3:22 PM
  • Thanks Oscar,

    good to know, that I am not the only one having this issue.

    I also considered using the Id, but this can't give me the information I seek. What I want to know is the path of the packages.config file, to check for a certain NuGetPackage to be installed. But without the file path to the UIMap, I think I have no way to determine the Project that the uimap is in. Do you know of any other solution for my problem?

    Thanks in Advance,

    Waldi.

    Monday, August 29, 2016 6:59 AM
  • Hi Waldinator,

    According to your description, you want to get the file path of package.config. So we could use C# API to get the path of our project. Because test projects belong to Class Library project. Maybe we could use C# API to get the path of the current project. I will try to use this method and I will update my result here as soon as possible. Thank you very much.

    Sincerely,

    Oscar


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Tuesday, August 30, 2016 5:30 AM
  • Hi Waldinator,

    I have tried the above idea, but it did not return the project path. So I will invoke other people to take a look at this thread.

    Sincerely,

    Oscar


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.


    • Edited by Oscar_Wu Thursday, September 1, 2016 9:36 AM
    Thursday, September 1, 2016 9:27 AM