Modern QA2026A Complete ReAct Agent Implementation — tiles
Log inJoin
3 / 108 · 03 Agentic Testing Architectures · The ReAct Core Loop for Testing← prev⊞ allnext →☰ Read as one page

1.3A Complete ReAct Agent Implementation

class ReActTestAgent:
    def __init__(self, llm, browser, max_steps=20):
        self.llm = llm
        self.browser = browser
        self.max_steps = max_steps
        self.history = []

    def run(self, test_objective: str) -> TestResult:
        """Execute a test using the ReAct loop."""
        for step in range(self.max_steps):
            # OBSERVE
            observation = self.browser.get_state()

            # THINK
            prompt = f"""
            Objective: {test_objective}
            Current URL: {observation.url}
            Page content (truncated): {observation.text[:2000]}
            Console errors: {observation.errors}
            History: {self.history[-5:]}

            What should I do next? Choose one:
            - NAVIGATE <url>
            - CLICK <selector>
            - TYPE <selector> <text>
            - ASSERT <condition>
            - DONE <pass|fail> <reason>
            """
            decision = self.llm.generate(prompt)

            # ACT
            action_result = self.execute(decision)
            self.history.append({
                "step": step,
                "observation": observation.summary,
                "decision": decision,
                "result": action_result
            })

            # EVALUATE
            if decision.startswith("DONE"):
                return self.parse_result(decision)

        return TestResult(
            status="TIMEOUT",
            reason=f"Exceeded {self.max_steps} steps"
        )