studio
2025.10
false
- 发行说明
 - 入门指南
 - 设置和配置
 - 自动化项目
 - 依赖项
 - 工作流类型
 - 控制流程
 - 文件比较
 - 自动化最佳实践
 - 源代码控件集成
 - 调试
 - 日志记录
 - 诊断工具
 - 工作流分析器
 - 变量
 - 参数
 - 导入的命名空间
 - 编码自动化
 - 基于触发器的 Attended 自动化
 - 对象存储库
 - ScreenScrapeJavaSupport 工具
 - 扩展程序
 - Studio 测试
 - 故障排除
 
重要 :
新发布内容的本地化可能需要 1-2 周的时间才能完成。

Studio 用户指南
上次更新日期 2025年10月30日
在测试用例中,“之前执行”和“之后执行”允许您在运行测试用例之前和之后执行某些操作。这些上下文通常用于设置和关闭资源、执行日志记录以及管理测试环境。
               
            
            
            BeforeRunContext(设置):“前”执行用于在主要执行测试用例之前执行操作。它通常用于设置测试环境的初始状态和准备资源。“前”上下文的一些常见用例包括:- 正在初始化测试所需的变量或数据。
 - 设置与外部系统或数据库的连接。
 - 记录测试执行的开始。
 - 打开与测试交互的应用程序或网页。
 - 在运行测试用例之前设置特定的异常。
 
AfterRunContext(拆卸):“后”执行用于在测试用例的主要执行完成后执行操作。它通常用于清理资源、完成操作以及记录测试结果。“后”上下文的常见用例包括:- 关闭测试期间使用的应用程序或网页。
 - 释放资源,例如数据库连接或网络资源。
 - 捕获并打印在“前”上下文和主执行中记录的异常。
 
在编码自动化中,您可以选择为要在运行之前和之后执行的操作创建自定义界面。这可以通过在主类旁边声明 
            
            
            IBeforeAfterRun 接口,然后为这些操作实现您自己的自定义逻辑来实现。
            探索 
               
               
               BeforeRunContext 的定义时,
您可以找到以下
代码片段:// Context of the run before the execution.
public class BeforeRunContext
{
    // The relative path of the workflow.
    public string RelativeFilePath { get; set; }
}// Context of the run before the execution.
public class BeforeRunContext
{
    // The relative path of the workflow.
    public string RelativeFilePath { get; set; }
}此上下文表示执行自动化之前的状态。它包含有关正在执行的工作流的相对路径的信息。您可以根据用例在此处设置异常。
探索 
               
               
               AfterRunContext 的定义时,
您可以找到以下
代码片段:// Context of the run after the execution.
public class AfterRunContext
{
    // The relative path of the workflow.
    public string RelativeFilePath { get; set; }
    // The exception caught in execution if any.
    public Exception Exception { get; set; }
}// Context of the run after the execution.
public class AfterRunContext
{
    // The relative path of the workflow.
    public string RelativeFilePath { get; set; }
    // The exception caught in execution if any.
    public Exception Exception { get; set; }
}此上下文表示执行自动化之后的状态。其中包括有关已执行工作流的相对路径以及执行之前和执行期间捕获的任何异常的详细信息。
“前”和“后”执行可用于定义编码测试用例之前和之后的操作,从而增强自动化。您可以在编码测试用例中创建此自定义实现,也可以将其创建为单独的部分类。在本教程中,了解如何在编码测试用例中集成设置、拆卸和日志记录。
               
               
               
               在编码测试用例中实现
您可以直接在编码测试用例中实现“前”执行和“后”执行接口。
- 在编码测试用例
所在的公共类中,添加 
IBeforeAfterRun接口。此问题将以红色突出显示,表示可能需要 修复。public class MyTest : CodedWorkflow, IBeforeAfterRunpublic class MyTest : CodedWorkflow, IBeforeAfterRun - 选择“显示潜在的修复程序”或按 
Ctrl + .,然后选择“实现接口”。这将生成一个默认界面,如下 所示:public void After(AfterRunContext context) { throw new NotImplementedException(); } public void Before(BeforeRunContext context) { throw new NotImplementedException(); }public void After(AfterRunContext context) { throw new NotImplementedException(); } public void Before(BeforeRunContext context) { throw new NotImplementedException(); } - 根据需要修改实现。 
                        在提供的代码示例中,上下文在执行之前 记录一条消息,并在执行之后检查是否存在 异常。
public void After(AfterRunContext context) { if (context.Exception != null) { Log(context.Exception.Message); } } public void Before(BeforeRunContext context) { Log("this is before"); }public void After(AfterRunContext context) { if (context.Exception != null) { Log(context.Exception.Message); } } public void Before(BeforeRunContext context) { Log("this is before"); } 
使用自定义部分类实现
您可以使用自定义部分类,仅为项目中的某些 
                  
                  
                  .cs 文件实现“前”和“后”执行接口。
                  - 创建代码源文件。您可在此处实现“前”和“后”执行接口。在此示例中,将文件命名为 
TestCaseBase。- 在公共类旁边添加 
IBeforeAfterRun接口。此问题将以红色突出显示,表示可能需要修复。注意:自定义分部类(在此示例中为TestCaseBase)必须继承CodedWorkflow类。这可使继承自定义部分类的其他cs文件按预期运行。public class TestCaseBase : CodedWorkflow, IBeforeAfterRunpublic class TestCaseBase : CodedWorkflow, IBeforeAfterRun - 选择“显示潜在的修复程序”或按 
Ctrl + .,然后选择“实现接口”。这将生成一个默认界面,如下 所示:public void After(AfterRunContext context) { throw new NotImplementedException(); } public void Before(BeforeRunContext context) { throw new NotImplementedException(); }public void After(AfterRunContext context) { throw new NotImplementedException(); } public void Before(BeforeRunContext context) { throw new NotImplementedException(); } - 根据需要修改实现。
                              
在提供的示例中,上下文在执行之前记录一条消息,并在执行之后检查是否存在异常。
public void After(AfterRunContext context) { if (context.Exception != null) { throw context.Exception; } else { Log("Test " + context.RelativeFilePath + " finished with no exception."); } } public void Before(BeforeRunContext context) { Log("Execution started for " + context.RelativeFilePath); }public void After(AfterRunContext context) { if (context.Exception != null) { throw context.Exception; } else { Log("Test " + context.RelativeFilePath + " finished with no exception."); } } public void Before(BeforeRunContext context) { Log("Execution started for " + context.RelativeFilePath); } 
现在,您可以使用此自定义部分类,仅对继承该类的.cs文件使用相同的“前”和“后”执行接口。 - 在公共类旁边添加 
 - 创建编码自动化。对于此示例,创建一个名为 
TestCase的编码测试用例。要在自定义部分类中使用相同的“执行前”和“执行后”界面,请确保编码测试用例继承自此部分类。public class TestCase : TestCaseBase { [TestCase] public void Execute() { Log("Executing the test..."); }public class TestCase : TestCaseBase { [TestCase] public void Execute() { Log("Executing the test..."); } 
要演示此特定实现的工作方式,请查看下方的输出
日志:
                  
                  
               Execution started for file: TestCase.cs
 [Info] IBeforeAfterRunExamples execution started
 [Info] Execution started for TestCase.cs
 [Info] Executing the test...
 [Info] Test TestCase.cs finished with no exception.
 [Info] IBeforeAfterRunExamples execution ended in: 00:00:00 Execution started for file: TestCase.cs
 [Info] IBeforeAfterRunExamples execution started
 [Info] Execution started for TestCase.cs
 [Info] Executing the test...
 [Info] Test TestCase.cs finished with no exception.
 [Info] IBeforeAfterRunExamples execution ended in: 00:00:00使用 CodedWorkflow 部分类实现
                  
                  
                  
                  您可以使用项目中任何其他编码测试用例或编码工作流可继承的 
                  
                  
                  CodedWorkflow 部分类来实现“前”和“后”上下文接口。此实现适用于项目中的所有编码测试用例和编码工作流。
                  - 创建一个代码源文件,并将其命名为不同于 
CodedWorkflow.cs的名称,否则部分类将与现有的CodedWorkflow只读类冲突。 - 构造一个为“前”和“后”上下文实现自定义接口的部分类。
 - 将类重命名为 
CodedWorkflow。否则,您将无法使用可用服务。 - 确保已编码源文件中的类继承 
CodedWorkflowBase类。 - 在代码源文件的开头添加 
UiPath.CodedWorkflows命名空间 。using System; using System.Collections.Generic; using UiPath.CodedWorkflows;using System; using System.Collections.Generic; using UiPath.CodedWorkflows; - 在代码源文件的类中
,包含
 
IBeforeAfterRun。namespace TestAutomationProject1 { public partial class CodedWorkflow : CodedWorkflowBase, IBeforeAfterRunnamespace TestAutomationProject1 { public partial class CodedWorkflow : CodedWorkflowBase, IBeforeAfterRun - 将鼠标悬停在 
IBeforeAfterRun上,然后选择“显示可能的修复”,或选择它并按Ctrl + .。 - 选择“实现接口”。
                        这将在下面创建默认 实现:
public void After(AfterRunContext context) { throw new NotImplementedException(); } public void Before(BeforeRunContext context) { throw new NotImplementedException(); }public void After(AfterRunContext context) { throw new NotImplementedException(); } public void Before(BeforeRunContext context) { throw new NotImplementedException(); } - 或者,您可以根据用例进一步自定义界面。在下面的代码示例中,上下文在执行之前打印出一条消息,在执行之后,它会检查是否引发了任何异常并再次打印输出。
public void After(AfterRunContext context) { if (context.Exception != null) { Log(context.Exception.Message); } } public void Before(BeforeRunContext context) { Log("this is before"); }public void After(AfterRunContext context) { if (context.Exception != null) { Log(context.Exception.Message); } } public void Before(BeforeRunContext context) { Log("this is before"); }