Skip to content

Commit

Permalink
Merge pull request #818 from plotly/testing-download
Browse files Browse the repository at this point in the history
✨ add download support for both browser
  • Loading branch information
byronz authored Jul 12, 2019
2 parents 03c3474 + 28fee35 commit 5132fc5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
53 changes: 45 additions & 8 deletions dash/testing/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ def __init__(
headless=False,
options=None,
remote=None,
download_path=None,
wait_timeout=10,
):
self._browser = browser.lower()
self._headless = headless
self._options = options
self._download_path = download_path
self._wait_timeout = wait_timeout

self._driver = self.get_webdriver(remote)
Expand All @@ -55,6 +57,10 @@ def __init__(
)
self.percy_runner.initialize_build()

logger.debug("initialize browser with arguments")
logger.debug(" headless => %s", self._headless)
logger.debug(" download_path => %s", self._download_path)

def __enter__(self):
return self

Expand Down Expand Up @@ -273,9 +279,38 @@ def _get_chrome(self):
if "DASH_TEST_CHROMEPATH" in os.environ:
options.binary_location = os.environ["DASH_TEST_CHROMEPATH"]

options.add_experimental_option(
"prefs",
{
"download.default_directory": self.download_path,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": False,
"safebrowsing.disable_download_protection": True,
},
)

chrome = webdriver.Chrome(
options=options, desired_capabilities=capabilities
)

# https://bugs.chromium.org/p/chromium/issues/detail?id=696481
if self._headless:
# pylint: disable=protected-access
chrome.command_executor._commands["send_command"] = (
"POST",
"/session/$sessionId/chromium/send_command",
)
params = {
"cmd": "Page.setDownloadBehavior",
"params": {
"behavior": "allow",
"downloadPath": self.download_path,
},
}
res = chrome.execute("send_command", params)
logger.debug("enabled headless download returns %s", res)

chrome.set_window_position(0, 0)
return chrome

Expand All @@ -288,16 +323,14 @@ def _get_firefox(self):

# https://developer.mozilla.org/en-US/docs/Download_Manager_preferences
fp = webdriver.FirefoxProfile()

# this will be useful if we wanna test download csv or other data
# files with selenium
# TODO this could be replaced with a tmpfixture from pytest too
fp.set_preference("browser.download.dir", "/tmp")
fp.set_preference("browser.download.dir", self.download_path)
fp.set_preference("browser.download.folderList", 2)
fp.set_preference("browser.download.manager.showWhenStarting", False)

fp.set_preference(
"browser.helperApps.neverAsk.saveToDisk",
"application/octet-stream", # this MIME is generic for binary
)
return webdriver.Firefox(
fp, options=options, capabilities=capabilities
firefox_profile=fp, options=options, capabilities=capabilities
)

@staticmethod
Expand Down Expand Up @@ -363,3 +396,7 @@ def server_url(self, value):
"""
self._url = value
self.wait_for_page()

@property
def download_path(self):
return self._download_path
6 changes: 4 additions & 2 deletions dash/testing/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,23 @@ def dash_process_server():


@pytest.fixture
def dash_br(request):
def dash_br(request, tmpdir):
with Browser(
browser=request.config.getoption("webdriver"),
headless=request.config.getoption("headless"),
options=request.config.hook.pytest_setup_options(),
download_path=tmpdir.mkdir('download').strpath
) as browser:
yield browser


@pytest.fixture
def dash_duo(request, dash_thread_server):
def dash_duo(request, dash_thread_server, tmpdir):
with DashComposite(
dash_thread_server,
browser=request.config.getoption("webdriver"),
headless=request.config.getoption("headless"),
options=request.config.hook.pytest_setup_options(),
download_path=tmpdir.mkdir('download').strpath
) as dc:
yield dc

0 comments on commit 5132fc5

Please sign in to comment.