All posts

Build Data‑Driven Phoma Flows That Pull Test Inputs from a Remote JSON Endpoint

Build Data‑Driven Phoma Flows That Pull Test Inputs from a Remote JSON Endpoint

Why data‑driven flows matter

Hard‑coding usernames, product IDs, or search terms inside a flow makes maintenance painful. When the test matrix grows — different accounts, regions, or feature flags — you end up duplicating the same flow or editing it every run. A data‑driven approach lets you keep the what (the flow logic) separate from the which (the test inputs). Phoma’s HTTP Request step, combined with Loop and Condition steps, gives you everything you need to pull a JSON array from a URL and iterate over it, all without writing code.

Prerequisites

  • Phoma installed on Windows and at least one Android phone connected (USB or Wi‑Fi).
  • A place to host a static JSON file that Phoma can reach via HTTP/HTTPS. Good options:
    • A GitHub Gist (raw URL).
    • A private GitHub repository with GitHub Pages enabled.
    • A tiny local HTTP server (python -m http.server 8000) if the PC and phone are on the same LAN.
  • Basic familiarity with Phoma’s visual builder: adding steps, setting selectors, and using variables.

1. Design the JSON test‑data contract

Keep the schema simple and version‑tolerant. Each object in the top‑level array represents one test case. Example test‑cases.json:

[
  {
    "id": "login‑valid‑01",
    "username": "alice@example.com",
    "password": "Secret123!",
    "expectedToast": "Welcome, Alice"
  },
  {
    "id": "login‑invalid‑02",
    "username": "bob@example.com",
    "password": "wrong",
    "expectedToast": "Invalid credentials"
  },
  {
    "id": "login‑empty‑03",
    "username": "",
    "password": "",
    "expectedToast": "Please enter credentials"
  }
]

Why this shape?

  • id gives you a stable key for logging and screenshots.
  • Flat fields map directly to Phoma variables (username, password, expectedToast).
  • Adding new fields later (e.g., deviceTag) won’t break existing flows.

Host the file and note the raw URL (e.g., https://gist.githubusercontent.com/yourname/abc123/raw/test-cases.json).

2. Create a new flow in Phoma

  1. Open Phoma → Flows → New Flow.
  2. Name it Data‑Driven Login Test.
  3. Set Target App to the package you’re testing (e.g., com.example.myapp).

3. Add the HTTP Request step to fetch the JSON

  1. Click + Step → HTTP Request.
  2. Configure:
    • Method: GET
    • URL: paste your raw JSON URL.
    • Response Type: JSON (Phoma will parse the body into a variable).
    • Variable Name: testCases (this becomes a list variable you can loop over).
  3. Run once to verify the request succeeds (green check). You should see the array length in the run log.

Tip: If the endpoint requires authentication, add headers in the Headers table (e.g., Authorization: Bearer <token>).

4. Loop over the test cases

  1. Add a Loop step after the HTTP Request.
  2. Set Loop Type → For each item in variable.
  3. Choose variable testCases.
  4. Phoma creates a loop block; everything you drag inside will execute once per array element.

Inside the loop you now have access to the current item’s fields via dot notation: item.username, item.password, item.expectedToast, item.id.

5. Build the per‑iteration logic

5.1 Open the app and reach the login screen

  • Step: Open App → package com.example.myapp.
  • Step: Wait for Element → selector for the username field (e.g., resourceId=com.example.myapp:id/et_username).

5.2 Type credentials from the current item

  • Step: Type Text → target the username field, text {{item.username}}.
  • Step: Type Text → target the password field, text {{item.password}}.

Phoma resolves {{item.username}} at runtime for each iteration.

5.3 Submit and verify

  • Step: Tap → login button selector.
  • Step: Wait for Element → toast or success screen selector.
  • Step: Read Text → capture the toast message into variable actualToast.
  • Step: Condition → If actualToast == {{item.expectedToast}} then Pass else Fail.
  • Step: Screenshot → name {{item.id}}_result.png (useful for later review).

5.4 Clean up for next iteration

  • Step: Press Keys → BACK (or Close App) to return to a known state.
  • Step: Wait → 500 ms (prevents race conditions).

6. Run the flow on one or many devices

  • In the Run dialog, pick a single phone or a device group.
  • Enable Run on all selected devices if you want the same data matrix on multiple phones (e.g., different OS versions).
  • Click Run.

Phoma will:

  1. Fetch the JSON once per run (not per device).
  2. For each device, iterate the loop, substituting the current item’s values.
  3. Produce a run report with a row per iteration per device, plus the screenshots you named.

7. Extend the pattern

Need How to add it
Different environments (staging vs prod) Host separate JSON files (test-cases-staging.json, test-cases-prod.json) and parameterize the URL with a Phoma Run Variable (set at run time).
Dynamic data (e.g., generated OTP) Add a second HTTP Request inside the loop that calls your OTP service, store the response in item.otp, then type {{item.otp}}.
Parallel execution Create a Sub‑Flow containing the per‑iteration logic, then call it from the loop with Run Sub‑Flow (Phoma runs each sub‑flow sequentially; for true parallelism, duplicate the flow per device group).
Reporting to a dashboard After the loop, add an HTTP Request (POST) that sends a summary JSON ({flow: "Data‑Driven Login", passes: 2, fails: 1}) to your internal API.

8. Troubleshooting checklist

  • HTTP Request fails → verify URL is reachable from the PC (open in browser). Check firewall / VPN.
  • JSON parsing error → ensure the response is valid JSON (no trailing commas, correct encoding).
  • Loop never runs → confirm testCases variable is an array; a single object will not iterate.
  • Variable substitution not working → use double curly braces {{item.field}} exactly; Phoma does not support $ syntax.
  • Selector flakiness → prefer stable attributes (resourceId, content-desc) over text; see “Choose Stable Android Element Selectors for Phoma Flows” for deeper guidance.

9. Full flow outline (copy‑paste into Phoma’s export/import if you use JSON export)

Flow: Data‑Driven Login Test
Steps:
1. HTTP Request (GET) → variable testCases
2. Loop (for each item in testCases)
   2.1 Open App (com.example.myapp)
   2.2 Wait for Element (username field)
   2.3 Type Text → {{item.username}}
   2.4 Type Text → {{item.password}}
   2.5 Tap (login button)
   2.6 Wait for Element (toast)
   2.7 Read Text → actualToast
   2.8 Condition: actualToast == {{item.expectedToast}}
   2.9 Screenshot → {{item.id}}_result.png
   2.10 Press Keys (BACK)
   2.11 Wait (500 ms)
3. End Loop

10. Next steps

  • Version your test data alongside your app releases (Git tags).
  • Add a CI step that runs the flow nightly via Phoma’s scheduler (see “Schedule Unattended Phoma Flows on Android Test Phones”).
  • Share the flow with your team using Phoma’s Team plan so everyone runs the same data‑driven matrix.

By pulling test inputs from a remote JSON endpoint, you turn a single Phoma flow into a reusable test harness that scales with your test matrix — no code, no duplicate flows, and a single source of truth for test data.