Skip to content

Fixing desired capabilities and find element deprecation #2050

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 17 additions & 27 deletions dash/testing/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.action_chains import ActionChains

from selenium.common.exceptions import (
Expand Down Expand Up @@ -229,18 +228,17 @@ def take_snapshot(self, name):

self.driver.save_screenshot(f"{target}/{name}_{self.session_id}.png")

def find_element(self, selector):
"""find_element returns the first found element by the css `selector`
def find_element(self, selector, attribute="CSS_SELECTOR"):
"""find_element returns the first found element by the attribute `selector`
shortcut to `driver.find_element(By.CSS_SELECTOR, ...)`."""
return self.driver.find_element(By.CSS_SELECTOR, selector)

def find_elements(self, selector):
"""find_elements returns a list of all elements matching the css
`selector`.
return self.driver.find_element(getattr(By, attribute.upper()), selector)

def find_elements(self, selector, attribute="CSS_SELECTOR"):
"""find_elements returns a list of all elements matching the attribute
`selector`
shortcut to `driver.find_elements(By.CSS_SELECTOR, ...)`.
"""
return self.driver.find_elements(By.CSS_SELECTOR, selector)
return self.driver.find_elements(getattr(By, attribute.upper()), selector)

def _get_element(self, elem_or_selector):
if isinstance(elem_or_selector, str):
Expand Down Expand Up @@ -430,9 +428,8 @@ def _get_wd_options(self):
def _get_chrome(self):
options = self._get_wd_options()

capabilities = DesiredCapabilities.CHROME
capabilities["loggingPrefs"] = {"browser": "SEVERE"}
capabilities["goog:loggingPrefs"] = {"browser": "SEVERE"}
options.set_capability("loggingPrefs", {"browser": "SEVERE"})
options.set_capability("goog:loggingPrefs", {"browser": "SEVERE"})

if "DASH_TEST_CHROMEPATH" in os.environ:
options.binary_location = os.environ["DASH_TEST_CHROMEPATH"]
Expand All @@ -455,11 +452,10 @@ def _get_chrome(self):
chrome = (
webdriver.Remote(
command_executor=self._remote_url,
options=options,
desired_capabilities=capabilities,
options=options
)
if self._remote
else webdriver.Chrome(options=options, desired_capabilities=capabilities)
else webdriver.Chrome(options=options)
)

# https://bugs.chromium.org/p/chromium/issues/detail?id=696481
Expand All @@ -482,28 +478,22 @@ def _get_chrome(self):
def _get_firefox(self):
options = self._get_wd_options()

capabilities = DesiredCapabilities.FIREFOX
capabilities["loggingPrefs"] = {"browser": "SEVERE"}
capabilities["marionette"] = True
options.set_capability("loggingPrefs", {"browser": "SEVERE"})
options.set_capability("marionette", True)

# https://developer.mozilla.org/en-US/docs/Download_Manager_preferences
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.dir", self.download_path)
fp.set_preference("browser.download.folderList", 2)
fp.set_preference(
options.set_preference("browser.download.dir", self.download_path)
options.set_preference("browser.download.folderList", 2)
options.set_preference(
"browser.helperApps.neverAsk.saveToDisk",
"application/octet-stream", # this MIME is generic for binary
)
return (
webdriver.Remote(
command_executor=self._remote_url,
options=options,
desired_capabilities=capabilities,
)
if self._remote
else webdriver.Firefox(
firefox_profile=fp, options=options, capabilities=capabilities
)
else webdriver.Firefox(options=options)
)

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/dash_assets/test_dash_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def test_dada002_external_files_init(dash_duo):
(("//script[@src='{}']", x) for x in js_urls),
(("//link[@href='{}']", x) for x in css_urls),
):
dash_duo.driver.find_element_by_xpath(fmt.format(url))
dash_duo.find_element(fmt.format(url), attribute="XPATH")

assert (
dash_duo.find_element("#btn").value_of_css_property("height") == "18px"
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/devtools/test_devtools_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,4 @@ def create_an_alternative_response():
)

driver.get(dash_thread_server.url)
driver.find_element_by_id("alternative_id")
dash_br.find_element("alternative_id", attribute="ID")