async def wait_for_request_url(self, url, method='GET', body=None, timeout=None): url = str2str(url) method = str2str(method) req = await self.library_ctx.get_current_page().get_page( ).waitForRequest( lambda req: re.search(url, req.url ) is not None and req.method == method, options={ 'timeout': self.timestr_to_secs_for_default_timeout(timeout) * 1000 }) try: pos_data = (await req.postData()) except: pos_data = '' if body is None or re.search(body, pos_data.replace('\n', '')): log_str = 'Wait for request url: ' + req.method + ' - ' + req.url if pos_data != '': log_str + '\n' + pos_data self.info(log_str) else: raise Exception('Can\'t match request body with ' + body + ' \n ' + pos_data) return DotDict({ 'url': req.url, 'method': req.method, 'body': pos_data })
async def handle_dialog(self, dialog, action, prompt_text=''): action = str2str(action) prompt_text = str2str(prompt_text) if action == 'ACCEPT': await dialog.accept(prompt_text) elif action == 'DISMISS': await dialog.dismiss()
async def add_cookie(self, name: str, value: str): name = str2str(name) value = str2str(value) await self.library_ctx.get_current_page().get_page().setCookie({ 'name': name, 'value': value, })
async def click_element_at_coordinate(self, locator: str, xoffset: str, yoffset: str): xoffset = str2str(xoffset) yoffset = str2str(yoffset) element = await self.library_ctx.get_current_page().querySelector_with_selenium_locator(locator) await element.click( position={ 'x': int(xoffset), 'y': int(yoffset) })
async def add_cookie(self, name: str, value: str): name = str2str(name) value = str2str(value) url = self.library_ctx.get_current_page().get_page().url await self.library_ctx.get_browser_context().contexts[0].add_cookies([{ 'url': url, 'name': name, 'value': value }])
async def mock_current_page_api_response(self, url, mock_response, method='GET', body=None): url = str2str(url) method = str2str(method) page = self.library_ctx.get_current_page().get_page() await page.route( url, lambda route, request: asyncio.ensure_future( self.mock_api_response(route, request, mock_response, method, body)))
async def mock_api_response(self, request: Request, url, mock_response, method, body): url = str2str(url) method = str2str(method) if re.search(re.escape(url), request.url) is not None and request.method == method: try: pos_data = (await request.postData()) except: pos_data = '' if body is None or re.search(body, pos_data.replace('\n', '')): return await request.respond(mock_response) await request.continue_()
async def mock_current_page_api_response(self, url, mock_response, method='GET', body=None): url = str2str(url) method = str2str(method) page = self.library_ctx.get_current_page().get_page() await page.setRequestInterception(True) page.on( 'request', lambda request: asyncio.ensure_future( self.mock_api_response(request, url, mock_response, method, body)))
async def input_text(self, locator: str, text: str, clear=True): text = str2str(text) clear = str2bool(clear) if clear: await self._clear_input_text(locator) await self.library_ctx.get_current_page().type_with_selenium_locator( locator, text)
async def wait_for_response_url(self, url, status=200, body=None, timeout=None): url = str2str(url) status = str2int(status) res = await self.library_ctx.get_current_page().get_page( ).waitForResponse( lambda res: re.search(url, res.url ) is not None and res.status == status, options={ 'timeout': self.timestr_to_secs_for_default_timeout(timeout) * 1000 }) try: res_text = (await res.text()) except: res_text = '' if body is None or re.search(body, res_text.replace('\n', '')): log_str = 'Wait for request url: ' + res.url if res_text != '': log_str += '\n' + res_text self.info(log_str) else: raise Exception('Can\'t match response body with ' + body + ' \n ' + res_text) return DotDict({ 'url': res.url, 'status': res.status, 'body': res_text })
async def wait_until_page_does_not_contains(self, text, timeout=None): text = str2str(text) locator = "xpath://*[contains(., %s)]" % self.escape_xpath_value(text) return await self._wait_for_selenium_selector(locator, timeout, visible=False, hidden=True)
async def capture_page_screenshot(self, path: str, fullPage: bool): path = str2str(path) fullPage = str2bool(fullPage) return await self.library_ctx.get_current_page().get_page().screenshot( path=f''+path, full_page=fullPage )
async def _click_with_specific_tag(self, locator: str, expect_tag_name: str): expect_tag_name = str2str(expect_tag_name) elements = await self.library_ctx.get_current_page().querySelectorAll_with_selenium_locator(locator) for element in elements: tag_name = await (await element.get_property('tagName')).json_value() if tag_name.lower() == expect_tag_name: return await element.click() raise Exception('Can\'t find the specific '+ expect_tag_name +' element for '+locator)
async def wait_until_location_contains(self, expected, timeout=None): expected = str2str(expected) async def validate_url_contains_text(): return expected in self.library_ctx.get_current_page().get_page( ).url return await self._wait_until_worker( validate_url_contains_text, self.timestr_to_secs_for_default_timeout(timeout))
async def wait_until_element_contains(self, locator, text, timeout=None): text = str2str(text) async def validate_element_contains_text(): return (text in (await (await (await self.library_ctx.get_current_page( ).querySelector_with_selenium_locator(locator) ).getProperty('textContent')).jsonValue())) return await self._wait_until_worker( validate_element_contains_text, self.timestr_to_secs_for_default_timeout(timeout))
async def go_to(self, url): url = str2str(url) return await self.library_ctx.get_current_page().goto(url)
async def element_should_contain(self, locator: str, expected: str, ignore_case=False): expected = str2str(expected) ignore_case = str2bool(ignore_case) text = await self.get_text(locator) return BuiltIn().should_contain(text, expected, ignore_case=ignore_case)
async def upload_file(self, locator: str, file_path: str): file_path = str2str(file_path) element = await self.library_ctx.get_current_page().querySelector_with_selenium_locator(locator) await element.set_input_files(file_path)
async def get_cookie(self, name: str): name = str2str(name) cookies = await self.get_cookies() return cookies[name]
async def element_text_should_not_be(self, locator: str, expected: str, ignore_case=False): expected = str2str(expected) ignore_case = str2bool(ignore_case) text = await self.get_text(locator) return BuiltIn().should_not_be_equal_as_strings(text, expected, ignore_case=ignore_case)
async def get_attribute(self, locator: str, attribute: str) -> str: attribute = str2str(attribute) element = await self.library_ctx.get_current_page().querySelector_with_selenium_locator(locator) return (await (await element.get_property(attribute)).json_value())
async def handle_alert(self, action, prompt_text=''): action = str2str(action) prompt_text = str2str(prompt_text) return self.library_ctx.get_current_page().get_page().on( 'dialog', lambda dialog: asyncio.ensure_future( self.handle_dialog(dialog, action, prompt_text)))
async def input_password(self, locator: str, text: str, clear=True): text = str2str(text) clear = str2bool(clear) await self.input_text(locator, text, clear)
async def execute_javascript(self, code): code = str2str(code) return await self.library_ctx.get_current_page().get_page().evaluate( code)
async def select_from_list_by_value(self, locator, values): values = str2str(values) selector_value = SelectorAbstraction.get_selector(locator) return await self.library_ctx.get_current_page( ).get_selected_frame_or_page().select_option(selector_value, value=values)