创建 C# Power Fx test 函数(预览版)

备注

预览功能不适合生产使用且功能可能受限。 这些功能在正式发布之前已经可用,以便客户可以抢先体验并提供反馈。

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 该参数允许同意对话框等待过程退出,并且带有所提供文本的标签是可见的。

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

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

// 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
  • 构造函数:使用必要的依赖项初始化函数,包括 ITestInfraFunctions、和 ITestState ILogger
  • 执行方法:记录执行并调用该 ExecuteAsync 方法来处理同意对话框。
  • ExecuteAsync 方法:检索页面上下文并处理超时的同意对话框。