Skip to content

Commit

Permalink
#2649 type conversion promise to (#2696)
Browse files Browse the repository at this point in the history
Added type conversion to `Promise To`
  • Loading branch information
Snooz82 authored Mar 14, 2023
1 parent 96d2687 commit b730bb0
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 27 deletions.
4 changes: 2 additions & 2 deletions Browser/keywords/interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,8 @@ def click(
| ``button`` | Defaults to ``left`` if invalid. |
| ``clickCount`` | Defaults to 1. |
| ``delay`` | Time to wait between mouse-down and mouse-up. Defaults to 0. |
| ``position_x`` | & ``position_y`` A point to click relative to the top-left corner of element bounding-box. Only positive values within the bounding-box are allowed. If not specified, clicks to some visible point of the element. |
| ``force`` | Set to True to skip Playwright's [https://playwright.dev/docs/actionability | Actionability checks]. |
| ``position_x`` ``position_y`` | A point to click relative to the top-left corner of element bounding-box. Only positive values within the bounding-box are allowed. If not specified, clicks to some visible point of the element. |
| ``force`` | Set to True to skip Playwright's Actionability checks (https://playwright.dev/docs/actionability). |
| ``noWaitAfter`` | Actions that initiate navigation, are waiting for these navigation to happen and for pages to start loading. You can opt out of waiting via setting this flag. You would only need this option in the exceptional cases such as navigating to inaccessible pages. Defaults to ``False``. |
Keyword uses strict mode, see `Finding elements` for more details about strict mode.
Expand Down
1 change: 0 additions & 1 deletion Browser/keywords/playwright_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,6 @@ def new_browser(
slowMo: timedelta = timedelta(seconds=0),
timeout: timedelta = timedelta(seconds=30),
) -> str:

"""Create a new playwright Browser with specified options.
See `Browser, Context and Page` for more information about Browser and related concepts.
Expand Down
45 changes: 23 additions & 22 deletions Browser/keywords/promises.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@
from time import sleep
from typing import Any, Dict, List

from assertionengine import AssertionOperator
from robot.api.deco import keyword
from robot.running.arguments.typeconverters import TypeConverter
from robot.utils import DotDict

from ..base import LibraryComponent
from ..generated.playwright_pb2 import Request
from ..utils import DownloadedFile, logger
from ..utils.data_types import DialogAction, ElementState, PageLoadStates


class Promises(LibraryComponent):
Expand Down Expand Up @@ -74,36 +73,38 @@ def resolve_arguments(self, kw: str, *args):
named: Dict[str, Any] = {}
logger.debug(f"*args {args}")

keyword_arguments = [
arg_names = [
argument[0] if isinstance(argument, tuple) else argument
for argument in self.library.get_keyword_arguments(kw)
]
for arg in args:
parts = arg.partition("=")
if parts[0].strip() in keyword_arguments:
if parts[2].strip().lower() in DialogAction.__members__:
named[parts[0].strip()] = DialogAction[parts[2].strip().lower()]
elif parts[2].strip().lower() in PageLoadStates.__members__:
named[parts[0].strip()] = PageLoadStates[parts[2].strip().lower()]
else:
named[parts[0].strip()] = parts[2].strip()
for index, arg in enumerate(args):
arg_name, has_equal, arg_value = arg.partition("=")
arg_name = arg_name.strip()
arg_value = arg_value.strip()

if not named and (not has_equal or arg_name not in arg_names):
positional.append(self.convert_keyword_arg(kw, arg_names[index], arg))
elif arg_name in arg_names:
named[arg_name] = self.convert_keyword_arg(kw, arg_name, arg_value)
else:
if arg.strip() in AssertionOperator.__members__:
positional.append(AssertionOperator[arg.strip()])
elif arg.strip() in ElementState.__members__:
positional.append(ElementState.__members__[arg.strip()])
elif arg.strip() in DialogAction.__members__:
positional.append(DialogAction[arg.strip()])
elif arg.strip() in PageLoadStates.__members__:
positional.append(PageLoadStates.__members__[arg.strip()])
else:
positional.append(arg)
raise ValueError(
f"Invalid argument '{arg_name}' for keyword '{kw}'. "
f"Valid arguments are: {', '.join(arg_names)}"
)

logger.debug(
f"named arguments: {named}, positional arguments: {tuple(positional)}"
)
return tuple(positional), named

def convert_keyword_arg(self, kw: str, arg_name: str, arg_value: Any) -> Any:
argument_type = self.library.get_keyword_types(kw).get(arg_name)
if argument_type is not None:
return TypeConverter.converter_for(argument_type).convert(
arg_name, arg_value
)
return arg_value

@keyword(tags=("Wait", "BrowserControl"))
def promise_to_wait_for_download(self, saveAs: str = "") -> Future:
"""Returns a promise that waits for next download event on page.
Expand Down
20 changes: 20 additions & 0 deletions atest/test/03_Waiting/promise_to.robot
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
*** Settings ***
Resource imports.resource

Test Setup New Page ${WAIT_URL}


*** Test Cases ***
Promise To With Type Conversion
${promise}= Promise To Click id=victim button=left
Select Options By id=dropdown value True enabled
Click id=submit noWaitAfter=True
Wait For ${promise}

Promise To Convert Type
${promise}= Promise To Click id=clickWithOptions left clickCount=4 delay=200 ms
Go To ${LOGIN_URL}
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
12 changes: 12 additions & 0 deletions debug/test.robot
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
*** 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}
1 change: 0 additions & 1 deletion tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@

@task
def deps(c):

if _sources_changed(
[ROOT_DIR / "Browser/dev-requirements.txt"], python_deps_timestamp_file
):
Expand Down
2 changes: 1 addition & 1 deletion utest/test_python_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_promise_to_wait_for_response_with_name_arguments(browser):
def test_promise_to_wait_for_alert_with_name_arguments(browser):
browser.new_page()
promise = browser.promise_to(
"Wait For Alert", "action=ignore", "prompt_input=Kala", "text=Wrong Text"
"Wait For Alert", "action=accept", "prompt_input=Kala", "text=Wrong Text"
)
browser.go_to(url="https://www.google.com")
assert (promise.running() or promise.done()) is True
Expand Down

0 comments on commit b730bb0

Please sign in to comment.