Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def test_nice_error_after_navigation(self):
executionContext = await self.page.mainFrame.executionContext()
await asyncio.wait([
self.page.waitForNavigation(),
executionContext.evaluate('window.location.reload()'),
])
with self.assertRaises(NetworkError) as cm:
await executionContext.evaluate('() => null')
self.assertIn('navigation', cm.exception.args[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()
def open(self):
if self.is_open():
raise ConnectionError('Connection is already open')
self.logger.info('opening')
self._reading_thread = threading.Thread(target=lambda: self._read_forever())
self._reading_thread.start()
self._open_event.wait()
self.logger.info('opened')
if self.prefer_experimental:
self.send(
'Security.setIgnoreCertificateErrors', # API status: experimental
{'ignore': True}
)
else:
self.send('Security.enable', {}) # API status: stable
self.send(
'Security.setOverrideCertificateErrors', # API status: deprecated
async def test_ignore_https_errors(self):
browser = await launch(DEFAULT_OPTIONS, ignoreHTTPSErrors=True)
page = await browser.newPage()
port = get_free_port()
time.sleep(0.1)
app = get_application()
server = app.listen(port)
response = await page.goto('https://localhost:{}'.format(port))
self.assertTrue(response.ok)
await browser.close()
server.stop()
async def test_user_data_dir_restore_cookie_in_browser(self):
browser = await launch(
DEFAULT_OPTIONS, userDataDir=self.datadir, headless=False)
page = await browser.newPage()
await page.goto(self.url + 'empty')
await page.evaluate('() => document.cookie = "foo=true; expires=Fri, 31 Dec 9999 23:59:59 GMT"') # noqa: E501
await browser.close()
browser2 = await launch(DEFAULT_OPTIONS, userDataDir=self.datadir)
page2 = await browser2.newPage()
await page2.goto(self.url + 'empty')
result = await page2.evaluate('() => document.cookie')
await browser2.close()
self.assertEqual(result, 'foo=true')
async def test_launch(self):
browser = await launch(DEFAULT_OPTIONS)
await browser.newPage()
await browser.close()
async def test_json_circular_object_error(self):
windowHandle = await self.page.evaluateHandle('window')
with self.assertRaises(NetworkError) as cm:
await windowHandle.jsonValue()
self.assertIn('Object reference chain is too long',
cm.exception.args[0])
async def test_fail_page_closed(self):
page = await self.context.newPage()
await page.goto(self.url + 'empty')
task = page.waitForSelector('.box')
await page.close()
with self.assertRaises(NetworkError):
await task
async def test_query_objects_disposed(self):
await self.page.goto(self.url + 'empty')
prototypeHandle = await self.page.evaluateHandle(
'() => HTMLBodyElement.prototype'
)
await prototypeHandle.dispose()
with self.assertRaises(ElementHandleError):
await self.page.queryObjects(prototypeHandle)
async def test_Jeval_with_missing_selector(self):
htmlContent = '<div class="a">not-a-child-div</div><div id="myId"></div>' # noqa: E501
await self.page.setContent(htmlContent)
elementHandle = await self.page.J('#myId')
with self.assertRaises(ElementHandleError) as cm:
await elementHandle.Jeval('.a', 'node => node.innerText')
self.assertIn('Error: failed to find element matching selector ".a"',
cm.exception.args[0])