def _wait_response(self, window_width: int, window_height: int, timeout: Optional[int]) -> Dict: """Open a browser to the created form and wait for the user to submit it. """ browser = Selenium() def is_browser_open(): try: return bool(browser.driver.current_window_handle) except Exception: # pylint: disable=broad-except return False try: browser.open_available_browser(self.server_address) browser.set_window_size(window_width, window_height) start_time = time.time() while True: response = requests.get( f"{self.server_address}/requestresponse", headers={"Prefer": "wait=120"}, ) if response.status_code == 200: return response.json() elif response.status_code != 304: response.raise_for_status() elif not is_browser_open(): raise RuntimeError("Browser closed by user") elif timeout and time.time() > start_time + int(timeout): raise RuntimeError("No response within timeout") else: time.sleep(1) finally: browser.close_browser()
def request_response( self, formspec: str = None, window_width: int = 600, window_height: int = 1000 ) -> dict: """Start server and show form. Waits for user response. :param formspec: form json specification file, defaults to None :param window_width: window width in pixels, defaults to 600 :param window_height: window height in pixels, defaults to 1000 :return: form response Example: .. code-block:: robotframework Create Form ${CURDIR}/${/}myform.json &{response} Request Response """ self._start_attended_server() if self.custom_form is None: self.create_form("Requesting response") if formspec: formdata = open(formspec, "rb") else: formdata = json.dumps(self.custom_form) headers = {"Accept": "application/json", "Content-Type": "application/json"} requests.post( f"{self.server_address}/formspec", data=formdata, headers=headers, ) response_json = {} try: br = Selenium() br.open_available_browser(f"{self.server_address}/form.html") br.set_window_size(window_width, window_height) headers = {"Prefer": "wait=120"} # etag = None while True: # if etag: # headers['If-None-Match'] = etag headers["If-None-Match"] = "2434432243" response = requests.get( f"{self.server_address}/requestresponse", headers=headers ) # etag = response.headers.get("ETag") if response.status_code == 200: try: response_json = response.json() break except JSONDecodeError: break elif response.status_code != 304: # back off if the server is throwing errors time.sleep(10) continue time.sleep(1) finally: br.close_browser() self._stop_attended_server() return response_json