简体   繁体   English

如何在运行Coded UI时解析当前目录

[英]How to resolve Current Directory when running Coded UI

I am running a CodedUI test that references a DLL and that DLL refrences a sort of "config" file. 我正在运行一个引用DLL的CodedUI测试,该DLL引用了一种“配置”文件。 While running the test the current Directory returns the one where CodedUI puts the test results files I've used 在运行测试时,当前Directory返回CodedUI放置我使用过的测试结果文件的目录

AppDomain.CurrentDomain.BaseDirectory

and

System.Reflection.Assembly.GetExecutingAssembly().CodeBase

and

System.Reflection.Assembly.GetExecutingAssembly().Location

These all give me the same path 这些都给了我相同的道路

What I need is to get the path where the DLL resides because that is where the config file will be built. 我需要的是获取DLL所在的路径,因为这是构建配置文件的位置。

The location where this will be changes if I am debugging or if I am just running the test (obviously) so I can't use that and navigate backwards or anything like that. 如果我正在调试或者我刚刚运行测试(显然),这将改变的位置,所以我不能使用它并向后导航或类似的东西。

Are there any other ways to get the location of the DLL you are referencing?? 有没有其他方法来获取您引用的DLL的位置?

Edit: 编辑:

I am referencing this config file from inside the DLL that i am referencing. 我从我引用的DLL内部引用此配置文件。

So far the only place I have found the original path to the test dll is in a private variable in the test context. 到目前为止,我找到测试dll的原始路径的唯一地方是测试上下文中的私有变量。 I ended up using reflection to get the value out and make it usable. 我最终使用反射来获取值并使其可用。

    using System.Reflection;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    public static string CodeBase(
        TestContext testContext)
    {
        System.Type t = testContext.GetType();
        FieldInfo field = t.GetField("m_test", BindingFlags.NonPublic | BindingFlags.Instance);
        object fieldValue = field.GetValue(testContext);
        t = fieldValue.GetType();
        PropertyInfo property = fieldValue.GetType().GetProperty("CodeBase");
        return (string)property.GetValue(fieldValue, null);
    }

I used this to get the path to the DLL that is getting run and use that to then run the application that I know is compiled to the same location as the test was at. 我使用它来获取正在运行的DLL的路径,然后使用它来运行我知道编译到与测试所在位置相同的位置的应用程序。

If anyone finds a better way of getting this, please let me know as well. 如果有人找到更好的方法,请告诉我。

The best way to get the directory of where a given DLL was loaded from is to use the following on a type that is defined in that assembly. 获取加载给定DLL的目录的最佳方法是在该程序集中定义的类型上使用以下内容。

var type = typeof(TypeInThatAssembly);
var path = Path.GetDirectory(type.Location);

The CodeBase and Location property often return the same information but are very different CodeBaseLocation属性通常返回相同的信息,但是非常不同

  • CodeBase: This contains the location for the assembly as it was referenced during load CodeBase:它包含在加载期间引用的程序集的位置
  • Location: This is where the assembly was actually loaded from on disk 位置:这是从磁盘上实际加载程序集的位置

These can differ in application which use shadow copied assemblies (Asp.Net, xUnit, etc ...) 这些可以在使用阴影复制程序集(Asp.Net,xUnit等)的应用程序中有所不同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行编码的UI测试时控制台未出现 - Console not appearing when running Coded UI test 在针对已编码的ui的调试中运行代码时,如何获取文件的相对路径 - How do i get a relative path of a file when running my code in debug for coded ui 编码的UI测试一起运行时无法正常运行 - Coded UI Tests not running properly when run together 在编码的UI测试中更改当前浏览器的麻烦 - Troubles changing current browser in Coded UI Tests 编码的用户界面-当新的IE窗口打开时,如何吸引焦点? - Coded UI - How to bring in focus when a new IE window opens? 使用MStest运行编码的UI测试 - Running Coded UI Test Using MStest 如何使用编码的UI或MSAA获得对在模拟器中运行的Windows CE应用程序控件的编程访问权限? - How to get programmatic access to controls of Windows CE app running in Emulator using Coded UI or MSAA? 我如何在我要测试的本地项目中同时运行已运行的编码UI测试 - How can i run a Coded UI Test running at the same time the project in localhost that i want to test 当隐藏的控件在编码的UI测试中显示在UI中时,如何使它们成为记录隐藏控件? - How can I make record hidden controls when they become visible in UI in coded UI tests? 当IIS在服务帐户下运行时,如何在ASP.NET中获取当前的Active Directory用户 - How to get current Active Directory user in ASP.NET when IIS is running under a service account
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM