Skip to content

Commit 7f27722

Browse files
committed
test: refactor tests to use public methods for element visibility checks
1 parent 954303e commit 7f27722

File tree

2 files changed

+63
-33
lines changed

2 files changed

+63
-33
lines changed

tests/test_browser/test_browser_tab.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,26 @@ async def test_take_screenshot_as_base64(self, tab):
406406
assert result == screenshot_data
407407
assert_mock_called_at_least_once(tab._connection_handler)
408408

409+
@pytest.mark.asyncio
410+
async def test_take_screenshot_beyond_viewport(self, tab):
411+
"""Test capture_beyond_viewport flag is forwarded to command."""
412+
screenshot_data = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/wcAAgAB/edzE+oAAAAASUVORK5CYII='
413+
414+
with patch.object(tab, '_execute_command', AsyncMock(return_value={
415+
'result': {'data': screenshot_data}
416+
})) as mock_execute:
417+
result = await tab.take_screenshot(
418+
path=None,
419+
beyond_viewport=True,
420+
as_base64=True,
421+
)
422+
423+
mock_execute.assert_called_once()
424+
command = mock_execute.call_args[0][0]
425+
assert command['method'] == 'Page.captureScreenshot'
426+
assert command['params']['captureBeyondViewport'] is True
427+
assert result == screenshot_data
428+
409429
@pytest.mark.asyncio
410430
async def test_print_to_pdf_to_file(self, tab, tmp_path):
411431
"""Test printing to PDF and saving to file."""
@@ -1288,6 +1308,16 @@ async def test_get_frame_no_frame_id(self, tab, mock_browser):
12881308
class TestTabUtilityMethods:
12891309
"""Test Tab utility and helper methods."""
12901310

1311+
@pytest.mark.asyncio
1312+
async def test_bring_to_front(self, tab):
1313+
"""Test bringing the tab to front sends the correct command."""
1314+
with patch.object(tab, '_execute_command', AsyncMock()) as mock_execute:
1315+
await tab.bring_to_front()
1316+
1317+
mock_execute.assert_called_once()
1318+
command = mock_execute.call_args[0][0]
1319+
assert command['method'] == 'Page.bringToFront'
1320+
12911321
@pytest.mark.asyncio
12921322
async def test_close(self, tab, mock_browser):
12931323
"""Test closing the tab."""

tests/test_web_element.py

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -494,21 +494,21 @@ class TestWebElementClicking:
494494
async def test_click_using_js_success(self, web_element):
495495
"""Test successful JavaScript click."""
496496
# Mock element visibility and click success
497-
web_element._is_element_visible = AsyncMock(return_value=True)
497+
web_element.is_visible = AsyncMock(return_value=True)
498498
web_element.scroll_into_view = AsyncMock()
499-
web_element._execute_script = AsyncMock(
499+
web_element.execute_script = AsyncMock(
500500
return_value={'result': {'result': {'value': True}}}
501501
)
502502

503503
await web_element.click_using_js()
504504

505505
web_element.scroll_into_view.assert_called_once()
506-
web_element._is_element_visible.assert_called_once()
506+
web_element.is_visible.assert_called_once()
507507

508508
@pytest.mark.asyncio
509509
async def test_click_using_js_not_visible(self, web_element):
510510
"""Test JavaScript click when element is not visible."""
511-
web_element._is_element_visible = AsyncMock(return_value=False)
511+
web_element.is_visible = AsyncMock(return_value=False)
512512
web_element.scroll_into_view = AsyncMock()
513513

514514
with pytest.raises(ElementNotVisible):
@@ -517,9 +517,9 @@ async def test_click_using_js_not_visible(self, web_element):
517517
@pytest.mark.asyncio
518518
async def test_click_using_js_not_interactable(self, web_element):
519519
"""Test JavaScript click when element is not interactable."""
520-
web_element._is_element_visible = AsyncMock(return_value=True)
520+
web_element.is_visible = AsyncMock(return_value=True)
521521
web_element.scroll_into_view = AsyncMock()
522-
web_element._execute_script = AsyncMock(
522+
web_element.execute_script = AsyncMock(
523523
return_value={'result': {'result': {'value': False}}}
524524
)
525525

@@ -539,7 +539,7 @@ async def test_click_using_js_option_element(self, option_element):
539539
async def test_click_success(self, web_element):
540540
"""Test successful mouse click."""
541541
bounds = [0, 0, 100, 0, 100, 100, 0, 100] # Rectangle coordinates
542-
web_element._is_element_visible = AsyncMock(return_value=True)
542+
web_element.is_visible = AsyncMock(return_value=True)
543543
web_element.scroll_into_view = AsyncMock()
544544
web_element._connection_handler.execute_command.side_effect = [
545545
{'result': {'model': {'content': bounds}}}, # bounds
@@ -557,7 +557,7 @@ async def test_click_success(self, web_element):
557557
@pytest.mark.asyncio
558558
async def test_click_not_visible(self, web_element):
559559
"""Test click when element is not visible."""
560-
web_element._is_element_visible = AsyncMock(return_value=False)
560+
web_element.is_visible = AsyncMock(return_value=False)
561561

562562
with pytest.raises(ElementNotVisible):
563563
await web_element.click()
@@ -574,7 +574,7 @@ async def test_click_option_element(self, option_element):
574574
@pytest.mark.asyncio
575575
async def test_click_bounds_fallback_to_js(self, web_element):
576576
"""Test click falls back to JS bounds when CDP bounds fail."""
577-
web_element._is_element_visible = AsyncMock(return_value=True)
577+
web_element.is_visible = AsyncMock(return_value=True)
578578
web_element.scroll_into_view = AsyncMock()
579579

580580
# First call (bounds) raises KeyError, second call (JS bounds) succeeds
@@ -686,61 +686,61 @@ class TestWebElementVisibility:
686686
@pytest.mark.asyncio
687687
async def test_is_element_visible_true(self, web_element):
688688
"""Test _is_element_visible returns True."""
689-
web_element._execute_script = AsyncMock(
689+
web_element.execute_script = AsyncMock(
690690
return_value={'result': {'result': {'value': True}}}
691691
)
692692

693-
result = await web_element._is_element_visible()
693+
result = await web_element.is_visible()
694694
assert result is True
695695

696696
@pytest.mark.asyncio
697697
async def test_is_element_visible_false(self, web_element):
698698
"""Test _is_element_visible returns False."""
699-
web_element._execute_script = AsyncMock(
699+
web_element.execute_script = AsyncMock(
700700
return_value={'result': {'result': {'value': False}}}
701701
)
702702

703-
result = await web_element._is_element_visible()
703+
result = await web_element.is_visible()
704704
assert result is False
705705

706706
@pytest.mark.asyncio
707707
async def test_is_element_on_top_true(self, web_element):
708708
"""Test _is_element_on_top returns True."""
709-
web_element._execute_script = AsyncMock(
709+
web_element.execute_script = AsyncMock(
710710
return_value={'result': {'result': {'value': True}}}
711711
)
712712

713-
result = await web_element._is_element_on_top()
713+
result = await web_element.is_on_top()
714714
assert result is True
715715

716716
@pytest.mark.asyncio
717717
async def test_is_element_on_top_false(self, web_element):
718718
"""Test _is_element_on_top returns False."""
719-
web_element._execute_script = AsyncMock(
719+
web_element.execute_script = AsyncMock(
720720
return_value={'result': {'result': {'value': False}}}
721721
)
722722

723-
result = await web_element._is_element_on_top()
723+
result = await web_element.is_on_top()
724724
assert result is False
725725

726726
@pytest.mark.asyncio
727727
async def test_is_element_interactable_true(self, web_element):
728728
"""Test _is_element_interactable returns True."""
729-
web_element._execute_script = AsyncMock(
729+
web_element.execute_script = AsyncMock(
730730
return_value={'result': {'result': {'value': True}}}
731731
)
732732

733-
result = await web_element._is_element_interactable()
733+
result = await web_element.is_interactable()
734734
assert result is True
735735

736736
@pytest.mark.asyncio
737737
async def test_is_element_interactable_false(self, web_element):
738738
"""Test _is_element_interactable returns False."""
739-
web_element._execute_script = AsyncMock(
739+
web_element.execute_script = AsyncMock(
740740
return_value={'result': {'result': {'value': False}}}
741741
)
742742

743-
result = await web_element._is_element_interactable()
743+
result = await web_element.is_interactable()
744744
assert result is False
745745

746746

@@ -750,21 +750,21 @@ class TestWebElementWaitUntil:
750750
@pytest.mark.asyncio
751751
async def test_wait_until_visible_success(self, web_element):
752752
"""Test wait_until succeeds when element becomes visible."""
753-
web_element._is_element_visible = AsyncMock(side_effect=[False, True])
753+
web_element.is_visible = AsyncMock(side_effect=[False, True])
754754

755755
with patch('asyncio.sleep') as mock_sleep, \
756756
patch('asyncio.get_event_loop') as mock_loop:
757757
mock_loop.return_value.time.side_effect = [0, 0.5]
758758

759759
await web_element.wait_until(is_visible=True, timeout=2)
760760

761-
assert web_element._is_element_visible.call_count == 2
761+
assert web_element.is_visible.call_count == 2
762762
mock_sleep.assert_called_once_with(0.5)
763763

764764
@pytest.mark.asyncio
765765
async def test_wait_until_visible_timeout(self, web_element):
766766
"""Test wait_until raises WaitElementTimeout when visibility not met."""
767-
web_element._is_element_visible = AsyncMock(return_value=False)
767+
web_element.is_visible = AsyncMock(return_value=False)
768768

769769
with patch('asyncio.sleep') as mock_sleep, \
770770
patch('asyncio.get_event_loop') as mock_loop:
@@ -780,16 +780,16 @@ async def test_wait_until_visible_timeout(self, web_element):
780780
@pytest.mark.asyncio
781781
async def test_wait_until_interactable_success(self, web_element):
782782
"""Test wait_until succeeds when element becomes interactable."""
783-
web_element._is_element_interactable = AsyncMock(return_value=True)
783+
web_element.is_interactable = AsyncMock(return_value=True)
784784

785785
await web_element.wait_until(is_interactable=True, timeout=1)
786786

787-
web_element._is_element_interactable.assert_called_once()
787+
web_element.is_interactable.assert_called_once()
788788

789789
@pytest.mark.asyncio
790790
async def test_wait_until_interactable_timeout(self, web_element):
791791
"""Test wait_until raises WaitElementTimeout when not interactable."""
792-
web_element._is_element_interactable = AsyncMock(return_value=False)
792+
web_element.is_interactable = AsyncMock(return_value=False)
793793

794794
with patch('asyncio.sleep') as mock_sleep, \
795795
patch('asyncio.get_event_loop') as mock_loop:
@@ -805,8 +805,8 @@ async def test_wait_until_interactable_timeout(self, web_element):
805805
@pytest.mark.asyncio
806806
async def test_wait_until_visible_and_interactable(self, web_element):
807807
"""Test wait_until requires both conditions when both are True."""
808-
web_element._is_element_visible = AsyncMock(side_effect=[False, True])
809-
web_element._is_element_interactable = AsyncMock(side_effect=[False, True])
808+
web_element.is_visible = AsyncMock(side_effect=[False, True])
809+
web_element.is_interactable = AsyncMock(side_effect=[False, True])
810810

811811
with patch('asyncio.sleep') as mock_sleep, \
812812
patch('asyncio.get_event_loop') as mock_loop:
@@ -816,8 +816,8 @@ async def test_wait_until_visible_and_interactable(self, web_element):
816816
is_visible=True, is_interactable=True, timeout=2
817817
)
818818

819-
assert web_element._is_element_visible.call_count == 2
820-
assert web_element._is_element_interactable.call_count == 2
819+
assert web_element.is_visible.call_count == 2
820+
assert web_element.is_interactable.call_count == 2
821821
mock_sleep.assert_called_once_with(0.5)
822822

823823
@pytest.mark.asyncio
@@ -880,7 +880,7 @@ async def test_execute_script(self, web_element):
880880
expected_response = {'result': {'result': {'value': 'DIV'}}}
881881
web_element._connection_handler.execute_command.return_value = expected_response
882882

883-
result = await web_element._execute_script(script, return_by_value=True)
883+
result = await web_element.execute_script(script, return_by_value=True)
884884

885885
assert result == expected_response
886886
expected_command = RuntimeCommands.call_function_on(
@@ -1086,7 +1086,7 @@ async def test_text_property_with_malformed_html(self, web_element):
10861086
async def test_click_with_zero_hold_time(self, web_element):
10871087
"""Test click with zero hold time."""
10881088
bounds = [0, 0, 50, 0, 50, 50, 0, 50]
1089-
web_element._is_element_visible = AsyncMock(return_value=True)
1089+
web_element.is_visible = AsyncMock(return_value=True)
10901090
web_element.scroll_into_view = AsyncMock()
10911091
web_element._connection_handler.execute_command.side_effect = [
10921092
{'result': {'model': {'content': bounds}}},

0 commit comments

Comments
 (0)