In this module, we will explore how to use simulated connectors in your Power Apps tests. Simulated connectors allow you to mock network requests, providing predefined responses for testing purposes. This is particularly useful for testing scenarios where you want to control the data returned by external services.
Mocking is a technique used in software testing to simulate the behavior of real objects. By using mocks, you can create controlled environments where you can test specific parts of your application in isolation. This helps ensure that your tests are reliable and repeatable, as they are not dependent on external factors.
Lets look at an example of simulating a connection. The first set fo checks validate the expected Weather from the WeatherService. After using the SimulateConnector function The alternative Location and Condition values are used.
NOTES:
- If the value does not match the test will return “One or more errors occurred. (Exception has been thrown by the target of an invocation.)”
- Reload the page to reset the sample to the default state
Want to explore more concepts examples checkout the Learning Playground to explore related testing concepts.
To run the test, follow these steps:
Navigate to the Connector Sample Directory:
\samples\connector\
directory in the Power Apps Test Engine repository.Run the Test Script:
RunTests.ps1
script to start the test:pwsh -File RunTests.ps1
The test plan for the connector sample includes a section that simulates the MSN Weather connector. This is defined in the onTestSuiteStart
block of the testPlan.fx.yaml
file. By using this value we can ensure that the expected simulation is applied before the test cases is loaded.
Here’s the relevant part of the testPlan.fx.yaml
file:
# yaml-embedded-languages: powerfx
testSuite:
testSuiteName: Connector App
testSuiteDescription: Verifies that you can mock network requests
persona: User1
appLogicalName: new_connectorapp_da583
onTestSuiteStart: |
= Preview.SimulateConnector({name: "msnweather", then: {responses: { daily: { day: { summary: "You are seeing the mock response" }}}}})
testCases:
- testCaseName: Fill in a city name and do the search
testSteps: |
= Screenshot("connectorapp_loaded.png");
SetProperty(TextInput1.Text, "Atlanta");
Select(Button1);
Assert(Label4.Text = "You are seeing the mock response", "Validate the output is from the mock");
Screenshot("connectorapp_end.png");
testSettings:
locale: "en-US"
recordVideo: true
browserConfigurations:
- browser: Chromium
environmentVariables:
users:
- personaName: User1
emailKey: user1Email
passwordKey: user1Password
The onTestSuiteStart block sets up the simulated connector response.
= Preview.SimulateConnector({name: "msnweather", then: {responses: { daily: { day: { summary: "You are seeing the mock response" }}}}})
Test Steps:
The test steps include taking a screenshot, setting the text input to “Atlanta”, clicking the search button, and asserting the label text.
= Screenshot("connectorapp_loaded.png");
SetProperty(TextInput1.Text, "Atlanta");
Select(Button1);
Assert(Label4.Text = "You are seeing the mock response", "Validate the output is from the mock");
Screenshot("connectorapp_end.png");
Using simulated connectors in your Power Apps tests allows you to control the responses from external services, making it easier to test various scenarios. By following the steps outlined in this section, you can set up and run tests that use simulated connectors, ensuring your application behaves as expected under different conditions.