From 533807601ed4605c244f5cd01110579cd29c03b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9?= <41592183+Snooz82@users.noreply.github.com> Date: Tue, 14 Mar 2023 16:21:23 +0100 Subject: [PATCH] fixes #2650 (#2697) fixes #2650 --- Browser/keywords/promises.py | 35 +++++++++++++++++------- atest/test/03_Waiting/promise_to.robot | 12 +++++++- atest/test/05_JS_Tests/funky.js | 2 +- atest/test/05_JS_Tests/jsextension.robot | 7 +++++ debug/test.robot | 12 -------- 5 files changed, 44 insertions(+), 24 deletions(-) delete mode 100644 debug/test.robot diff --git a/Browser/keywords/promises.py b/Browser/keywords/promises.py index cad0f3cdb..bf03e5e02 100644 --- a/Browser/keywords/promises.py +++ b/Browser/keywords/promises.py @@ -55,19 +55,34 @@ def promise_to(self, kw: str, *args) -> Future: [https://forum.robotframework.org/t//4312|Comment >>] """ promise: Future = Future() - keyword_name = kw.strip().lower().replace(" ", "_") - - if keyword_name in self.library.get_keyword_names(): - positional, named = self.resolve_arguments(keyword_name, *args) - promise = self._executor.submit( - self.library.keywords[keyword_name], *positional, **(named or {}) + known_keyword = self.get_known_keyword(kw) + if not known_keyword: + raise ValueError( + f"Unknown keyword '{kw}'! 'Promise To' can only be used with Browser keywords." ) - self.unresolved_promises.add(promise) - while not (promise.running() or promise.done()): - sleep(0.01) - + positional, named = self.resolve_arguments(known_keyword, *args) + promise = self._executor.submit( + self.library.keywords[known_keyword], *positional, **(named or {}) + ) + self.unresolved_promises.add(promise) + while not (promise.running() or promise.done()): + sleep(0.01) return promise + def get_known_keyword(self, kw: str) -> str: + normalized_kw = self.normalized_keyword_name(kw) + for keyword_name in self.library.get_keyword_names(): + if normalized_kw == self.normalized_keyword_name(keyword_name): + return keyword_name + return "" + + def normalized_keyword_name(self, kw: str) -> str: + """Returns normalized keyword name. + + Keyword name is normalized by removing spaces and converting to lower case. + """ + return kw.lower().replace(" ", "").replace("_", "") + def resolve_arguments(self, kw: str, *args): positional: List[Any] = [] named: Dict[str, Any] = {} diff --git a/atest/test/03_Waiting/promise_to.robot b/atest/test/03_Waiting/promise_to.robot index e6aa53897..1f95e32ed 100644 --- a/atest/test/03_Waiting/promise_to.robot +++ b/atest/test/03_Waiting/promise_to.robot @@ -17,4 +17,14 @@ Promise To Convert Type Wait For ${promise} Get Text id=click_count == 4 Get Text id=mouse_delay_time validate int(value) > 100 and int(value) < 300 - Get Text id=mouse_button == left \ No newline at end of file + Get Text id=mouse_button == left + +Could Not Find Keyword W Promise To + [Setup] NONE + TRY + Promise To Could Not Find Keyword + EXCEPT ValueError: Unknown keyword 'Could Not Find Keyword'! 'Promise To' can only be used with Browser keywords. AS ${e} + Log ${e} + ELSE + FAIL Should have failed + END \ No newline at end of file diff --git a/atest/test/05_JS_Tests/funky.js b/atest/test/05_JS_Tests/funky.js index 92f198758..99e99d784 100644 --- a/atest/test/05_JS_Tests/funky.js +++ b/atest/test/05_JS_Tests/funky.js @@ -1,5 +1,5 @@ async function myFunkyKeyword(page, args, logger) { - const h = await page.$(args[0]); + const h = await page.locator(args[0]); logger("Logging something funky"); return await h.evaluate((e) => e.textContent = "Funk yeah!"); } diff --git a/atest/test/05_JS_Tests/jsextension.robot b/atest/test/05_JS_Tests/jsextension.robot index 5a7bce906..06f1b343c 100644 --- a/atest/test/05_JS_Tests/jsextension.robot +++ b/atest/test/05_JS_Tests/jsextension.robot @@ -5,6 +5,13 @@ Resource imports.resource Force Tags no-iframe *** Test Cases *** +Promise To Call Custom Js Keyword + New Page + ${promise} Promise To My Funky keyword h1 + Go To ${LOGIN_URL} + Wait For ${promise} + Get Text h1 == Funk yeah! + Calling Custom Js Keyword New Page ${LOGIN_URL} Get Text h1 == Login Page diff --git a/debug/test.robot b/debug/test.robot deleted file mode 100644 index 1de4eb496..000000000 --- a/debug/test.robot +++ /dev/null @@ -1,12 +0,0 @@ -*** Settings *** -Library Browser - - -*** Test Cases *** -Test - New Browser chromium headless=False - New Context - New Page https://robotframework.org/code - ${p} Promise To Click text="log.html" left position_x=10 position_y=5 - Click "Run" - Wait For ${p} \ No newline at end of file