예제 #1
0
    async def emulateViewport(self, viewport: dict) -> bool:
        """Evaluate viewport."""
        options = dict()
        mobile = viewport.get('isMobile', False)
        options['mobile'] = mobile
        if 'width' in viewport:
            options['width'] = helper.get_positive_int(viewport, 'width')
        if 'height' in viewport:
            options['height'] = helper.get_positive_int(viewport, 'height')

        options['deviceScaleFactor'] = viewport.get('deviceScaleFactor', 1)
        if viewport.get('isLandscape'):
            options['screenOrientation'] = {'angle': 90,
                                            'type': 'landscapePrimary'}
        else:
            options['screenOrientation'] = {'angle': 0,
                                            'type': 'portraitPrimary'}
        hasTouch = viewport.get('hasTouch', False)

        await self._client.send('Emulation.setDeviceMetricsOverride', options)
        await self._client.send('Emulation.setTouchEmulationEnabled', {
            'enabled': hasTouch,
            'configuration': 'mobile' if mobile else 'desktop'
        })

        reloadNeeded = (self._emulatingMobile != mobile or
                        self._hasTouch != hasTouch)

        self._emulatingMobile = mobile
        self._hasTouch = hasTouch
        return reloadNeeded
    async def emulateViewport(self, viewport: dict) -> bool:
        """Evaluate viewport."""
        options = dict()
        mobile = viewport.get('isMobile', False)
        options['mobile'] = mobile
        if 'width' in viewport:
            options['width'] = helper.get_positive_int(viewport, 'width')
        if 'height' in viewport:
            options['height'] = helper.get_positive_int(viewport, 'height')

        options['deviceScaleFactor'] = viewport.get('deviceScaleFactor', 1)
        if viewport.get('isLandscape'):
            options['screenOrientation'] = {'angle': 90,
                                            'type': 'landscapePrimary'}
        else:
            options['screenOrientation'] = {'angle': 0,
                                            'type': 'portraitPrimary'}
        hasTouch = viewport.get('hasTouch', False)

        await self._client.send('Emulation.setDeviceMetricsOverride', options)
        await self._client.send('Emulation.setTouchEmulationEnabled', {
            'enabled': hasTouch,
            'configuration': 'mobile' if mobile else 'desktop'
        })

        reloadNeeded = (self._emulatingMobile != mobile or
                        self._hasTouch != hasTouch)

        self._emulatingMobile = mobile
        self._hasTouch = hasTouch
        return reloadNeeded
예제 #3
0
    async def emulateViewport(self, client: CDPSession, viewport: dict
                              ) -> bool:
        """Evaluate viewport."""
        options = dict()
        mobile = viewport.get('isMobile', False)
        options['mobile'] = mobile
        if 'width' in viewport:
            options['width'] = helper.get_positive_int(viewport, 'width')
        if 'height' in viewport:
            options['height'] = helper.get_positive_int(viewport, 'height')

        options['deviceScaleFactor'] = viewport.get('deviceScaleFactor', 1)
        if viewport.get('isLandscape'):
            options['screenOrientation'] = {'angle': 90,
                                            'type': 'landscapePrimary'}
        else:
            options['screenOrientation'] = {'angle': 0,
                                            'type': 'portraitPrimary'}

        await self._client.send('Emulation.setDeviceMetricsOverride', options)
        await self._client.send('Emulation.setTouchEmulationEnabled', {
            'enabled': viewport.get('hasTouch', False),
            'configuration': 'mobile' if mobile else 'desktop'
        })

        injectedTouchEventsFunction = '''
function injectedTouchEventsFunction() {
  const touchEvents = ['ontouchstart', 'ontouchend', 'ontouchmove', 'ontouchcancel'];
  const recepients = [window.__proto__, document.__proto__];
  for (let i = 0; i < touchEvents.length; ++i) {
    for (let j = 0; j < recepients.length; ++j) {
      if (!(touchEvents[i] in recepients[j])) {
        Object.defineProperty(recepients[j], touchEvents[i], {
          value: null, writable: true, configurable: true, enumerable: true
        });
      }
    }
  }
}
        '''  # noqa: E501

        reloadNeeded = False
        if viewport.get('hasTouch') and not self._injectedTouchScriptId:
            source = f'({injectedTouchEventsFunction})()'
            self._injectedTouchScriptId = (await self._client.send(
                'Page.addScriptToEvaluateOnNewDocument',
                {'source': source})).get('identifier')
            reloadNeeded = True
        elif not viewport.get('hasTouch') and self._injectedTouchScriptId:
            await self._client.send(
                'Page.removeScriptToEvaluateOnNewDocument',
                {'identifier': self._injectedTouchScriptId}
            )
            self._injectedTouchScriptId = None
            reloadNeeded = True

        if self._emulatingMobile != mobile:
            reloadNeeded = True
        self._emulatingMobile = mobile
        return reloadNeeded
예제 #4
0
 async def emulateViewport(self, client: CDPSession,
                           viewport: dict) -> bool:
     'Evaluate viewport.'
     options = dict()
     mobile = viewport.get('isMobile', False)
     options['mobile'] = mobile
     if ('width' in viewport):
         options['width'] = helper.get_positive_int(viewport, 'width')
     if ('height' in viewport):
         options['height'] = helper.get_positive_int(viewport, 'height')
     options['deviceScaleFactor'] = viewport.get('deviceScaleFactor', 1)
     if viewport.get('isLandscape'):
         options['screenOrientation'] = {
             'angle': 90,
             'type': 'landscapePrimary',
         }
     else:
         options['screenOrientation'] = {
             'angle': 0,
             'type': 'portraitPrimary',
         }
     (await self._client.send('Emulation.setDeviceMetricsOverride',
                              options))
     (await self._client.send(
         'Emulation.setTouchEmulationEnabled', {
             'enabled': viewport.get('hasTouch', False),
             'configuration': ('mobile' if mobile else 'desktop'),
         }))
     injectedTouchEventsFunction = "\nfunction injectedTouchEventsFunction() {\n  const touchEvents = ['ontouchstart', 'ontouchend', 'ontouchmove', 'ontouchcancel'];\n  const recepients = [window.__proto__, document.__proto__];\n  for (let i = 0; i < touchEvents.length; ++i) {\n    for (let j = 0; j < recepients.length; ++j) {\n      if (!(touchEvents[i] in recepients[j])) {\n        Object.defineProperty(recepients[j], touchEvents[i], {\n          value: null, writable: true, configurable: true, enumerable: true\n        });\n      }\n    }\n  }\n}\n        "
     reloadNeeded = False
     if (viewport.get('hasTouch') and (not self._injectedTouchScriptId)):
         source = ''.join(
             ['(', '{}'.format(injectedTouchEventsFunction), ')()'])
         self._injectedTouchScriptId = (await self._client.send(
             'Page.addScriptToEvaluateOnNewDocument', {
                 'source': source,
             })).get('identifier')
         reloadNeeded = True
     elif ((not viewport.get('hasTouch')) and self._injectedTouchScriptId):
         (await
          self._client.send('Page.removeScriptToEvaluateOnNewDocument', {
              'identifier': self._injectedTouchScriptId,
          }))
         self._injectedTouchScriptId = None
         reloadNeeded = True
     if (self._emulatingMobile != mobile):
         reloadNeeded = True
     self._emulatingMobile = mobile
     return reloadNeeded
예제 #5
0
    async def emulateViewport(self, viewport: dict) -> bool:
        """Evaluate viewport."""
        options = dict()
        mobile = viewport.get('isMobile', False)
        options['mobile'] = mobile
        if 'width' in viewport:
            options['width'] = helper.get_positive_int(viewport, 'width')
        if 'height' in viewport:
            options['height'] = helper.get_positive_int(viewport, 'height')

        options['deviceScaleFactor'] = viewport.get('deviceScaleFactor', 1)
        if viewport.get('isLandscape'):
            options['screenOrientation'] = {
                'angle': 90,
                'type': 'landscapePrimary'
            }
        else:
            options['screenOrientation'] = {
                'angle': 0,
                'type': 'portraitPrimary'
            }

        await self._client.send('Emulation.setDeviceMetricsOverride', options)
        await self._client.send(
            'Emulation.setTouchEmulationEnabled', {
                'enabled': viewport.get('hasTouch', False),
                'configuration': 'mobile' if mobile else 'desktop'
            })

        injectedTouchEventsFunction = '''
function injectedTouchEventsFunction() {
  const touchEvents = ['ontouchstart', 'ontouchend', 'ontouchmove', 'ontouchcancel'];
  const recepients = [window.__proto__, document.__proto__];
  for (let i = 0; i < touchEvents.length; ++i) {
    for (let j = 0; j < recepients.length; ++j) {
      if (!(touchEvents[i] in recepients[j])) {
        Object.defineProperty(recepients[j], touchEvents[i], {
          value: null, writable: true, configurable: true, enumerable: true
        });
      }
    }
  }
}
        '''  # noqa: E501

        reloadNeeded = False
        if viewport.get('hasTouch') and not self._injectedTouchScriptId:
            source = f'({injectedTouchEventsFunction})()'
            self._injectedTouchScriptId = (await self._client.send(
                'Page.addScriptToEvaluateOnNewDocument',
                {'source': source})).get('identifier')
            reloadNeeded = True
        elif not viewport.get('hasTouch') and self._injectedTouchScriptId:
            await self._client.send(
                'Page.removeScriptToEvaluateOnNewDocument',
                {'identifier': self._injectedTouchScriptId})
            self._injectedTouchScriptId = None
            reloadNeeded = True

        if self._emulatingMobile != mobile:
            reloadNeeded = True
        self._emulatingMobile = mobile
        return reloadNeeded
예제 #6
0
 def test_negative_int(self):
     with self.assertRaises(ValueError):
         get_positive_int({'a': -1}, 'a')
예제 #7
0
 def test_badtype(self):
     with self.assertRaises(TypeError):
         get_positive_int({'a': 'b'}, 'a')
예제 #8
0
 def test_negative_int(self):
     with self.assertRaises(ValueError):
         get_positive_int({'a': -1}, 'a')
예제 #9
0
 def test_badtype(self):
     with self.assertRaises(TypeError):
         get_positive_int({'a': 'b'}, 'a')