注意
預覽功能不供生產時使用,而且可能功能受限。 這些功能是在正式發行前先行推出,讓客戶能夠搶先體驗並提供意見反應。
Microsoft Power Fx 是一種功能強大的低代碼語言 Power Apps,可以使用 C# 進行擴展以創建自定義測試函數。 本文介紹了如何創建 C# Power Fx test 函數,為製作者和開發人員提供無縫體驗。
測試引擎的 「無懸崖」可擴充性模型 Power Apps 確保使用者可以在不遇到任何障礙的情況下擴展測試引擎的功能 Power Apps 。 該模型允許開發人員使用 C# 建立自定義函數,可以將其集成到 Power Fx 中以處理複雜的場景。
測試引擎模組
測試引擎內部的 測試引擎模組 Power Fx 是使用可擴充性模型構建的。 您可以使用產品代碼作為如何擴展測試引擎的範例。
同意對話框示例
下面是一個函數範例 Power Fx ,該函數提供用於處理畫布應用程式 中的條件同意對話框的代碼大綱。
同意對話框 是向用戶顯示的提示,請求他們訪問某些資源或執行特定作的許可權。 此對話對於維護安全性並確保用戶瞭解並同意代表他們採取的作至關重要。
同意對話框很重要,因為它有助於防止未經授權的訪問和作。 它確保在執行任何敏感作之前通知使用者並提供明確同意。 這在應用程式需要訪問用戶數據或執行作並且這種條件行為可能會影響自動化測試的情況下非常重要。
測試中同意對話框的挑戰
同意對話的挑戰之一是它們可能使測試不確定。 提示可以根據各種因素 (例如用戶許可權或以前的交互) 有條件地出現。 這種條件外觀可能會使測試過程複雜化,因為測試引擎需要適當地處理這些對話框。
抽象複雜性 Power Fx
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、和ITestStateILogger。 -
執行方法:記錄執行並調用該
ExecuteAsync方法來處理同意對話方塊。 - ExecuteAsync 方法:檢索頁面上下文並處理超時的同意對話框。