detox_generate_matcher
Generate Detox matcher code for selecting mobile app elements by ID, text, label, type, or traits in React Native E2E testing.
Instructions
Generate Detox matcher code for element selection (by.id, by.text, etc.).
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"elementDescription": "<elementDescription>"
},
"name": "detox_generate_matcher"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| elementDescription | Yes | Description of the element to match | |
| matcherType | No | ||
| withAncestor | No | Ancestor matcher | |
| withDescendant | No | Descendant matcher | |
| atIndex | No | Index for multiple matches |
Implementation Reference
- src/tools/index.ts:364-380 (handler)The handler function that executes the detox_generate_matcher tool: parses input args, calls generateMatcherCode helper, and returns the generated code.
handler: async (args: z.infer<typeof GenerateMatcherArgsSchema>) => { const parsed = GenerateMatcherArgsSchema.parse(args); const code = generateMatcherCode({ type: parsed.matcherType || "id", value: parsed.elementDescription, withAncestor: parsed.withAncestor, withDescendant: parsed.withDescendant, atIndex: parsed.atIndex, }); return { success: true, code, description: `Matcher for: ${parsed.elementDescription}`, }; }, - src/utils/validators.ts:99-105 (schema)Zod schema defining the input parameters for the detox_generate_matcher tool.
export const GenerateMatcherArgsSchema = z.object({ elementDescription: z.string().describe("Description of the element to match"), matcherType: z.enum(["id", "text", "label", "type", "traits"]).optional(), withAncestor: z.string().optional().describe("Ancestor matcher"), withDescendant: z.string().optional().describe("Descendant matcher"), atIndex: z.number().optional().describe("Index for multiple matches"), }); - Core helper function that constructs the Detox matcher code string based on type, value, and optional modifiers like ancestor, descendant, and index.
export function generateMatcherCode(options: { type: "id" | "text" | "label" | "type" | "traits"; value: string; withAncestor?: string; withDescendant?: string; atIndex?: number; }): string { let matcher: string; switch (options.type) { case "id": matcher = `by.id('${options.value}')`; break; case "text": matcher = `by.text('${options.value}')`; break; case "label": matcher = `by.label('${options.value}')`; break; case "type": matcher = `by.type('${options.value}')`; break; case "traits": matcher = `by.traits(['${options.value}'])`; break; default: matcher = `by.id('${options.value}')`; } if (options.withAncestor) { matcher = `${matcher}.withAncestor(by.id('${options.withAncestor}'))`; } if (options.withDescendant) { matcher = `${matcher}.withDescendant(by.id('${options.withDescendant}'))`; } let code = `element(${matcher})`; if (options.atIndex !== undefined) { code = `element(${matcher}).atIndex(${options.atIndex})`; } return code; } - src/tools/index.ts:429-442 (registration)Registration of the detox_generate_matcher tool (as generateMatcherTool) in the allTools array exported for use in the MCP server.
export const allTools: Tool[] = [ buildTool, testTool, initTool, readConfigTool, listConfigurationsTool, validateConfigTool, createConfigTool, listDevicesTool, generateTestTool, generateMatcherTool, generateActionTool, generateExpectationTool, ];