50 / 66 · 09 Mobile & Cross-Platform Testing · Testing Voice Interfaces and Camera-Based Features← prev⊞ allnext →☰ Read as one page
9.2Testing Voice Command Processing
NLU Layer Testing
# Testing voice command processing (the recognition part)
def test_voice_command_parsing():
"""Test that the NLU layer correctly parses voice commands."""
test_cases = [
{
"transcript": "show me my orders from last week",
"expected_intent": "view_orders",
"expected_entities": {"time_range": "last_week"}
},
{
"transcript": "cancel order number twelve thirty four",
"transcript_variants": [
"cancel order number 1234",
"cancel order #1234",
],
"expected_intent": "cancel_order",
"expected_entities": {"order_id": "1234"}
},
{
"transcript": "how much is shipping to new york",
"expected_intent": "shipping_estimate",
"expected_entities": {"destination": "new_york"}
},
{
"transcript": "add two widgets to my cart",
"transcript_variants": [
"add 2 widgets to my cart",
"at two widgets to my cart", # Common misrecognition
],
"expected_intent": "add_to_cart",
"expected_entities": {"quantity": 2, "product": "widgets"}
},
]
for case in test_cases:
# Test primary transcript
result = parse_voice_command(case["transcript"])
assert result.intent == case["expected_intent"], \
f"Intent mismatch for '{case['transcript']}': expected {case['expected_intent']}, got {result.intent}"
for key, value in case["expected_entities"].items():
assert result.entities[key] == value
# Test transcript variants (speech-to-text produces different outputs)
for variant in case.get("transcript_variants", []):
result = parse_voice_command(variant)
assert result.intent == case["expected_intent"], \
f"Variant '{variant}' produced wrong intent: {result.intent}"
Ambiguity and Error Handling
def test_ambiguous_voice_commands():
"""Test handling of ambiguous or incomplete commands."""
ambiguous_cases = [
{
"transcript": "order",
"expected": "clarification_needed",
"description": "Too vague -- could be view, create, or cancel"
},
{
"transcript": "cancel",
"expected": "clarification_needed",
"description": "Missing order ID"
},
{
"transcript": "",
"expected": "no_input",
"description": "Empty transcript (silence)"
},
{
"transcript": "asdfghjkl",
"expected": "unrecognized",
"description": "Nonsensical input"
},
]
for case in ambiguous_cases:
result = parse_voice_command(case["transcript"])
assert result.intent == case["expected"], \
f"'{case['transcript']}' ({case['description']}): expected {case['expected']}, got {result.intent}"
def test_voice_command_confidence_threshold():
"""Commands below confidence threshold should trigger confirmation."""
# Low confidence should prompt "Did you mean...?"
result = parse_voice_command("cancel order maybe twelve thirty four ish")
if result.confidence < 0.7:
assert result.needs_confirmation is True
End-to-End Voice Flow Testing
def test_voice_to_action_flow(driver):
"""Test the complete voice command flow: mic -> speech -> action."""
# Navigate to voice-enabled screen
driver.find_element(AppiumBy.ACCESSIBILITY_ID, "voice-command-btn").click()
# On emulators, inject pre-recorded audio
driver.execute_script("mobile: shell", {
"command": "am",
"args": ["broadcast", "-a", "com.testapp.INJECT_AUDIO",
"--es", "audio_path", "/sdcard/test_audio/cancel_order.wav"]
})
# Verify the app processed the command
confirmation = driver.find_element(AppiumBy.ACCESSIBILITY_ID, "voice-confirmation")
assert "cancel" in confirmation.text.lower()
assert "order" in confirmation.text.lower()