From 1cfdfa6c21c44c9892f66da021f0765adf51a81a Mon Sep 17 00:00:00 2001 From: cmathw Date: Thu, 28 Nov 2024 21:28:19 +1100 Subject: [PATCH] /Users/chris/Harmony-Intelligence/inspect_ai/src/inspect_ai/tool/_tools/_web_browser/_resources/test_playwright_crawler.py --- .../_resources/test_playwright_crawler.py | 123 +++++++++++++++++- 1 file changed, 116 insertions(+), 7 deletions(-) diff --git a/src/inspect_ai/tool/_tools/_web_browser/_resources/test_playwright_crawler.py b/src/inspect_ai/tool/_tools/_web_browser/_resources/test_playwright_crawler.py index f09210e36..826b51100 100644 --- a/src/inspect_ai/tool/_tools/_web_browser/_resources/test_playwright_crawler.py +++ b/src/inspect_ai/tool/_tools/_web_browser/_resources/test_playwright_crawler.py @@ -3,28 +3,49 @@ class TestPlaywrightCrawler(parameterized.TestCase): - def setUp(self): - self._browser = playwright_crawler.PlaywrightBrowser() + def init_crawler(self, headless): + self._browser = playwright_crawler.PlaywrightBrowser(headless=headless) self._crawler = playwright_crawler.PlaywrightCrawler( self._browser.get_new_context() ) - def test_go_to_page_changes_url(self): + def tearDown(self): + if hasattr(self, "_browser"): + self._browser.close() + super().tearDown() + + @parameterized.named_parameters( + {"testcase_name": "_HeadlessTrue", "headless": True}, + {"testcase_name": "_HeadlessFalse", "headless": False}, + ) + def test_go_to_page_changes_url(self, headless): + self.init_crawler(headless=headless) self.assertEqual(self._crawler.url, "about:blank") self._crawler.go_to_page("https://www.example.com") self.assertEqual(self._crawler.url, "https://www.example.com/") - def test_go_to_page_adds_missing_protocol(self): + @parameterized.named_parameters( + {"testcase_name": "_HeadlessTrue", "headless": True}, + {"testcase_name": "_HeadlessFalse", "headless": False}, + ) + def test_go_to_page_adds_missing_protocol(self, headless): + self.init_crawler(headless=headless) self._crawler.go_to_page("www.example.com") self.assertEqual(self._crawler.url, "https://www.example.com/") - def test_nodes_change_on_update(self): + @parameterized.named_parameters( + {"testcase_name": "_HeadlessTrue", "headless": True}, + {"testcase_name": "_HeadlessFalse", "headless": False}, + ) + def test_nodes_change_on_update(self, headless): + self.init_crawler(headless=headless) self._crawler.go_to_page("https://www.example.com") self.assertFalse(self._crawler._nodes) self._crawler.update() self.assertTrue(self._crawler._nodes) - def test_render_accessibility_tree(self): + def test_render_accessibility_tree_headless(self): + self.init_crawler(headless=True) self._crawler.go_to_page("https://www.example.com") at_no_update = self._crawler.render(playwright_crawler.CrawlerOutputFormat.AT) self.assertEqual(at_no_update, "") @@ -50,7 +71,42 @@ def test_render_accessibility_tree(self): ) ) - def test_click_adjusts_to_scrolling_position(self): + def test_render_accessibility_tree_headful(self): + """Order of AT Nodes varies more in headful browsing. Check content of nodes, not order""" + self.init_crawler(headless=False) + self._crawler.go_to_page("https://www.example.com") + at_no_update = self._crawler.render(playwright_crawler.CrawlerOutputFormat.AT) + self.assertEqual(at_no_update, "") + + self._crawler.update() + + at_update = self._crawler.render(playwright_crawler.CrawlerOutputFormat.AT) + nodes = at_update.splitlines() + self.assertEqual(len(nodes), 3) + self.assertTrue( + any( + 'RootWebArea "Example Domain" [focused: True, url: https://www.example.com/]' + in node + for node in nodes + ) + ) + self.assertTrue( + any( + 'StaticText "This domain is for use in illustrative examples in documents' + in node + for node in nodes + ) + ) + self.assertTrue( + any( + 'link "More information..." [url: https://www.iana.org/domains/example]' + in node + for node in nodes + ) + ) + + def test_click_adjusts_to_scrolling_position_headless(self): + self.init_crawler(headless=True) test_html = """ @@ -91,3 +147,56 @@ def test_click_adjusts_to_scrolling_position(self): self._crawler.update() at_after_click = self._crawler.render(playwright_crawler.CrawlerOutputFormat.AT) self.assertIn("Text Changed!", at_after_click) + + def test_click_adjusts_to_scrolling_position_headful(self): + """Order of AT Nodes varies more in headful browsing. Check content of nodes, not order""" + self.init_crawler(headless=False) + test_html = """ + + + + + Scrolling Test Page + + + + + + + + """ + self._crawler._page.set_content(test_html) + self._crawler.update() + at_before_scroll = self._crawler.render( + playwright_crawler.CrawlerOutputFormat.AT + ) + self.assertIn("Scrolling Test Page", at_before_scroll) + self.assertNotIn("Click Me", at_before_scroll) + + self._crawler.scroll("down") + self._crawler.update() + at_after_scroll = self._crawler.render( + playwright_crawler.CrawlerOutputFormat.AT + ) + self.assertIn("Click Me", at_after_scroll) + + # Find the button node ID dynamically + button_node_id = None + for node_id, node in self._crawler._nodes.items(): + if "Click Me" in str(node): + button_node_id = node_id + break + + self.assertIsNotNone(button_node_id, "Button node was not found") + + self._crawler.click(button_node_id) + self._crawler.update() + at_after_click = self._crawler.render(playwright_crawler.CrawlerOutputFormat.AT) + self.assertIn("Text Changed!", at_after_click)