Skip to content
This repository has been archived by the owner on May 8, 2020. It is now read-only.

Commit

Permalink
Accept a list of arguments as ignoreDefaultArgs option
Browse files Browse the repository at this point in the history
  • Loading branch information
miyakogi committed Sep 28, 2018
1 parent 4486db7 commit 7da3b91
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ History
## Version 0.0.26 (next version)

* Add `$PYPPETEER_NO_PROGRESS_BAR` environment variable
* `pyppeteer.defaultArgs` now accepts that help infer chromium command-line flags.
* `pyppeteer.launch()` argument `ignoreDefaultArgs` now accepts a list of flags to ignore.
* `Page.type()` now supports typing emoji
* `Page.pdf()` accepts a new argument `preferCSSPageSize`
* Add new option `defaultViewport` to `launch()` and `connect()`
Expand Down
49 changes: 33 additions & 16 deletions pyppeteer/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,17 @@ def __init__(self, options: Dict[str, Any] = None, # noqa: C901
if logLevel:
logging.getLogger('pyppeteer').setLevel(logLevel)

self.chromeArguments: List[str] = defaultArgs(options) if not ignoreDefaultArgs else args # noqa: E501
self.chromeArguments: List[str] = list()
if not ignoreDefaultArgs:
self.chromeArguments.extend(defaultArgs(options))
elif isinstance(ignoreDefaultArgs, list):
self.chromeArguments.extend(filter(
lambda arg: arg not in ignoreDefaultArgs,
defaultArgs(options),
))
else:
self.chromeArguments.extend(args)

self.temporaryUserDataDir: Optional[str] = None

if not any(arg for arg in self.chromeArguments
Expand Down Expand Up @@ -269,8 +279,10 @@ async def launch(options: dict = None, **kwargs: Any) -> Browser:
* ``args`` (List[str]): Additional arguments (flags) to pass to the browser
process.
* ``ignoreDefaultArgs`` (bool): Do not use pyppeteer's default args. This
is dangerous option; use with care.
* ``ignoreDefaultArgs`` (bool or List[str]): If ``True``, do not use
:func:`~pyppeteer.defaultArgs`. If list is given, then filter out given
default arguments. Dangerous option; use with care. Defaults to
``False``.
* ``handleSIGINT`` (bool): Close the browser process on Ctrl+C. Defaults to
``True``.
* ``handleSIGTERM`` (bool): Close the browser process on SIGTERM. Defaults
Expand All @@ -292,6 +304,23 @@ async def launch(options: dict = None, **kwargs: Any) -> Browser:
* ``loop`` (asyncio.AbstractEventLoop): Event loop (**experimental**).
* ``appMode`` (bool): Deprecated.
This function combines 3 steps:
1. Infer a set of flags to launch chromium with using
:func:`~pyppeteer.defaultArgs`.
2. Launch browser and start managing its process according to the
``executablePath``, ``handleSIGINT``, ``dumpio``, and other options.
3. Create an instance of :class:`~pyppeteer.browser.Browser` class and
initialize it with ``defaultViewport``, ``slowMo``, and
``ignoreHTTPSErrors``.
``ignoreDefaultArgs`` option can be used to customize behavior on the (1)
step. For example, to filter out ``--mute-audio`` from default arguments:
.. code::
browser = await launch(ignoreDefaultArgs=['--mute-audio'])
.. note::
Pyppeteer can also be used to control the Chrome browser, but it works
best with the version of Chromium it is bundled with. There is no
Expand Down Expand Up @@ -362,7 +391,7 @@ def executablePath() -> str:


def defaultArgs(options: Dict = None, **kwargs: Any) -> List[str]: # noqa: C901,E501
"""Get the default flags the chromium will be launched.
"""Get the default flags the chromium will be launched with.
``options`` or keyword arguments are set of configurable options to set on
the browser. Can have the following fields:
Expand All @@ -375,18 +404,6 @@ def defaultArgs(options: Dict = None, **kwargs: Any) -> List[str]: # noqa: C901
* ``userDataDir`` (str): Path to a User Data Directory.
* ``devtools`` (bool): Whether to auto-open DevTools panel for each tab. If
this option is ``True``, the ``headless`` option will be set ``False``.
``pyppeteer.defaultArgs`` can be used to turn off some flags that pyppeteer
usually launches chromium with:
.. code:: python
customArgs = [arg for arg in pyppeteer.defaultArgs()
if arg != '--mute-audio']
browser = await pyppeteer.launch(
ignoreDefaultArgs=True,
args=customArgs,
)
"""
options = merge_dict(options, kwargs)
devtools = options.get('devtools', False)
Expand Down
1 change: 1 addition & 0 deletions spell.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ html
http
https
iframe
ignoredefaultargs
innertext
internetdisconnected
ip
Expand Down
14 changes: 13 additions & 1 deletion tests/test_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from syncer import sync
import websockets

from pyppeteer import connect, launch, executablePath
from pyppeteer import connect, launch, executablePath, defaultArgs
from pyppeteer.chromium_downloader import chromium_executable, current_platform
from pyppeteer.errors import NetworkError
from pyppeteer.launcher import Launcher
Expand Down Expand Up @@ -69,6 +69,18 @@ def test_args(self):
self.check_default_args(launcher)
self.assertIn('--some-args', launcher.chromeArguments)

def test_filter_ignore_default_args(self):
_defaultArgs = defaultArgs()
options = deepcopy(DEFAULT_OPTIONS)
launcher = Launcher(
options,
# ignore first and third default arguments
ignoreDefaultArgs=[_defaultArgs[0], _defaultArgs[2]],
)
self.assertNotIn(_defaultArgs[0], launcher.cmd)
self.assertIn(_defaultArgs[1], launcher.cmd)
self.assertNotIn(_defaultArgs[2], launcher.cmd)

def test_user_data_dir(self):
launcher = Launcher({'args': ['--user-data-dir=/path/to/profile']})
self.check_default_args(launcher)
Expand Down

0 comments on commit 7da3b91

Please sign in to comment.