Skip to content

Commit defbf4c

Browse files
authored
Merge pull request #79 from mozilla/sl/frequently-used-context-actions
Frequently Used Context Actions
2 parents ac02282 + 1ce96ac commit defbf4c

10 files changed

+220
-50
lines changed

modules/browser_object_context_menu.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@ def get_context_item(self, item: str) -> WebElement:
1616
"""
1717
with self.driver.context(self.driver.CONTEXT_CHROME):
1818
return self.get_element(item)
19+
20+
def click_context_item(self, context_element: WebElement) -> BasePage:
21+
"""
22+
Clicks the context item.
23+
"""
24+
with self.driver.context(self.driver.CONTEXT_CHROME):
25+
context_element.click()

modules/browser_object_navigation.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,10 @@ def wait_for_download_animation_finish(
142142
"""
143143
Waits for the download button to finish playing the animation for downloading to local computer
144144
"""
145-
try:
146-
self.wait.until(
147-
lambda _: downloads_button.get_attribute("notification") == "finish"
148-
)
149-
except TimeoutException:
150-
logging.warning("Animation did not finish or did not play.")
145+
with self.driver.context(self.driver.CONTEXT_CHROME):
146+
try:
147+
self.wait.until(
148+
lambda _: downloads_button.get_attribute("notification") == "finish"
149+
)
150+
except TimeoutException:
151+
logging.warning("Animation did not finish or did not play.")

modules/data/context_menu.components.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,23 @@
44
"selectorData": "context-searchselect",
55
"strategy": "id",
66
"groups": []
7+
},
8+
9+
"context-menu-inspect": {
10+
"selectorData": "context-inspect",
11+
"strategy": "id",
12+
"groups": []
13+
},
14+
15+
"context-menu-take-screenshot": {
16+
"selectorData": "context-take-screenshot",
17+
"strategy": "id",
18+
"groups": []
19+
},
20+
21+
"context-menu-save-page-as": {
22+
"selectorData": "context-savepage",
23+
"strategy": "id",
24+
"groups": []
725
}
826
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"title-header": {
3+
"selectorData": "h1",
4+
"strategy": "tag",
5+
"groups": []
6+
},
7+
8+
"inspect-menu-horizontal-splitter": {
9+
"selectorData": "devtools-horizontal-splitter",
10+
"strategy": "class",
11+
"groups": []
12+
},
13+
14+
"take-screenshot-box": {
15+
"selectorData": "screenshotsPagePanel",
16+
"strategy": "id",
17+
"groups": [
18+
"doNotCache"
19+
]
20+
}
21+
22+
}

modules/page_base.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,15 +398,19 @@ def context_click_element(self, element: WebElement) -> Page:
398398
self.actions.context_click(element).perform()
399399
return self
400400

401-
def hide_popup(self, context_menu: str) -> Page:
401+
def hide_popup(self, context_menu: str, chrome=False) -> Page:
402402
"""
403403
Given the ID of the context menu, it will dismiss the menu.
404404
405405
For example, the tab context menu corresponds to the id of tabContextMenu. Usage would be: tabs.hide_popup("tabContextMenu")
406406
"""
407407
script = f"""document.querySelector("#{context_menu}").hidePopup();
408408
"""
409-
self.driver.execute_script(script)
409+
if chrome:
410+
with self.driver.context(self.driver.CONTEXT_CHROME):
411+
self.driver.execute_script(script)
412+
else:
413+
self.driver.execute_script(script)
410414

411415
def hide_popup_by_class(self, class_name: str) -> None:
412416
"""
@@ -422,12 +426,16 @@ def hide_popup_by_class(self, class_name: str) -> None:
422426
"""
423427
self.driver.execute_script(script)
424428

425-
def hide_popup_by_child_node(self, node: WebElement) -> Page:
429+
def hide_popup_by_child_node(self, node: WebElement, chrome=False) -> Page:
426430
script = """var element = arguments[0].parentNode;
427431
if (element && element.hidePopup) {
428432
element.hidePopup();
429433
}"""
430-
self.driver.execute_script(script, node)
434+
if chrome:
435+
with self.driver.context(self.driver.CONTEXT_CHROME):
436+
self.driver.execute_script(script, node)
437+
else:
438+
self.driver.execute_script(script, node)
431439

432440
@property
433441
def loaded(self):

modules/page_object.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
from modules.page_object_addons_mozilla_org import *
99
from modules.page_object_autofill_credit_card import *
1010
from modules.page_object_autofill_test_basic import *
11+
from modules.page_object_example_page import *
1112
from modules.page_object_google_search import *
1213
from modules.page_object_wiki_firefox_logo import *

modules/page_object_example_page.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from modules.page_base import BasePage
2+
3+
4+
class ExamplePage(BasePage):
5+
"""
6+
Page Object Model for the website https://example.com/
7+
"""
8+
9+
URL_TEMPLATE = "https://example.com/"
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import platform
2+
from time import sleep
3+
4+
import pytest
5+
from selenium.webdriver import Firefox
6+
7+
from modules.browser_object import ContextMenu, Navigation
8+
from modules.page_object import ExamplePage
9+
from modules.util import BrowserActions, Utilities
10+
11+
12+
@pytest.mark.unstable
13+
def test_save_page_as(driver: Firefox):
14+
"""
15+
C2637623.1: save page as
16+
"""
17+
try:
18+
from pynput.keyboard import Key
19+
except ModuleNotFoundError:
20+
pytest.skip("Could not load pynput")
21+
# create objects
22+
context_menu = ContextMenu(driver)
23+
driver.get("https://example.com")
24+
example_page = ExamplePage(driver)
25+
nav = Navigation(driver)
26+
util = Utilities()
27+
ba = BrowserActions(driver)
28+
29+
# right click something that is not a hyperlink
30+
title_header = example_page.get_element("title-header")
31+
context_menu.context_click_element(title_header)
32+
33+
save_page_as = context_menu.get_context_item("context-menu-save-page-as")
34+
context_menu.click_context_item(save_page_as)
35+
context_menu.hide_popup_by_child_node(save_page_as, chrome=True)
36+
37+
downloads_button = nav.get_download_button()
38+
39+
# short sleep to ensure menu is shown
40+
sleep(0.5)
41+
42+
# perform key presses to save the file
43+
this_platform = platform.system()
44+
if this_platform == "Linux":
45+
ba.controller.press(Key.alt)
46+
ba.controller.press(Key.tab)
47+
ba.controller.release(Key.tab)
48+
ba.controller.release(Key.alt)
49+
50+
ba.controller.press(Key.alt)
51+
ba.controller.press(Key.tab)
52+
ba.controller.release(Key.tab)
53+
ba.controller.release(Key.alt)
54+
55+
ba.key_press_release(Key.tab)
56+
57+
ba.key_press_release(Key.tab)
58+
59+
# Press and release the Enter key
60+
ba.key_press_release(Key.enter)
61+
62+
# Wait for the animation to complete
63+
nav.wait_for_download_animation_finish(downloads_button)
64+
65+
# verify and delete downloaded file
66+
saved_image_location = util.get_saved_file_path("Example Domain.html")
67+
util.check_file_path_validility(saved_image_location)
68+
util.remove_file(saved_image_location)
69+
70+
71+
def test_take_screenshot(driver: Firefox):
72+
"""
73+
C2637623.2: take screenshot works from context menu
74+
"""
75+
# create objects
76+
context_menu = ContextMenu(driver)
77+
driver.get("https://example.com")
78+
example_page = ExamplePage(driver)
79+
80+
# ensure that the screenshot is not present
81+
example_page.element_does_not_exist("take-screenshot-box")
82+
83+
# right click the header
84+
title_header = example_page.get_element("title-header")
85+
context_menu.context_click_element(title_header)
86+
87+
# context click the screenshot option and verify its not hidden
88+
take_screenshot = context_menu.get_context_item("context-menu-take-screenshot")
89+
context_menu.click_context_item(take_screenshot)
90+
context_menu.hide_popup_by_child_node(take_screenshot, chrome=True)
91+
92+
with driver.context(driver.CONTEXT_CHROME):
93+
screenshot_box = example_page.get_element("take-screenshot-box")
94+
assert screenshot_box.get_attribute("hidden") is None
95+
96+
97+
def test_inspect(driver: Firefox):
98+
"""
99+
C2637623.3: inspect works from context menu
100+
"""
101+
# create objects
102+
context_menu = ContextMenu(driver)
103+
driver.get("https://example.com")
104+
example_page = ExamplePage(driver)
105+
106+
# right click something that is not a hyperlink
107+
title_header = example_page.get_element("title-header")
108+
context_menu.context_click_element(title_header)
109+
110+
inspect_option = context_menu.get_context_item("context-menu-inspect")
111+
context_menu.click_context_item(inspect_option)
112+
context_menu.hide_popup_by_child_node(inspect_option, chrome=True)
113+
114+
# find an element present in the dev tools
115+
with driver.context(driver.CONTEXT_CHROME):
116+
example_page.element_exists("inspect-menu-horizontal-splitter")

tests/menus/test_hyperlink_context_menu.py

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@ def test_open_link_in_new_window(driver: Firefox):
2121
hyperlink_context.context_click_element(hyperlink)
2222

2323
# click on the open in new window option
24-
with driver.context(driver.CONTEXT_CHROME):
25-
open_in_new_window = hyperlink_context.get_context_item(
26-
"context-menu-open-in-new-window"
27-
)
28-
open_in_new_window.click()
2924

30-
hyperlink_context.hide_popup("contentAreaContextMenu")
25+
open_in_new_window = hyperlink_context.get_context_item(
26+
"context-menu-open-in-new-window"
27+
)
28+
hyperlink_context.click_context_item(open_in_new_window)
29+
hyperlink_context.hide_popup("contentAreaContextMenu", chrome=True)
3130

3231
# verify there are two instances (two windows)
3332
tabs.wait_for_num_tabs(2)
@@ -53,13 +52,11 @@ def test_open_link_in_private_window(driver: Firefox):
5352
hyperlink_context.context_click_element(hyperlink)
5453

5554
# click on the open in new window option
56-
with driver.context(driver.CONTEXT_CHROME):
57-
open_in_new_window = hyperlink_context.get_context_item(
58-
"context-menu-open-in-private-window"
59-
)
60-
open_in_new_window.click()
61-
62-
hyperlink_context.hide_popup("contentAreaContextMenu")
55+
open_in_new_window = hyperlink_context.get_context_item(
56+
"context-menu-open-in-private-window"
57+
)
58+
hyperlink_context.click_context_item(open_in_new_window)
59+
hyperlink_context.hide_popup("contentAreaContextMenu", chrome=True)
6360

6461
# verify there are two instances (two windows)
6562
tabs.wait_for_num_tabs(2)
@@ -89,13 +86,9 @@ def test_copy_link(driver: Firefox):
8986
hyperlink_context.context_click_element(hyperlink)
9087

9188
# click on the open in new window option
92-
with driver.context(driver.CONTEXT_CHROME):
93-
open_in_new_window = hyperlink_context.get_context_item(
94-
"context-menu-copy-link"
95-
)
96-
open_in_new_window.click()
97-
98-
hyperlink_context.hide_popup("contentAreaContextMenu")
89+
open_in_new_window = hyperlink_context.get_context_item("context-menu-copy-link")
90+
hyperlink_context.click_context_item(open_in_new_window)
91+
hyperlink_context.hide_popup("contentAreaContextMenu", chrome=True)
9992

10093
# open a new tab
10194
tabs.new_tab_by_button()

tests/menus/test_image_context_menu_actions.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ def test_open_image_in_new_tab(driver: Firefox):
2626
wiki_image_page.context_click_element(image_logo)
2727

2828
# open in a new tab
29-
with driver.context(driver.CONTEXT_CHROME):
30-
open_in_new_tab = image_context_menu.get_context_item(
31-
"context-menu-open-image-in-new-tab"
32-
)
33-
open_in_new_tab.click()
34-
wiki_image_page.hide_popup_by_child_node(open_in_new_tab)
29+
open_in_new_tab = image_context_menu.get_context_item(
30+
"context-menu-open-image-in-new-tab"
31+
)
32+
image_context_menu.click_context_item(open_in_new_tab)
33+
wiki_image_page.hide_popup_by_child_node(open_in_new_tab, chrome=True)
3534

3635
# switch to the second tab and verify the URL
3736
tabs.wait_for_num_tabs(2)
@@ -64,12 +63,9 @@ def test_save_image_as(driver: Firefox):
6463
wiki_image_page.context_click_element(image_logo)
6564

6665
# save it
67-
with driver.context(driver.CONTEXT_CHROME):
68-
save_image_as = image_context_menu.get_context_item(
69-
"context-menu-save-image-as"
70-
)
71-
save_image_as.click()
72-
wiki_image_page.hide_popup_by_child_node(save_image_as)
66+
save_image_as = image_context_menu.get_context_item("context-menu-save-image-as")
67+
image_context_menu.click_context_item(save_image_as)
68+
wiki_image_page.hide_popup_by_child_node(save_image_as, chrome=True)
7369

7470
# create the pynput controller
7571
downloads_button = nav.get_download_button()
@@ -97,8 +93,7 @@ def test_save_image_as(driver: Firefox):
9793
ba.key_press_release(Key.enter)
9894

9995
# Wait for the animation to complete
100-
with driver.context(driver.CONTEXT_CHROME):
101-
nav.wait_for_download_animation_finish(downloads_button)
96+
nav.wait_for_download_animation_finish(downloads_button)
10297

10398
saved_image_location = util.get_saved_file_path("Firefox_logo,_2019.svg.png")
10499

@@ -111,6 +106,7 @@ def test_copy_image_link(driver: Firefox):
111106
"""
112107
C2637622.3: copy an image link and verify its correct
113108
"""
109+
# create objs
114110
nav = Navigation(driver).open()
115111
wiki_image_page = WikiFirefoxLogo(driver).open()
116112
image_context_menu = ImageContextMenu(driver)
@@ -124,12 +120,11 @@ def test_copy_image_link(driver: Firefox):
124120
wiki_image_page.context_click_element(image_logo)
125121

126122
# copy the link
127-
with driver.context(driver.CONTEXT_CHROME):
128-
copy_image_link = image_context_menu.get_context_item(
129-
"context-menu-copy-image-link"
130-
)
131-
copy_image_link.click()
132-
wiki_image_page.hide_popup_by_child_node(copy_image_link)
123+
copy_image_link = image_context_menu.get_context_item(
124+
"context-menu-copy-image-link"
125+
)
126+
image_context_menu.click_context_item(copy_image_link)
127+
wiki_image_page.hide_popup_by_child_node(copy_image_link, chrome=True)
133128

134129
# open a new tab
135130
tabs.new_tab_by_button()

0 commit comments

Comments
 (0)