UiPath Documentation
studio
2024.10
false
Studio 用户指南
重要 :
新发布内容的本地化可能需要 1-2 周的时间才能完成。

在编码自动化中使用导入的库项目

在 Studio 的编码自动化中使用导入库项目的对象存储库元素和工作流。

本教程将向您介绍如何在编码自动化中使用导入库项目中的对象。该示例演示了如何使用导入的库项目中的对象存储库元素和工作流。给定的场景涉及自动化 Acme 网站,会填写学生记分卡并将其添加到数据库中。

先决条件​

  • 安装“用户界面自动化活动”版本 23.10.13 或更高版本。

    备注:

    我们建议您使用与您正在处理的主版本相对应的最新次要版本。 例如,如果您正在使用活动包的 23.10 主要版本,我们建议您将其升级到可用的最新次要版本(23.10.x)。

  • 发布 Acme 库示例,并将其安装在您正在进行的项目中。

1. 创建库项目​

创建一个库项目以存储自动化 Acme 应用所必需的用户界面元素,特别是用于填写学生记分卡的元素。此外,在库项目中创建低代码 (.xaml) 或编码工作流 (.cs),以便稍后在另一个编码自动化中使用。

  1. 创建一个名为 AcmeLibrary 的库项目。
  2. 捕获要在“对象存储库”和“描述符”部分中使用的所有用户界面元素。
  3. 从“文件”组创建新的编码工作流。 此步骤对于在另一个项目中导入库时,在编码自动化中启用对对象存储库的访问至关重要。
  4. 将主要的低代码工作流重命名为 Send message box,并添加具有以下文本的“消息框”活动:“您已为 Acme 中的学生添加了新的计分卡。”
备注: 您也可以按照本教程中概述的步骤将“消息框”活动重新创建为编码工作流。 :::
  1. 将库项目保存并发布到订阅源。 您将在编码自动化中的另一个项目中安装和使用此已发布库。

结果​

库项目已发布到订阅源,可以在其他项目中安装。

2. 创建编码自动化​

创建库项目后,将其安装到另一个项目中以利用其可重用资产,包括对象存储库元素和工作流,低代码 (.xaml) 和编码 (.cs) 均可。

提示:

使用 UI Automation 编码的自动化 API 时,请记住,带有 IScreenDescriptor 或 IElementDescriptor 的重载旨在与对象存储库元素一起使用,而带有 TargetAnchorableModel 的重载旨在与通过 UI Explorer 获取的原生选取器一起使用。请访问“用户界面自动化的编码自动化 API”,了解如何通过用户界面自动化的编码自动化 API 使用选取器。

  1. 在“文件”组中,新建编码工作流。

  2. 转到“管理包”,然后安装您之前创建的库项目。

  3. 在“文件”组中,新建工作流。

    1. 将工作流命名为 notifyStudentScoreCardAdded。
    2. 导航到“活动”面板,然后进入“已安装”部分。
    3. 搜索您导入的库项目的名称(在此示例中为 AcmeLibrary)。
    4. 将 Send message box 活动拖放到 notifyStudentScoreCardAdded。现在,notifyStudentScoreCardAdded 具有与导入的库项目中的 Send message box 工作流相同的行为。
  4. 为了增强可读性,您可以在工作流开头应用 using 语句,将导入的对象存储库应用程序定义为变量。示例如下:using app = <ProjectName>.ObjectRepository.Descriptors。

使用此方法,您可以轻松地按以下格式调用描述符:app.<AppName>.<ScreenName>.<UiElementName>

在此示例中,我们定义了 Acme 对象存储库,如下所示:

namespace UsingImportedLibraryProjects
{
    using AcmeApp = AcmeLibrary.ObjectRepository.Descriptors;
namespace UsingImportedLibraryProjects
{
    using AcmeApp = AcmeLibrary.ObjectRepository.Descriptors;
  1. 现在,您可以在 Acme 中对给定场景进行自动化。首先使用 Open 编码自动化 API 打开 Acme 登录屏幕。

    [Workflow]
            public void Execute()
            {
              // 1.Open the Acme app on the Login screen
              var AcmeLoginScreen = uiAutomation.Open(AcmeApp.Acme.LoginScreen);
    [Workflow]
            public void Execute()
            {
              // 1.Open the Acme app on the Login screen
              var AcmeLoginScreen = uiAutomation.Open(AcmeApp.Acme.LoginScreen);
    
  2. 使用 TypeInto 编码自动化 API 键入登录 Acme 所需的凭据。

    // 2.Type in the necessary credentials
             AcmeLoginScreen.TypeInto(AcmeApp.Acme.LoginScreen.Email, "john.doe2023@uipath.com");
             AcmeLoginScreen.TypeInto(AcmeApp.Acme.LoginScreen.Password, "12345678");
    // 2.Type in the necessary credentials
             AcmeLoginScreen.TypeInto(AcmeApp.Acme.LoginScreen.Email, "john.doe2023@uipath.com");
             AcmeLoginScreen.TypeInto(AcmeApp.Acme.LoginScreen.Password, "12345678");
    
  3. 使用Select编码自动化 API 和原生选取器来选择“登录”按钮。

    // 3.Use a native selector to click the Login button
    AcmeLoginScreen.Click(Target.FromSelector("<webctrl tag='BUTTON' type='submit'/>"));
    // 3.Use a native selector to click the Login button
    AcmeLoginScreen.Click(Target.FromSelector("<webctrl tag='BUTTON' type='submit'/>"));
    
  4. 登录流程将转到 Acme Web 应用程序中的“仪表板”屏幕。因此,您可以使用 Attach 编码自动化 API,专注于新打开的屏幕。

    // 4. Focus on the Dashboard screen
    var DashboardScreen = uiAutomation.Attach(AcmeApp.Acme.Dashboard);
    // 4. Focus on the Dashboard screen
    var DashboardScreen = uiAutomation.Attach(AcmeApp.Acme.Dashboard);
    
  5. 使用选择编码自动化 API 在网站上选择“学生”条目。

    // 5. Click the Students entry
    DashboardScreen.Click(AcmeApp.Acme.Dashboard.Students);
    // 5. Click the Students entry
    DashboardScreen.Click(AcmeApp.Acme.Dashboard.Students);
    
  6. 选择分数卡以使用Select编码自动化 API 打开“学生 - 分数卡”表单。

    // 6. Click Score card, to start filling in a student score card
    DashboardScreen.Click(AcmeApp.Acme.Dashboard.Students.ScoreCard);
    // 6. Click Score card, to start filling in a student score card
    DashboardScreen.Click(AcmeApp.Acme.Dashboard.Students.ScoreCard);
    
  7. 使用 Attach 编码自动化 API,专注于“学生 - 分数卡”屏幕。

    // 7. Focus on the Score Card screen where you fill in the necessary information
    var ScoreCardScreen = uiAutomation.Attach(AcmeApp.Acme.StudentsScoreCard);
    // 7. Focus on the Score Card screen where you fill in the necessary information
    var ScoreCardScreen = uiAutomation.Attach(AcmeApp.Acme.StudentsScoreCard);
    
  8. 使用 TypeInto 编码自动化 API 和对象存储库中的元素填写“学生 - 分数卡”表单中的一半字段。

    ScoreCardScreen.TypeInto(AcmeApp.StudentsScoreCard.StudentEmail, "john.doe@uipath.com");
    ScoreCardScreen.TypeInto(AcmeApp.Acme.StudentsScoreCard.FirstName, "John");
    ScoreCardScreen.TypeInto(AcmeApp.Acme.StudentsScoreCard.LastName, "Doe");
    ScoreCardScreen.TypeInto(AcmeApp.Acme.StudentsScoreCard.ParentEmail, "johnny.doe@uipath.com");
    ScoreCardScreen.TypeInto(AcmeApp.Acme.StudentsScoreCard.Tuition, "Private");
    ScoreCardScreen.TypeInto(AcmeApp.StudentsScoreCard.StudentEmail, "john.doe@uipath.com");
    ScoreCardScreen.TypeInto(AcmeApp.Acme.StudentsScoreCard.FirstName, "John");
    ScoreCardScreen.TypeInto(AcmeApp.Acme.StudentsScoreCard.LastName, "Doe");
    ScoreCardScreen.TypeInto(AcmeApp.Acme.StudentsScoreCard.ParentEmail, "johnny.doe@uipath.com");
    ScoreCardScreen.TypeInto(AcmeApp.Acme.StudentsScoreCard.Tuition, "Private");
    
  9. 将 TypeInto 编码自动化 API 与通过 UI Explorer 发现的原生选取器配合使用,填写表单中的其余字段。

    ScoreCardScreen.TypeInto(Target.FromSelector("<webctrl id='mathematics' tag='INPUT' />"), "A");
    ScoreCardScreen.TypeInto(Target.FromSelector("<webctrl id='physics' tag='INPUT' />"), "B");
    ScoreCardScreen.TypeInto(Target.FromSelector("<webctrl id='biology' tag='INPUT' />"), "C");
    ScoreCardScreen.TypeInto(Target.FromSelector("<webctrl id='chemistry' tag='INPUT' />"), "A");
    ScoreCardScreen.TypeInto(Target.FromSelector("<webctrl id='geography' tag='INPUT' />"), "C");
    ScoreCardScreen.TypeInto(Target.FromSelector("<webctrl id='history' tag='INPUT' />"), "A");
    ScoreCardScreen.TypeInto(Target.FromSelector("<webctrl id='english' tag='INPUT' />"), "A");
    ScoreCardScreen.TypeInto(Target.FromSelector("<webctrl id='computer Science' tag='INPUT' />"), "C");
    ScoreCardScreen.TypeInto(Target.FromSelector("<webctrl id='professorComments' tag='INPUT' />"), "The student is doing okay, very hardworking and does its best");
    ScoreCardScreen.TypeInto(Target.FromSelector("<webctrl id='mathematics' tag='INPUT' />"), "A");
    ScoreCardScreen.TypeInto(Target.FromSelector("<webctrl id='physics' tag='INPUT' />"), "B");
    ScoreCardScreen.TypeInto(Target.FromSelector("<webctrl id='biology' tag='INPUT' />"), "C");
    ScoreCardScreen.TypeInto(Target.FromSelector("<webctrl id='chemistry' tag='INPUT' />"), "A");
    ScoreCardScreen.TypeInto(Target.FromSelector("<webctrl id='geography' tag='INPUT' />"), "C");
    ScoreCardScreen.TypeInto(Target.FromSelector("<webctrl id='history' tag='INPUT' />"), "A");
    ScoreCardScreen.TypeInto(Target.FromSelector("<webctrl id='english' tag='INPUT' />"), "A");
    ScoreCardScreen.TypeInto(Target.FromSelector("<webctrl id='computer Science' tag='INPUT' />"), "C");
    ScoreCardScreen.TypeInto(Target.FromSelector("<webctrl id='professorComments' tag='INPUT' />"), "The student is doing okay, very hardworking and does its best");
    
  10. 使用选择编码自动化 API,选择“添加分数卡详细信息” 。

    // 10. Click the "Add score card details" button using an Object Repository descriptor
    ScoreCardScreen.Click(AcmeApp.Acme.StudentsScoreCard.AddScoreCardDetailsButton);
    // 10. Click the "Add score card details" button using an Object Repository descriptor
    ScoreCardScreen.Click(AcmeApp.Acme.StudentsScoreCard.AddScoreCardDetailsButton);
    
  11. 使用 RunWorkflow() 方法调用从库项目导入的工作流。为方法提供项目内工作流的路径。

    // 11. Invoke imported workflow from library
    RunWorkflow("notifyStudentScoreCardAdded.xaml");
    // 11. Invoke imported workflow from library
    RunWorkflow("notifyStudentScoreCardAdded.xaml");
    
  12. 关闭您打开的浏览器标签页,以便使用 Dispose() 编码自动化 API 对该场景进行自动化。

    // 12. Close the applications/browsers you opened, to finish he automation
    ScoreCardScreen.Dispose();
    // 12. Close the applications/browsers you opened, to finish he automation
    ScoreCardScreen.Dispose();
    

结果​

编码自动化已成功运行。系统会在 Acme 应用程序中填写学生记分卡,并显示消息通知。自动化完成后,浏览器选项卡将关闭。

示例项目​

要按照步骤操作并自行尝试本教程,请参阅以下示例项目:在编码自动化中使用导入的库项目。该文件还包含库项目。

  • 先决条件​
  • 1. 创建库项目​
  • 结果​
  • 2. 创建编码自动化​
  • 结果​
  • 示例项目​

此页面有帮助吗?

连接

需要帮助? 支持

想要了解详细内容? UiPath Academy

有问题? UiPath 论坛

保持更新