Replaced mocked Playwright usage with real Playwright implementation in the test suite.

This commit is contained in:
Paul Gauthier (aider) 2024-07-23 11:43:14 +02:00
parent 8a73346f66
commit f99dbc2c43

View file

@ -1,5 +1,5 @@
import unittest
from unittest.mock import patch, MagicMock
from unittest.mock import MagicMock
import re
from aider.commands import Commands
@ -12,19 +12,13 @@ class TestScrape(unittest.TestCase):
self.io = InputOutput(yes=True)
self.commands = Commands(self.io, None)
@patch("aider.scrape.Scraper.scrape")
def test_cmd_web_imports_playwright(self, mock_scrape):
# Mock the scrape method
mock_scrape.return_value = "Mocked webpage content"
def test_cmd_web_imports_playwright(self):
# Run the cmd_web command
result = self.commands.cmd_web("https://example.com")
# Assert that the scrape method was called with the correct URL
mock_scrape.assert_called_once_with("https://example.com")
# Assert that the result contains the mocked content
self.assertIn("Mocked webpage content", result)
# Assert that the result contains some content
self.assertIsNotNone(result)
self.assertNotEqual(result, "")
# Try to import playwright
try:
@ -39,15 +33,7 @@ class TestScrape(unittest.TestCase):
playwright_imported, "Playwright should be importable after running cmd_web"
)
@patch("aider.scrape.sync_playwright")
def test_scrape_actual_url_with_playwright(self, mock_sync_playwright):
# Mock the Playwright browser and page
mock_browser = MagicMock()
mock_page = MagicMock()
mock_browser.new_page.return_value = mock_page
mock_page.content.return_value = "<html><body><h1>Test Page</h1></body></html>"
mock_sync_playwright.return_value.__enter__.return_value.chromium.launch.return_value = mock_browser
def test_scrape_actual_url_with_playwright(self):
# Create a Scraper instance with a mock print_error function
mock_print_error = MagicMock()
scraper = Scraper(print_error=mock_print_error, playwright_available=True)
@ -57,17 +43,11 @@ class TestScrape(unittest.TestCase):
# Assert that the result contains expected content
self.assertIsNotNone(result)
self.assertIn("Test Page", result)
self.assertIn("Example Domain", result)
# Assert that print_error was never called
mock_print_error.assert_not_called()
# Assert that Playwright methods were called
mock_sync_playwright.assert_called_once()
mock_browser.new_page.assert_called()
mock_page.goto.assert_called_with("https://example.com", wait_until="networkidle", timeout=5000)
mock_page.content.assert_called_once()
def test_scraper_print_error_not_called(self):
# Create a Scraper instance with a mock print_error function
mock_print_error = MagicMock()