コード例 #1
0
 def setUp(self):
     self.headless_options = [
         '--headless',
         '--hide-scrollbars',
         '--mute-audio',
     ]
     if current_platform().startswith('win'):
         self.headless_options.append('--disable-gpu')
コード例 #2
0
    async def test_browser_crash_send(self):
        browser = await launch(args=['--no-sandbox'])
        page = await browser.newPage()
        await page.goto('about:blank')
        await page.querySelector("title")
        browser.process.terminate()
        browser.process.wait()

        if current_platform().startswith('win'):
            # wait for terminating browser process
            await asyncio.sleep(1)

        with self.assertRaises(NetworkError):
            await page.querySelector("title")
        with self.assertRaises(NetworkError):
            with self.assertLogs('pyppeteer', logging.ERROR):
                await page.querySelector("title")
        with self.assertRaises(ConnectionError):
            await browser.newPage()
コード例 #3
0
ファイル: launcher.py プロジェクト: williamzhanggg/pyppeteer
def defaultArgs(options: Dict = None,
                **kwargs: Any) -> List[str]:  # noqa: C901,E501
    """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:

    * ``headless`` (bool): Whether to run browser in headless mode. Defaults to
      ``True`` unless the ``devtools`` option is ``True``.
    * ``args`` (List[str]): Additional arguments to pass to the browser
      instance. The list of chromium flags can be found
      `here <http://peter.sh/experiments/chromium-command-line-switches/>`__.
    * ``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``.
    """
    options = merge_dict(options, kwargs)
    devtools = options.get('devtools', False)
    headless = options.get('headless', not devtools)
    args = options.get('args', list())
    userDataDir = options.get('userDataDir')
    chromeArguments = copy(DEFAULT_ARGS)

    if userDataDir:
        chromeArguments.append(f'--user-data-dir={userDataDir}')
    if devtools:
        chromeArguments.append('--auto-open-devtools-for-tabs')
    if headless:
        chromeArguments.extend((
            '--headless',
            '--hide-scrollbars',
            '--mute-audio',
        ))
        if current_platform().startswith('win'):
            chromeArguments.append('--disable-gpu')

    if all(map(lambda arg: arg.startswith('-'), args)):  # type: ignore
        chromeArguments.append('about:blank')
    chromeArguments.extend(args)

    return chromeArguments
コード例 #4
0
class TestLogLevel(unittest.TestCase):
    def setUp(self):
        self.logger = logging.getLogger('pyppeteer')
        self.mock = mock.Mock()
        self._orig_stderr = sys.stderr.write
        sys.stderr.write = self.mock

    def tearDown(self):
        sys.stderr.write = self._orig_stderr
        logging.getLogger('pyppeteer').setLevel(logging.NOTSET)

    @sync
    async def test_level_default(self):
        browser = await launch(args=['--no-sandbox'])
        await browser.close()

        self.assertTrue(self.logger.isEnabledFor(logging.WARN))
        self.assertFalse(self.logger.isEnabledFor(logging.INFO))
        self.assertFalse(self.logger.isEnabledFor(logging.DEBUG))
        self.mock.assert_not_called()

    @unittest.skipIf(current_platform().startswith('win'), 'error on windows')
    @sync
    async def test_level_info(self):
        browser = await launch(args=['--no-sandbox'], logLevel=logging.INFO)
        await browser.close()

        self.assertTrue(self.logger.isEnabledFor(logging.WARN))
        self.assertTrue(self.logger.isEnabledFor(logging.INFO))
        self.assertFalse(self.logger.isEnabledFor(logging.DEBUG))

        self.assertIn('listening on', self.mock.call_args_list[0][0][0])

    @unittest.skipIf(current_platform().startswith('win'), 'error on windows')
    @sync
    async def test_level_debug(self):
        browser = await launch(args=['--no-sandbox'], logLevel=logging.DEBUG)
        await browser.close()

        self.assertTrue(self.logger.isEnabledFor(logging.WARN))
        self.assertTrue(self.logger.isEnabledFor(logging.INFO))
        self.assertTrue(self.logger.isEnabledFor(logging.DEBUG))

        self.assertIn('listening on', self.mock.call_args_list[0][0][0])
        if self.mock.call_args_list[1][0][0] == '\n':
            # python < 3.7.3
            self.assertIn('SEND', self.mock.call_args_list[2][0][0])
            self.assertIn('RECV', self.mock.call_args_list[4][0][0])
        else:
            self.assertIn('SEND', self.mock.call_args_list[1][0][0])
            self.assertIn('RECV', self.mock.call_args_list[2][0][0])

    @unittest.skipIf(current_platform().startswith('win'), 'error on windows')
    @sync
    async def test_connect_debug(self):
        browser = await launch(args=['--no-sandbox'])
        browser2 = await connect(
            browserWSEndpoint=browser.wsEndpoint,
            logLevel=logging.DEBUG,
        )
        page = await browser2.newPage()
        await page.close()
        await browser2.disconnect()
        await browser.close()

        self.assertTrue(self.logger.isEnabledFor(logging.WARN))
        self.assertTrue(self.logger.isEnabledFor(logging.INFO))
        self.assertTrue(self.logger.isEnabledFor(logging.DEBUG))

        self.assertIn('SEND', self.mock.call_args_list[0][0][0])
        self.assertIn('RECV', self.mock.call_args_list[2][0][0])