Testing liquid logic with test contacts

Loading

In Power Pages (formerly PowerApps Portals), Liquid is a powerful template language that allows for dynamic content rendering, manipulation of data, and interaction with Portal entities. When building portals that require personalized experiences or custom logic, developers use Liquid to handle things like form submissions, displaying user-specific content, or integrating with Dataverse.

However, testing Liquid logic can be challenging, especially when you need to verify that data rendering works correctly for different users. One effective way to test Liquid logic is by using test contacts. This allows developers and testers to simulate real-world scenarios where data is personalized based on the user’s profile or input.

In this guide, we’ll explore the best practices for testing Liquid logic with test contacts in Power Pages to ensure dynamic content is displayed correctly and that business logic works as intended.


1. Understanding Liquid Logic in Power Pages

Liquid in Power Pages is used for customizing content in a portal dynamically. It allows you to:

  • Display dynamic content based on user roles, attributes, or form submissions.
  • Manipulate data by interacting with Portal entities, Dataverse, or other integrated systems.
  • Create conditional content to change the user experience based on context, like user’s login status, preferences, or submitted data.

For example, you might want to display a user’s name, a list of records, or personalized messages based on their profile.


2. Creating Test Contacts for Liquid Logic Testing

Test contacts are dummy or example user accounts that can be used to simulate various scenarios during testing. By creating test contacts with different attributes, roles, or data, you can verify if your Liquid logic correctly displays dynamic content based on the contact’s details.

Best Practices for Creating Test Contacts:

  • Variety of Data: Create multiple test contacts with different attributes (e.g., names, roles, access levels, account status) to simulate a range of real-world users.
  • Mimic Real User Data: Populate the test contact records with realistic data that will be used during testing. This includes user profiles, preferred languages, or geographic locations that could affect the content rendered by Liquid.
  • Use Different Roles: If your portal uses role-based content (e.g., admin, customer, or guest), create test contacts with different roles to ensure that Liquid logic behaves as expected for each user type.
  • Test Case Scenarios: Develop specific scenarios for each test contact. For example, if a user is supposed to see a personalized greeting, ensure the Liquid code properly retrieves the contact’s name.

3. Setting Up the Testing Environment

To test Liquid logic with test contacts, you need to have the following in place:

Best Practices for Environment Setup:

  • Deploy Portal to Staging Environment: Perform all Liquid testing in a non-production environment to avoid impacting live users.
  • Create and Manage Test Contacts in Dataverse: In the Power Pages environment, contacts are stored in Dataverse. Use the Dataverse table for contacts and create new records with the necessary attributes for testing.
    • Navigate to Power Apps > Dataverse > Contacts and manually create test contacts, or use bulk import tools to add them.
  • Ensure Correct Access Permissions: Make sure your test contacts have the necessary roles or permissions to access the parts of the portal where you will test Liquid functionality.
  • Enable Debugging: Enable debugging tools in the portal to log the output and errors from Liquid rendering. This will help in troubleshooting if something doesn’t render as expected.

4. Implementing Liquid Logic for Testing

Once your test contacts are created, the next step is to implement the Liquid logic you want to test. Here are some common examples:

Example 1: Displaying a Personalized Greeting

If you want to display a personalized message for logged-in users, you can use the following Liquid code:

liquidCopyEdit{% if user %}
  <h1>Hello, {{ user.contact.firstname }}!</h1>
{% else %}
  <h1>Welcome, Guest!</h1>
{% endif %}

This logic checks if the user is logged in and then displays their first name. If the user is not logged in, it will display a generic welcome message.

Example 2: Conditional Content Based on User Role

You can also use Liquid to render different content based on the user’s role. Here’s an example of how to display different content for admins and regular users:

{% if user.role == 'Administrator' %}
<p>Welcome, Admin! You have full access.</p>
{% else %}
<p>Welcome, User! Your access is limited.</p>
{% endif %}

In this example, the content displayed is based on the role assigned to the user. The roles are fetched from the user’s contact record in Dataverse.


5. Testing the Liquid Logic with Test Contacts

After implementing the Liquid logic, you can now start testing it with your test contacts. Here’s how you can test:

Best Practices for Testing Liquid Logic:

  • Log in as Different Test Contacts: Log in using the test contacts you’ve created. For example, use the credentials of a user with an “Admin” role and one with a “Customer” role. Verify that each sees the correct personalized content.
  • Verify Data Display: Check that dynamic data such as names, emails, or other contact attributes display correctly in the Liquid output. Ensure that the correct content is rendered based on the contact’s data.
  • Validate Conditional Logic: Test whether the conditional statements (e.g., checking user roles or data attributes) are functioning as expected. For example, verify that the correct message appears for admin users versus regular users.
  • Use Debugging Mode: If you’re not seeing the expected output, enable debugging mode to view the Liquid errors or any variables being passed through the code.

Debugging Liquid Logic:

To help debug Liquid code in Power Pages, you can:

  • Use the Liquid Debugging feature, which displays the values of variables.
  • Log output to the page by using {{ variable | json }} or {{ variable | inspect }} to print the values of objects or variables.

Example:

<p>Contact First Name: {{ user.contact.firstname | inspect }}</p>

This will display the value of the first name for the logged-in user.


6. Automating Test Contact Management

For more advanced testing, especially in larger environments, you might want to automate the creation and management of test contacts. You can use Power Automate or other automation tools to create and update test contact records in bulk, simulate different data sets, and delete test data after testing is complete.

Leave a Reply

Your email address will not be published. Required fields are marked *