def test_get_token_from_local_storage(self, mock_post): mock_post.return_value.status_code = 200 mock_post.return_value.content = '{"token": "hey you"}' browser = to_sync( open_browser(True, write_websocket=False, launch_args=["--no-sandbox"])) page = to_sync(browser.newPage()) to_sync(page.goto('http://duckduckgo.com')) get_token("http://false", {}, True, page) token = get_local_storage_token(page) self.assertEqual(token, '{"token": "hey you"}') browser.close()
async def _page_manager(browser: Browser, url: str, params: dict) -> Page: page = await browser.newPage() arg_viewport = params.get("arg_viewport") if arg_viewport: await page.setViewport(arg_viewport) credentials = params.get("credentials") credentials_token_request = params.get("credentials_token_request") use_local_token = params.get("use_local_token") if credentials: credentials_data = credentials.get("credentials_data") if credentials.get("login"): await page.authenticate(credentials_data) if credentials.get("token_in_header"): await page.setExtraHTTPHeaders(credentials_data) elif credentials_token_request: url_token = credentials_token_request.pop("url") local_storage = credentials_token_request.pop("local_storage") extra_headers = get_token(url_token, credentials_token_request, local_storage=local_storage, page=page) await page.setExtraHTTPHeaders(extra_headers) elif use_local_token: extra_headers = get_local_storage_token(page) await page.setExtraHTTPHeaders(extra_headers) try: await page.goto(url, waitUntil=params.get("wait_until")) except PageError as _: raise BadUrl('url unknown: "{0}"'.format(url)) from None wait_for = params.get("wait_for") if wait_for: await page.waitForSelector(wait_for) wait_for_xpath = params.get("wait_for_xpath") if wait_for_xpath: await page.waitForXPath(wait_for_xpath) return page
def test_get_token_400(self, mock_post): mock_post.return_value.status_code = 400 mock_post.return_value.content = '{"token": "hey you"}' with self.assertRaises(BadAuth): get_token("http://false", {})
def test_get_token_200(self, mock_post): mock_post.return_value.status_code = 200 mock_post.return_value.content = '{"token": "hey you"}' self.assertEqual(get_token("http://false", {}), '{"token": "hey you"}')