test_browsing.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. """Browsing-related tests for the EventStreamRuntime, which connects to the ActionExecutor running in the sandbox."""
  2. from conftest import _close_test_runtime, _load_runtime
  3. from openhands.core.logger import openhands_logger as logger
  4. from openhands.events.action import (
  5. BrowseURLAction,
  6. CmdRunAction,
  7. )
  8. from openhands.events.observation import (
  9. BrowserOutputObservation,
  10. CmdOutputObservation,
  11. )
  12. # ============================================================================================================================
  13. # Browsing tests, without evaluation (poetry install --without evaluation)
  14. # For eval environments, tests need to run with poetry install
  15. # ============================================================================================================================
  16. PY3_FOR_TESTING = '/openhands/micromamba/bin/micromamba run -n openhands python3'
  17. def test_simple_browse(temp_dir, runtime_cls, run_as_openhands):
  18. runtime = _load_runtime(temp_dir, runtime_cls, run_as_openhands)
  19. # Test browse
  20. action_cmd = CmdRunAction(
  21. command=f'{PY3_FOR_TESTING} -m http.server 8000 > server.log 2>&1 &'
  22. )
  23. logger.info(action_cmd, extra={'msg_type': 'ACTION'})
  24. obs = runtime.run_action(action_cmd)
  25. logger.info(obs, extra={'msg_type': 'OBSERVATION'})
  26. assert isinstance(obs, CmdOutputObservation)
  27. assert obs.exit_code == 0
  28. assert '[1]' in obs.content
  29. action_cmd = CmdRunAction(command='sleep 3 && cat server.log')
  30. logger.info(action_cmd, extra={'msg_type': 'ACTION'})
  31. obs = runtime.run_action(action_cmd)
  32. logger.info(obs, extra={'msg_type': 'OBSERVATION'})
  33. assert obs.exit_code == 0
  34. action_browse = BrowseURLAction(url='http://localhost:8000')
  35. logger.info(action_browse, extra={'msg_type': 'ACTION'})
  36. obs = runtime.run_action(action_browse)
  37. logger.info(obs, extra={'msg_type': 'OBSERVATION'})
  38. assert isinstance(obs, BrowserOutputObservation)
  39. assert 'http://localhost:8000' in obs.url
  40. assert not obs.error
  41. assert obs.open_pages_urls == ['http://localhost:8000/']
  42. assert obs.active_page_index == 0
  43. assert obs.last_browser_action == 'goto("http://localhost:8000")'
  44. assert obs.last_browser_action_error == ''
  45. assert 'Directory listing for /' in obs.content
  46. assert 'server.log' in obs.content
  47. # clean up
  48. action = CmdRunAction(command='rm -rf server.log')
  49. logger.info(action, extra={'msg_type': 'ACTION'})
  50. obs = runtime.run_action(action)
  51. logger.info(obs, extra={'msg_type': 'OBSERVATION'})
  52. assert obs.exit_code == 0
  53. _close_test_runtime(runtime)