[Feature]: Support for Task Nesting and Conditional Execution in YAML Scripts
midscene.js provides a powerful YAML-based automation script format that allows defining test scenarios in a declarative way. However, when building complex test suites, we encountered scenarios where the current YAML syntax falls short.
## Feature 1: Task Nesting / Task Reuse
**Problem:**
Currently, there's no way to call one task from another. This leads to code duplication when the same sequence of actions needs to be performed in multiple tasks.
**Example use case:**
A "Login" task needs to be called before many other tasks. Without task nesting, the login steps must be duplicated in every task that requires authentication.
**Proposed syntax:**
```yaml
tasks:
- name: Login
flow:
- aiTap: username input field
- aiInput:
value: testuser
- aiTap: password input field
- aiInput:
value: password123
- aiTap: login button
- name: Search Train
flow:
- callTask: Login # nested call to Login task
- aiTap: search button
# ... other steps
- name: Book Train
flow:
- callTask: Login # reuse Login task
- aiTap: book button
# ... other steps
```
**Alternative syntax options:**
```yaml
# Option A: callTask (recommended - concise and explicit)
- callTask: Login
# Option B: include (familiar to template engine users)
- include: Login
```
## Feature 2: Conditional Execution
**Problem:**
There's no way to conditionally execute steps based on runtime conditions (e.g., page state, assertion results, variable values).
**Example use case:**
- If a popup appears, dismiss it
- If already logged in, skip the login flow
- Handle different app states based on what's visible
**Proposed syntax:**
```yaml
tasks:
- name: Handle Login State
flow:
# Conditional: if-else based on assertion
- if:
condition:
aiAssert: login button is visible on current page
then:
- aiTap: login button
# ... login flow
else:
- aiAction: already logged in, continue with operations
# Conditional: if-only (skip if condition not met)
- if:
condition:
aiAssert: popup ad is visible
then:
- aiTap: close ad button
# Conditional: based on variable
- if:
condition: ${isLoggedIn} == false
then:
- callTask: Login
```
## Benefits
1. **Code Reusability** - Define common flows once, reuse across tasks
2. **Maintainability** - Update login logic in one place, all tasks benefit
3. **Expressiveness** - Handle complex real-world scenarios gracefully
4. **Non-developer Friendly** - Clear, readable syntax for QA/PM
## Current Workaround
Users can:
1. Duplicate steps across tasks (maintenance burden)
2. Use JavaScript for logic (less accessible for non-developers)
3. Build custom orchestration layer outside midscene (adds complexity)
0 条评论