Skip to content

Commit

Permalink
fixes #2650 (#2697)
Browse files Browse the repository at this point in the history
fixes #2650
  • Loading branch information
Snooz82 authored Mar 14, 2023
1 parent b730bb0 commit 5338076
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 24 deletions.
35 changes: 25 additions & 10 deletions Browser/keywords/promises.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {}
Expand Down
12 changes: 11 additions & 1 deletion atest/test/03_Waiting/promise_to.robot
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
2 changes: 1 addition & 1 deletion atest/test/05_JS_Tests/funky.js
Original file line number Diff line number Diff line change
@@ -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!");
}
Expand Down
7 changes: 7 additions & 0 deletions atest/test/05_JS_Tests/jsextension.robot
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 0 additions & 12 deletions debug/test.robot

This file was deleted.

0 comments on commit 5338076

Please sign in to comment.