创建 C# Power Fx 测试函数(已弃用)

注释

测试引擎已弃用 ,将在将来的版本中删除。 使用 Power Platform Playwright 示例在 Power Platform 和 Dynamics 365 服务中测试自动化功能。

Microsoft Power Fx是Power Apps中使用的功能强大的低代码语言,可以使用 C# 扩展以创建自定义测试函数。 本文介绍了如何创建 C# Power Fx test 函数,为制作者和开发人员提供无缝体验。

Power Apps 测试引擎的“无障碍”可扩展性模型确保用户可以不遇到任何阻碍地扩展 Power Apps 测试引擎的功能。 该模型允许开发人员使用 C# 创建自定义函数,可以将其集成到 Power Fx 中以处理复杂的场景。

测试引擎模块

测试引擎中的 Power Fx 测试引擎模块 是使用可扩展性模型构建的。 您可以使用产品代码作为如何扩展测试引擎的示例。

下面是一个 Power Fx 函数示例,它提供了处理画布应用程序中条件同意对话框的代码框架。

同意对话框 是向用户显示的提示,请求他们访问某些资源或执行特定作的权限。 此对话对于维护安全性并确保用户了解并同意代表他们采取的作至关重要。

示例:应用程序连接到 SharePoint 站点的连接许可对话框。

同意对话框很重要,因为它有助于防止未经授权的访问和作。 它确保在执行任何敏感操作之前通知用户并获取他们的明确同意。 这在应用程序需要访问用户数据或执行作并且这种条件行为可能会影响自动化测试的情况下非常重要。

同意对话的挑战之一是它们可能使测试不确定。 提示可以根据各种因素(例如用户权限或以前的交互)有条件地出现。 这种条件外观可能会使测试过程复杂化,因为测试引擎需要适当地处理这些对话框。

使用 Power Fx 抽象复杂性

Power Fx 有助于抽象有条件地等待同意对话框并在需要时创建连接的复杂性。 开发者可以使用 Power Fx 来以更直接、更直观的方式定义处理同意对话框的逻辑。

下面是用于 Power Fx 处理自定义页面中的同意对话框的示例:

Preview.ConsentDialog(Table({Text: "Center of Excellence Setup Wizard"}))

在此示例中,该 ConsentDialog 函数检查同意对话框是否可见。 如果是,则该函数可以响应确认同意测试帐户的对话框。 处理对话后,将执行剩余的测试步骤。

Table 该参数允许同意对话框的等待过程在带有所提供文本的标签可见时退出。

使用 C# 扩展 Power Fx 测试函数

以下示例是一个示例大纲代码,可用作完成此示例的起点:

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using Microsoft.Extensions.Logging;
using Microsoft.Playwright;
using Microsoft.PowerApps.TestEngine.Config;
using Microsoft.PowerApps.TestEngine.TestInfra;
using Microsoft.PowerFx;
using Microsoft.PowerFx.Core.Utils;
using Microsoft.PowerFx.Types;

namespace testengine.module
{
    /// <summary>
    /// This will check the custom pages of a model driven app looking for a consent dialog
    /// </summary>
    public class ConsentDialogFunction : ReflectionFunction
    {
        private readonly ITestInfraFunctions _testInfraFunctions;
        private readonly ITestState _testState;
        private readonly ILogger _logger;
        private static TableType SearchType = TableType.Empty()
              .Add(new NamedFormulaType("Text", FormulaType.String, displayName: "Text"));
    
        /// <summary>
        /// Constructor: Initializes the function with necessary dependencies, 
        /// including ITestInfraFunctions, ITestState, and ILogger.
        /// </summary>
        /// <param name="testInfraFunctions">The test infrastructure functions.</param>
        /// <param name="testState">The test state.</param>
        /// <param name="logger">The logger instance.</param>
        public ConsentDialogFunction(ITestInfraFunctions testInfraFunctions, 
           ITestState testState, 
           ILogger logger) : base(DPath.Root.Append(
               new DName("Preview")), 
               "ConsentDialog", 
               FormulaType.Blank, 
               SearchType)
               {
                  _testInfraFunctions = testInfraFunctions;
                  _testState = testState;
                  _logger = logger;
               }

        /// <summary>
        /// Execute Method: Logs the execution and calls the ExecuteAsync 
        /// method to handle the consent dialog.
        /// </summary>
        /// <param name="searchFor">The table value to search for.</param>
        /// <returns>A blank value.</returns>
        public BlankValue Execute(TableValue searchFor)
        {
            _logger.LogInformation("------------------------------\n\n" +
                "Executing ConsentDialog function.");

            ExecuteAsync(searchFor).Wait();

            return FormulaValue.NewBlank();
        }

        /// <summary>
        /// ExecuteAsync Method: Retrieves the page context and handles the consent dialog with a timeout.
        /// </summary>
        /// <param name="searchFor">The table value to search for.</param>
        /// <returns>A task representing the asynchronous operation.</returns>
        private async Task ExecuteAsync(TableValue searchFor)
        {
            var page = _testInfraFunctions
               .GetContext()
               .Pages
               .Where(p => p.Url.Contains("main.aspx"))
               .First();

            // ... IPage to handle consent dialog with timeout
        }
    }
}

ConsentDialogFunction 示例说明

  • 命名空间和导入:导入必要的命名空间并定义 testengine.module 命名空间。
  • 类定义:该 ConsentDialogFunction 类继承自 ReflectionFunction 并定义自定义函数 ConsentDialog
  • 构造函数:使用必要的依赖项初始化函数,包括 ITestInfraFunctionsITestStateILogger
  • 执行方法:记录执行并调用该 ExecuteAsync 方法来处理同意对话框。
  • ExecuteAsync 方法:检索页面上下文,并处理同意对话框,设定超时时间。