- 入门指南
- 设置和配置
- 自动化项目
- 依赖项
- 工作流类型
- 控制流程
- 文件比较
- 自动化最佳实践
- 源代码控件集成
- 调试
- 日志记录
- 诊断工具
- 工作流分析器
- 变量
- 参数
- 导入的命名空间
- 编码自动化
- 简介
- 注册自定义服务
- “前”和“后”上下文
- 正在生成代码
- 根据手动测试用例生成编码测试用例
- 故障排除
- 基于触发器的 Attended 自动化
- 对象存储库
- ScreenScrapeJavaSupport 工具
- 扩展程序
- Studio 测试
- 故障排除

Studio 用户指南
“前”和“后”上下文
在测试用例中,“之前执行”和“之后执行”允许您在运行测试用例之前和之后执行某些操作。这些上下文通常用于设置和关闭资源、执行日志记录以及管理测试环境。
BeforeRunContext(设置):“前”执行用于在主要执行测试用例之前执行操作。它通常用于设置测试环境的初始状态和准备资源。“前”上下文的一些常见用例包括:- 正在初始化测试所需的变量或数据。
- 设置与外部系统或数据库的连接。
- 记录测试执行的开始。
- 打开与测试交互的应用程序或网页。
- 在运行测试用例之前设置特定的异常。
AfterRunContext(拆卸):“后”执行用于在测试用例的主要执行完成后执行操作。它通常用于清理资源、完成操作以及记录测试结果。“后”上下文的常见用例包括:- 关闭测试期间使用的应用程序或网页。
- 释放资源,例如数据库连接或网络资源。
- 捕获并打印在“前”上下文和主执行中记录的异常。
在编码自动化中,您可以选择为要在运行之前和之后执行的操作创建自定义界面。这可以通过在主类旁边声明 IBeforeAfterRun 接口,然后为这些操作实现您自己的自定义逻辑来实现。
BeforeRunContext
探索 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
探索 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; }
}
此上下文表示执行自动化之后的状态。其中包括有关已执行工作流的相对路径以及执行之前和执行期间捕获的任何异常的详细信息。
异常处理
Before 和主执行并发运行,以捕获执行期间出现的异常。在这些阶段引发的任何异常都将捕获并存储在 AfterRunContext 中,如果没有发生异常,则将其保留为 null。
在 Before 和主执行运行后,将执行 After 方法。如果在 After 运行期间引发异常,则执行将结束,并引发相应的异常。
如果 Before 和 After 运行中都发生异常,则会将它们捆绑到 AggregateException 中。
在执行之前和之后实现
“前”和“后”执行可用于定义编码测试用例之前和之后的操作,从而增强自动化。您可以在编码测试用例中创建此自定义实施,或将其作为单独的部分类创建。 在本教程中,了解如何在编码测试用例中集成设置、拆卸和日志记录。
在编码测试用例中实现
您可以直接在编码测试用例中实现“前”执行和“后”执行接口。
- 在编码测试用例
所在的公共类中,添加
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();
}
```
3. 根据您的需要修改实施。 在提供的示例中,上下文在执行之前记录消息,并在执行之后检查异常。 ``` 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 Before(BeforeRunContext context)
{
Log("Execution started for " + context.RelativeFilePath);
}
```
您现在可以使用此自定义部分类,仅对继承该类的文件使用相同的.cs“之前和之后执行”界面。 2. 创建编码自动化。 对于此示例,创建一个名为 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"); }