Skip to content

Commit 49fab85

Browse files
authored
Merge pull request #2050 from MrTeale/desired-capabilities-deprecation
Fixing desired capabilities and find element deprecation
2 parents b59ac20 + f601775 commit 49fab85

File tree

4 files changed

+35
-34
lines changed

4 files changed

+35
-34
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ This project adheres to [Semantic Versioning](https://semver.org/).
1111
`dangerously_allow_html=True` + `mathjax=True` works in some cases, and in some cases not.
1212
- [#2047](https://github.com/plotly/dash/pull/2047) Fix bug [#1979](https://github.com/plotly/dash/issues/1979) in which `DASH_DEBUG` as enviroment variable gets ignored.
1313

14+
### Changed
15+
16+
- [#2050](https://github.com/plotly/dash/pull/2050) Changed `find_element` and `find_elements` to accept an `attribute` argument that aligns with Selenium's `By` class, allowing you to search elements by other attributes. Default value is `CSS_SELECTOR` to maintain backwards compatibility with previous `find_elements`.
17+
1418
## [2.4.1] - 2022-05-11
1519

1620
### Fixed

dash/testing/browser.py

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from selenium.webdriver.common.by import By
1313
from selenium.webdriver.support.wait import WebDriverWait
1414
from selenium.webdriver.common.keys import Keys
15-
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
1615
from selenium.webdriver.common.action_chains import ActionChains
1716

1817
from selenium.common.exceptions import (
@@ -229,18 +228,27 @@ def take_snapshot(self, name):
229228

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

232-
def find_element(self, selector):
233-
"""find_element returns the first found element by the css `selector`
234-
shortcut to `driver.find_element(By.CSS_SELECTOR, ...)`."""
235-
return self.driver.find_element(By.CSS_SELECTOR, selector)
236-
237-
def find_elements(self, selector):
238-
"""find_elements returns a list of all elements matching the css
239-
`selector`.
231+
def find_element(self, selector, attribute="CSS_SELECTOR"):
232+
"""find_element returns the first found element by the attribute `selector`
233+
shortcut to `driver.find_element(By.CSS_SELECTOR, ...)`.
234+
args:
235+
- attribute: the attribute type to search for, aligns with the Selenium
236+
API's `By` class. default "CSS_SELECTOR"
237+
valid values: "CSS_SELECTOR", "ID", "NAME", "TAG_NAME",
238+
"CLASS_NAME", "LINK_TEXT", "PARTIAL_LINK_TEXT", "XPATH"
239+
"""
240+
return self.driver.find_element(getattr(By, attribute.upper()), selector)
240241

241-
shortcut to `driver.find_elements(By.CSS_SELECTOR, ...)`.
242+
def find_elements(self, selector, attribute="CSS_SELECTOR"):
243+
"""find_elements returns a list of all elements matching the attribute
244+
`selector`. Shortcut to `driver.find_elements(By.CSS_SELECTOR, ...)`.
245+
args:
246+
- attribute: the attribute type to search for, aligns with the Selenium
247+
API's `By` class. default "CSS_SELECTOR"
248+
valid values: "CSS_SELECTOR", "ID", "NAME", "TAG_NAME",
249+
"CLASS_NAME", "LINK_TEXT", "PARTIAL_LINK_TEXT", "XPATH"
242250
"""
243-
return self.driver.find_elements(By.CSS_SELECTOR, selector)
251+
return self.driver.find_elements(getattr(By, attribute.upper()), selector)
244252

245253
def _get_element(self, elem_or_selector):
246254
if isinstance(elem_or_selector, str):
@@ -430,9 +438,8 @@ def _get_wd_options(self):
430438
def _get_chrome(self):
431439
options = self._get_wd_options()
432440

433-
capabilities = DesiredCapabilities.CHROME
434-
capabilities["loggingPrefs"] = {"browser": "SEVERE"}
435-
capabilities["goog:loggingPrefs"] = {"browser": "SEVERE"}
441+
options.set_capability("loggingPrefs", {"browser": "SEVERE"})
442+
options.set_capability("goog:loggingPrefs", {"browser": "SEVERE"})
436443

437444
if "DASH_TEST_CHROMEPATH" in os.environ:
438445
options.binary_location = os.environ["DASH_TEST_CHROMEPATH"]
@@ -453,13 +460,9 @@ def _get_chrome(self):
453460
options.add_argument("--remote-debugging-port=9222")
454461

455462
chrome = (
456-
webdriver.Remote(
457-
command_executor=self._remote_url,
458-
options=options,
459-
desired_capabilities=capabilities,
460-
)
463+
webdriver.Remote(command_executor=self._remote_url, options=options)
461464
if self._remote
462-
else webdriver.Chrome(options=options, desired_capabilities=capabilities)
465+
else webdriver.Chrome(options=options)
463466
)
464467

465468
# https://bugs.chromium.org/p/chromium/issues/detail?id=696481
@@ -482,28 +485,22 @@ def _get_chrome(self):
482485
def _get_firefox(self):
483486
options = self._get_wd_options()
484487

485-
capabilities = DesiredCapabilities.FIREFOX
486-
capabilities["loggingPrefs"] = {"browser": "SEVERE"}
487-
capabilities["marionette"] = True
488+
options.set_capability("loggingPrefs", {"browser": "SEVERE"})
489+
options.set_capability("marionette", True)
488490

489-
# https://developer.mozilla.org/en-US/docs/Download_Manager_preferences
490-
fp = webdriver.FirefoxProfile()
491-
fp.set_preference("browser.download.dir", self.download_path)
492-
fp.set_preference("browser.download.folderList", 2)
493-
fp.set_preference(
491+
options.set_preference("browser.download.dir", self.download_path)
492+
options.set_preference("browser.download.folderList", 2)
493+
options.set_preference(
494494
"browser.helperApps.neverAsk.saveToDisk",
495495
"application/octet-stream", # this MIME is generic for binary
496496
)
497497
return (
498498
webdriver.Remote(
499499
command_executor=self._remote_url,
500500
options=options,
501-
desired_capabilities=capabilities,
502501
)
503502
if self._remote
504-
else webdriver.Firefox(
505-
firefox_profile=fp, options=options, capabilities=capabilities
506-
)
503+
else webdriver.Firefox(options=options)
507504
)
508505

509506
@staticmethod

tests/integration/dash_assets/test_dash_assets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def test_dada002_external_files_init(dash_duo):
114114
(("//script[@src='{}']", x) for x in js_urls),
115115
(("//link[@href='{}']", x) for x in css_urls),
116116
):
117-
dash_duo.driver.find_element_by_xpath(fmt.format(url))
117+
dash_duo.find_element(fmt.format(url), attribute="XPATH")
118118

119119
assert (
120120
dash_duo.find_element("#btn").value_of_css_property("height") == "18px"

tests/integration/devtools/test_devtools_ui.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,4 @@ def create_an_alternative_response():
256256
)
257257

258258
driver.get(dash_thread_server.url)
259-
driver.find_element_by_id("alternative_id")
259+
dash_br.find_element("#alternative_id")

0 commit comments

Comments
 (0)