From 1478a5c276f76ac534309bca5f96adef61124540 Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Sat, 17 Jul 2021 13:17:52 +0200 Subject: Added target css for click-element. --- qutebrowser/components/misccommands.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/qutebrowser/components/misccommands.py b/qutebrowser/components/misccommands.py index fe908b7d2..75ed7c5e2 100644 --- a/qutebrowser/components/misccommands.py +++ b/qutebrowser/components/misccommands.py @@ -27,7 +27,7 @@ import signal import functools import logging import pathlib -from typing import Optional +from typing import Optional, Union, List try: import hunter @@ -225,7 +225,7 @@ def insert_text(tab: apitypes.Tab, text: str) -> None: @cmdutils.register() @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) -@cmdutils.argument('filter_', choices=['id']) +@cmdutils.argument('filter_', choices=['id', 'css']) def click_element(tab: apitypes.Tab, filter_: str, value: str, *, target: apitypes.ClickTarget = apitypes.ClickTarget.normal, @@ -242,11 +242,16 @@ def click_element(tab: apitypes.Tab, filter_: str, value: str, *, target: How to open the clicked element (normal/tab/tab-bg/window). force_event: Force generating a fake click event. """ - def single_cb(elem: Optional[apitypes.WebElement]) -> None: + def single_cb(elem: Union[apitypes.WebElement, List[apitypes.WebElement], None]) -> None: """Click a single element.""" - if elem is None: - message.error("No element found with id {}!".format(value)) + if not elem: + message.error(f"No element found matching {filter_}={value}!") return + if isinstance(elem, list): + if len(elem) > 1: + message.error(f"Multiple elements found matching {filter_}={value}!") + return + elem = elem[0] try: elem.click(target, force_event=force_event) except apitypes.WebElemError as e: @@ -255,9 +260,10 @@ def click_element(tab: apitypes.Tab, filter_: str, value: str, *, handlers = { 'id': (tab.elements.find_id, single_cb), + 'css': (tab.elements.find_css, single_cb, message.error), } - handler, callback = handlers[filter_] - handler(value, callback) + handler, *callback = handlers[filter_] + handler(value, *callback) @cmdutils.register(debug=True) -- cgit v1.2.3-54-g00ecf From e5b17bf3b0c73650e5a4f4d0860dab036fcd4b79 Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Sat, 17 Jul 2021 13:31:58 +0200 Subject: Add argument to select first match to click-element. --- doc/help/commands.asciidoc | 3 ++- qutebrowser/components/misccommands.py | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/doc/help/commands.asciidoc b/doc/help/commands.asciidoc index 6003c0f1f..2694dd39d 100644 --- a/doc/help/commands.asciidoc +++ b/doc/help/commands.asciidoc @@ -254,7 +254,7 @@ Clear all message notifications. [[click-element]] === click-element -Syntax: +:click-element [*--target* 'target'] [*--force-event*] 'filter' 'value'+ +Syntax: +:click-element [*--target* 'target'] [*--force-event*] [*--select-first*] 'filter' 'value'+ Click the element matching the given filter. @@ -268,6 +268,7 @@ The given filter needs to result in exactly one element, otherwise, an error is ==== optional arguments * +*-t*+, +*--target*+: How to open the clicked element (normal/tab/tab-bg/window). * +*-f*+, +*--force-event*+: Force generating a fake click event. +* +*-s*+, +*--select-first*+: Select first matching element if there are multiple. [[close]] === close diff --git a/qutebrowser/components/misccommands.py b/qutebrowser/components/misccommands.py index 75ed7c5e2..abd21b2e2 100644 --- a/qutebrowser/components/misccommands.py +++ b/qutebrowser/components/misccommands.py @@ -229,7 +229,8 @@ def insert_text(tab: apitypes.Tab, text: str) -> None: def click_element(tab: apitypes.Tab, filter_: str, value: str, *, target: apitypes.ClickTarget = apitypes.ClickTarget.normal, - force_event: bool = False) -> None: + force_event: bool = False, + select_first: bool = False) -> None: """Click the element matching the given filter. The given filter needs to result in exactly one element, otherwise, an @@ -241,6 +242,7 @@ def click_element(tab: apitypes.Tab, filter_: str, value: str, *, value: The value to filter for. target: How to open the clicked element (normal/tab/tab-bg/window). force_event: Force generating a fake click event. + select_first: Select first matching element if there are multiple. """ def single_cb(elem: Union[apitypes.WebElement, List[apitypes.WebElement], None]) -> None: """Click a single element.""" @@ -248,7 +250,7 @@ def click_element(tab: apitypes.Tab, filter_: str, value: str, *, message.error(f"No element found matching {filter_}={value}!") return if isinstance(elem, list): - if len(elem) > 1: + if not select_first and len(elem) > 1: message.error(f"Multiple elements found matching {filter_}={value}!") return elem = elem[0] -- cgit v1.2.3-54-g00ecf From 47de400b0bb676faef6cff7832d4b9ad7604b434 Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Mon, 19 Jul 2021 19:16:34 +0200 Subject: Split handling of single and multiple elements in click_element. --- qutebrowser/components/misccommands.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/qutebrowser/components/misccommands.py b/qutebrowser/components/misccommands.py index abd21b2e2..7203c7f77 100644 --- a/qutebrowser/components/misccommands.py +++ b/qutebrowser/components/misccommands.py @@ -27,7 +27,7 @@ import signal import functools import logging import pathlib -from typing import Optional, Union, List +from typing import Optional, List try: import hunter @@ -244,25 +244,34 @@ def click_element(tab: apitypes.Tab, filter_: str, value: str, *, force_event: Force generating a fake click event. select_first: Select first matching element if there are multiple. """ - def single_cb(elem: Union[apitypes.WebElement, List[apitypes.WebElement], None]) -> None: - """Click a single element.""" - if not elem: - message.error(f"No element found matching {filter_}={value}!") - return - if isinstance(elem, list): - if not select_first and len(elem) > 1: - message.error(f"Multiple elements found matching {filter_}={value}!") - return - elem = elem[0] + def do_click(elem: apitypes.WebElement) -> None: try: elem.click(target, force_event=force_event) except apitypes.WebElemError as e: message.error(str(e)) + + def single_cb(elem: Optional[apitypes.WebElement]) -> None: + """Click a single element.""" + if elem is None: + message.error(f"No element found matching {filter_}={value}!") return + do_click(elem) + + def multiple_cb(elems: List[apitypes.WebElement]) -> None: + if len(elems) == 0: + message.error(f"No element found matching {filter_}={value}!") + return + + if not select_first and len(elems) > 1: + message.error(f"Multiple elements found matching {filter_}={value}!") + return + + do_click(elems[0]) + handlers = { 'id': (tab.elements.find_id, single_cb), - 'css': (tab.elements.find_css, single_cb, message.error), + 'css': (tab.elements.find_css, multiple_cb, message.error), } handler, *callback = handlers[filter_] handler(value, *callback) -- cgit v1.2.3-54-g00ecf From 53b87f024cf254c103968af613413277d14da75a Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Mon, 19 Jul 2021 19:48:33 +0200 Subject: Adapt tests to new error output. --- tests/end2end/features/misc.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/end2end/features/misc.feature b/tests/end2end/features/misc.feature index e6a02e038..0cd2c0ec9 100644 --- a/tests/end2end/features/misc.feature +++ b/tests/end2end/features/misc.feature @@ -436,7 +436,7 @@ Feature: Various utility commands. Scenario: Clicking an element with unknown ID When I open data/click_element.html And I run :click-element id blah - Then the error "No element found with id blah!" should be shown + Then the error "No element found matching id=blah!" should be shown Scenario: Clicking an element by ID When I open data/click_element.html -- cgit v1.2.3-54-g00ecf From b60939104aaaef2e8ec6d85fab6b1a27f0a47703 Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Mon, 19 Jul 2021 20:39:54 +0200 Subject: Add position as filter for click-element. --- qutebrowser/components/misccommands.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/qutebrowser/components/misccommands.py b/qutebrowser/components/misccommands.py index 7203c7f77..a15052c5c 100644 --- a/qutebrowser/components/misccommands.py +++ b/qutebrowser/components/misccommands.py @@ -34,7 +34,7 @@ try: except ImportError: hunter = None -from PyQt5.QtCore import Qt +from PyQt5.QtCore import Qt, QPoint from PyQt5.QtPrintSupport import QPrintPreviewDialog from qutebrowser.api import cmdutils, apitypes, message, config @@ -225,7 +225,7 @@ def insert_text(tab: apitypes.Tab, text: str) -> None: @cmdutils.register() @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) -@cmdutils.argument('filter_', choices=['id', 'css']) +@cmdutils.argument('filter_', choices=['id', 'css', 'position']) def click_element(tab: apitypes.Tab, filter_: str, value: str, *, target: apitypes.ClickTarget = apitypes.ClickTarget.normal, @@ -269,9 +269,18 @@ def click_element(tab: apitypes.Tab, filter_: str, value: str, *, do_click(elems[0]) + def wrap_find_at_pos(value, callback): + try: + x, y = map(int, value.split(',', maxsplit=1)) + except ValueError: + message.error(f"'{value}' is not a valid point.") + return + tab.elements.find_at_position(QPoint(x, y), callback) + handlers = { 'id': (tab.elements.find_id, single_cb), 'css': (tab.elements.find_css, multiple_cb, message.error), + 'position': (wrap_find_at_pos, single_cb), } handler, *callback = handlers[filter_] handler(value, *callback) -- cgit v1.2.3-54-g00ecf From dbc9e0d41b5df7417a78483923f85743c8390278 Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Mon, 19 Jul 2021 20:53:11 +0200 Subject: Added new filters to documentation of click-elements. --- doc/help/commands.asciidoc | 7 ++++++- qutebrowser/components/misccommands.py | 6 +++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/doc/help/commands.asciidoc b/doc/help/commands.asciidoc index 2694dd39d..aa7cc76cd 100644 --- a/doc/help/commands.asciidoc +++ b/doc/help/commands.asciidoc @@ -261,7 +261,12 @@ Click the element matching the given filter. The given filter needs to result in exactly one element, otherwise, an error is shown. ==== positional arguments -* +'filter'+: How to filter the elements. id: Get an element based on its ID. +* +'filter'+: How to filter the elements. + + - id: Get an element based on its ID. + - css: Filter by a CSS selector. + - position: Click the element at specified position. + Write position as 'x,y'. * +'value'+: The value to filter for. diff --git a/qutebrowser/components/misccommands.py b/qutebrowser/components/misccommands.py index a15052c5c..917c1145c 100644 --- a/qutebrowser/components/misccommands.py +++ b/qutebrowser/components/misccommands.py @@ -238,7 +238,11 @@ def click_element(tab: apitypes.Tab, filter_: str, value: str, *, Args: filter_: How to filter the elements. - id: Get an element based on its ID. + + - id: Get an element based on its ID. + - css: Filter by a CSS selector. + - position: Click the element at specified position. + Write value as 'x,y'. value: The value to filter for. target: How to open the clicked element (normal/tab/tab-bg/window). force_event: Force generating a fake click event. -- cgit v1.2.3-54-g00ecf From ef0db5918f247b0b4f59ec9b2b04e33f93ad8239 Mon Sep 17 00:00:00 2001 From: Nicholas42 Date: Mon, 19 Jul 2021 21:04:59 +0200 Subject: Apply suggestions from code review Co-authored-by: Florian Bruhin --- qutebrowser/components/misccommands.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/qutebrowser/components/misccommands.py b/qutebrowser/components/misccommands.py index 917c1145c..503ad1a75 100644 --- a/qutebrowser/components/misccommands.py +++ b/qutebrowser/components/misccommands.py @@ -263,7 +263,7 @@ def click_element(tab: apitypes.Tab, filter_: str, value: str, *, do_click(elem) def multiple_cb(elems: List[apitypes.WebElement]) -> None: - if len(elems) == 0: + if not elems: message.error(f"No element found matching {filter_}={value}!") return @@ -286,8 +286,8 @@ def click_element(tab: apitypes.Tab, filter_: str, value: str, *, 'css': (tab.elements.find_css, multiple_cb, message.error), 'position': (wrap_find_at_pos, single_cb), } - handler, *callback = handlers[filter_] - handler(value, *callback) + handler, *callbacks = handlers[filter_] + handler(value, *callbacks) @cmdutils.register(debug=True) -- cgit v1.2.3-54-g00ecf From 1045f57f09b7c7066f41b687bdcbb2485826078e Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Mon, 19 Jul 2021 21:08:28 +0200 Subject: Added type annotations. --- qutebrowser/components/misccommands.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/qutebrowser/components/misccommands.py b/qutebrowser/components/misccommands.py index 503ad1a75..1f342d9c4 100644 --- a/qutebrowser/components/misccommands.py +++ b/qutebrowser/components/misccommands.py @@ -27,7 +27,7 @@ import signal import functools import logging import pathlib -from typing import Optional, List +from typing import Optional, List, Callable try: import hunter @@ -273,7 +273,7 @@ def click_element(tab: apitypes.Tab, filter_: str, value: str, *, do_click(elems[0]) - def wrap_find_at_pos(value, callback): + def wrap_find_at_pos(value: str, callback: Callable) -> None: try: x, y = map(int, value.split(',', maxsplit=1)) except ValueError: -- cgit v1.2.3-54-g00ecf From 82e7a9f2ec1dc593e5a7134fb123728b296af45b Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Tue, 20 Jul 2021 09:50:41 +0200 Subject: Specify type of callback in click_element. --- qutebrowser/components/misccommands.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/qutebrowser/components/misccommands.py b/qutebrowser/components/misccommands.py index 1f342d9c4..c6171b44c 100644 --- a/qutebrowser/components/misccommands.py +++ b/qutebrowser/components/misccommands.py @@ -273,7 +273,9 @@ def click_element(tab: apitypes.Tab, filter_: str, value: str, *, do_click(elems[0]) - def wrap_find_at_pos(value: str, callback: Callable) -> None: + def wrap_find_at_pos(value: str, + callback: Callable[[Optional[apitypes.WebElement]], None] + ) -> None: try: x, y = map(int, value.split(',', maxsplit=1)) except ValueError: -- cgit v1.2.3-54-g00ecf From 2c0470d098a42fadcc5b33966b11ed6d00b6f6de Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Tue, 20 Jul 2021 10:01:32 +0200 Subject: Add tests for click-element with CSS filter. --- tests/end2end/data/click_element.html | 2 +- tests/end2end/features/misc.feature | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/end2end/data/click_element.html b/tests/end2end/data/click_element.html index acf0cf77c..c76fbf831 100644 --- a/tests/end2end/data/click_element.html +++ b/tests/end2end/data/click_element.html @@ -6,7 +6,7 @@ Test Element "Don't", he shouted Duplicate - Duplicate + Duplicate
link ID with dot diff --git a/tests/end2end/features/misc.feature b/tests/end2end/features/misc.feature index 0cd2c0ec9..8d31e19b4 100644 --- a/tests/end2end/features/misc.feature +++ b/tests/end2end/features/misc.feature @@ -457,6 +457,20 @@ Feature: Various utility commands. - data/click_element.html - data/hello.txt (active) + Scenario: Clicking an element by CSS selector + When I open data/click_element.html + And I run :click-element css .clickable + Then the javascript message "click_element CSS selector" should be logged + + Scenario: Clicking an element with non-unique filter + When I open data/click_element.html + And I run :click-element css span + Then the error "Multiple elements found matching css=span!" should be shown + + Scenario: Clicking first element matching a selector + When I open data/click_element.html + And I run :click-element --select-first css span + ## :command-history-{prev,next} Scenario: Calling previous command -- cgit v1.2.3-54-g00ecf From d48ccd30209e99b6c04b36a588a9fb0c6ae93995 Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Tue, 20 Jul 2021 10:19:30 +0200 Subject: Fixed typo in find_at_pos. --- qutebrowser/components/misccommands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qutebrowser/components/misccommands.py b/qutebrowser/components/misccommands.py index c6171b44c..5794c7305 100644 --- a/qutebrowser/components/misccommands.py +++ b/qutebrowser/components/misccommands.py @@ -281,7 +281,7 @@ def click_element(tab: apitypes.Tab, filter_: str, value: str, *, except ValueError: message.error(f"'{value}' is not a valid point.") return - tab.elements.find_at_position(QPoint(x, y), callback) + tab.elements.find_at_pos(QPoint(x, y), callback) handlers = { 'id': (tab.elements.find_id, single_cb), -- cgit v1.2.3-54-g00ecf From 16ea76090011e5e6fc685f8bb829b35c57762da5 Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Tue, 20 Jul 2021 10:22:48 +0200 Subject: Made error messages more uniform in click_element. --- qutebrowser/components/misccommands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qutebrowser/components/misccommands.py b/qutebrowser/components/misccommands.py index 5794c7305..a91be2d4b 100644 --- a/qutebrowser/components/misccommands.py +++ b/qutebrowser/components/misccommands.py @@ -279,7 +279,7 @@ def click_element(tab: apitypes.Tab, filter_: str, value: str, *, try: x, y = map(int, value.split(',', maxsplit=1)) except ValueError: - message.error(f"'{value}' is not a valid point.") + message.error(f"'{value}' is not a valid point!") return tab.elements.find_at_pos(QPoint(x, y), callback) -- cgit v1.2.3-54-g00ecf From 061542808b70a7862bb88d033753fd6129b2b077 Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Tue, 20 Jul 2021 10:24:46 +0200 Subject: Added some tests for click-element with position filter. --- tests/end2end/data/click_element.html | 2 ++ tests/end2end/features/misc.feature | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/tests/end2end/data/click_element.html b/tests/end2end/data/click_element.html index c76fbf831..c0783b4dd 100644 --- a/tests/end2end/data/click_element.html +++ b/tests/end2end/data/click_element.html @@ -10,5 +10,7 @@
link ID with dot + diff --git a/tests/end2end/features/misc.feature b/tests/end2end/features/misc.feature index 8d31e19b4..e32349754 100644 --- a/tests/end2end/features/misc.feature +++ b/tests/end2end/features/misc.feature @@ -471,6 +471,21 @@ Feature: Various utility commands. When I open data/click_element.html And I run :click-element --select-first css span + Scenario: Clicking an element by position + When I open data/click_element.html + And I run :click-element position 20,42 + Then the javascript message "click_element position" should be logged + + Scenario: Clicking an element with invalid position + When I open data/click_element.html + And I run :click-element position 20.42 + Then the error "'20.42' is not a valid point!" should be shown + + Scenario: Clicking an element with non-integer position + When I open data/click_element.html + And I run :click-element position 20,42.001 + Then the error "'20,42.001' is not a valid point!" should be shown + ## :command-history-{prev,next} Scenario: Calling previous command -- cgit v1.2.3-54-g00ecf From b56dfdc139a8d9665f80e75c5f4828134edc48f5 Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Wed, 21 Jul 2021 09:39:26 +0200 Subject: Make calling of click-element handlers more versatile. --- qutebrowser/components/misccommands.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/qutebrowser/components/misccommands.py b/qutebrowser/components/misccommands.py index a91be2d4b..70f4532a7 100644 --- a/qutebrowser/components/misccommands.py +++ b/qutebrowser/components/misccommands.py @@ -284,12 +284,12 @@ def click_element(tab: apitypes.Tab, filter_: str, value: str, *, tab.elements.find_at_pos(QPoint(x, y), callback) handlers = { - 'id': (tab.elements.find_id, single_cb), - 'css': (tab.elements.find_css, multiple_cb, message.error), - 'position': (wrap_find_at_pos, single_cb), + 'id': (tab.elements.find_id, value, single_cb), + 'css': (tab.elements.find_css, value, multiple_cb, message.error), + 'position': (wrap_find_at_pos, value, single_cb), } - handler, *callbacks = handlers[filter_] - handler(value, *callbacks) + handler, *arguments = handlers[filter_] + handler(*arguments) @cmdutils.register(debug=True) -- cgit v1.2.3-54-g00ecf From effeb9869caab6b6646e78ddebba20989b43ab27 Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Wed, 21 Jul 2021 09:43:43 +0200 Subject: Add focused filter to click-element. --- doc/help/commands.asciidoc | 3 ++- qutebrowser/components/misccommands.py | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/doc/help/commands.asciidoc b/doc/help/commands.asciidoc index aa7cc76cd..985b8b6a8 100644 --- a/doc/help/commands.asciidoc +++ b/doc/help/commands.asciidoc @@ -266,7 +266,8 @@ The given filter needs to result in exactly one element, otherwise, an error is - id: Get an element based on its ID. - css: Filter by a CSS selector. - position: Click the element at specified position. - Write position as 'x,y'. + Write value as 'x,y'. + - focused: Click the currently focused element. * +'value'+: The value to filter for. diff --git a/qutebrowser/components/misccommands.py b/qutebrowser/components/misccommands.py index 70f4532a7..9fe905519 100644 --- a/qutebrowser/components/misccommands.py +++ b/qutebrowser/components/misccommands.py @@ -225,7 +225,7 @@ def insert_text(tab: apitypes.Tab, text: str) -> None: @cmdutils.register() @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) -@cmdutils.argument('filter_', choices=['id', 'css', 'position']) +@cmdutils.argument('filter_', choices=['id', 'css', 'position', 'focused']) def click_element(tab: apitypes.Tab, filter_: str, value: str, *, target: apitypes.ClickTarget = apitypes.ClickTarget.normal, @@ -243,6 +243,7 @@ def click_element(tab: apitypes.Tab, filter_: str, value: str, *, - css: Filter by a CSS selector. - position: Click the element at specified position. Write value as 'x,y'. + - focused: Click the currently focused element. value: The value to filter for. target: How to open the clicked element (normal/tab/tab-bg/window). force_event: Force generating a fake click event. @@ -287,6 +288,7 @@ def click_element(tab: apitypes.Tab, filter_: str, value: str, *, 'id': (tab.elements.find_id, value, single_cb), 'css': (tab.elements.find_css, value, multiple_cb, message.error), 'position': (wrap_find_at_pos, value, single_cb), + 'focused': (tab.elements.find_focused, single_cb), } handler, *arguments = handlers[filter_] handler(*arguments) -- cgit v1.2.3-54-g00ecf From f79778710c60158bd4c52db2297275e8c1ac9020 Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Wed, 21 Jul 2021 09:44:11 +0200 Subject: Make value argument optional when clicking focused element. --- doc/help/commands.asciidoc | 4 ++-- qutebrowser/components/misccommands.py | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/doc/help/commands.asciidoc b/doc/help/commands.asciidoc index 985b8b6a8..2363b9499 100644 --- a/doc/help/commands.asciidoc +++ b/doc/help/commands.asciidoc @@ -254,7 +254,7 @@ Clear all message notifications. [[click-element]] === click-element -Syntax: +:click-element [*--target* 'target'] [*--force-event*] [*--select-first*] 'filter' 'value'+ +Syntax: +:click-element [*--target* 'target'] [*--force-event*] [*--select-first*] 'filter' ['value']+ Click the element matching the given filter. @@ -269,7 +269,7 @@ The given filter needs to result in exactly one element, otherwise, an error is Write value as 'x,y'. - focused: Click the currently focused element. -* +'value'+: The value to filter for. +* +'value'+: The value to filter for. Optional for 'focused' filter. ==== optional arguments * +*-t*+, +*--target*+: How to open the clicked element (normal/tab/tab-bg/window). diff --git a/qutebrowser/components/misccommands.py b/qutebrowser/components/misccommands.py index 9fe905519..456f3e3c9 100644 --- a/qutebrowser/components/misccommands.py +++ b/qutebrowser/components/misccommands.py @@ -226,7 +226,7 @@ def insert_text(tab: apitypes.Tab, text: str) -> None: @cmdutils.register() @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) @cmdutils.argument('filter_', choices=['id', 'css', 'position', 'focused']) -def click_element(tab: apitypes.Tab, filter_: str, value: str, *, +def click_element(tab: apitypes.Tab, filter_: str, value: str=None, *, target: apitypes.ClickTarget = apitypes.ClickTarget.normal, force_event: bool = False, @@ -244,7 +244,7 @@ def click_element(tab: apitypes.Tab, filter_: str, value: str, *, - position: Click the element at specified position. Write value as 'x,y'. - focused: Click the currently focused element. - value: The value to filter for. + value: The value to filter for. Optional for 'focused' filter. target: How to open the clicked element (normal/tab/tab-bg/window). force_event: Force generating a fake click event. select_first: Select first matching element if there are multiple. @@ -284,6 +284,9 @@ def click_element(tab: apitypes.Tab, filter_: str, value: str, *, return tab.elements.find_at_pos(QPoint(x, y), callback) + if value is None and filter_ != 'focused': + raise cmdutils.CommandError("Argument 'value' is only optional with filter 'focused'!") + handlers = { 'id': (tab.elements.find_id, value, single_cb), 'css': (tab.elements.find_css, value, multiple_cb, message.error), -- cgit v1.2.3-54-g00ecf From 08bba7ac775214771bf6ea52e09937b0ecefc69e Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Wed, 21 Jul 2021 09:58:03 +0200 Subject: Simplified click-element method. --- qutebrowser/components/misccommands.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/qutebrowser/components/misccommands.py b/qutebrowser/components/misccommands.py index 456f3e3c9..5f182aa20 100644 --- a/qutebrowser/components/misccommands.py +++ b/qutebrowser/components/misccommands.py @@ -223,10 +223,21 @@ def insert_text(tab: apitypes.Tab, text: str) -> None: tab.elements.find_focused(_insert_text_cb) +def _wrap_find_at_pos(value: str, tab: apitypes.Tab, + callback: Callable[[Optional[apitypes.WebElement]], None] + ) -> None: + try: + x, y = map(int, value.split(',', maxsplit=1)) + except ValueError: + message.error(f"'{value}' is not a valid point!") + return + tab.elements.find_at_pos(QPoint(x, y), callback) + + @cmdutils.register() @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) @cmdutils.argument('filter_', choices=['id', 'css', 'position', 'focused']) -def click_element(tab: apitypes.Tab, filter_: str, value: str=None, *, +def click_element(tab: apitypes.Tab, filter_: str, value: str = None, *, target: apitypes.ClickTarget = apitypes.ClickTarget.normal, force_event: bool = False, @@ -274,23 +285,13 @@ def click_element(tab: apitypes.Tab, filter_: str, value: str=None, *, do_click(elems[0]) - def wrap_find_at_pos(value: str, - callback: Callable[[Optional[apitypes.WebElement]], None] - ) -> None: - try: - x, y = map(int, value.split(',', maxsplit=1)) - except ValueError: - message.error(f"'{value}' is not a valid point!") - return - tab.elements.find_at_pos(QPoint(x, y), callback) - if value is None and filter_ != 'focused': raise cmdutils.CommandError("Argument 'value' is only optional with filter 'focused'!") handlers = { 'id': (tab.elements.find_id, value, single_cb), 'css': (tab.elements.find_css, value, multiple_cb, message.error), - 'position': (wrap_find_at_pos, value, single_cb), + 'position': (_wrap_find_at_pos, value, tab, single_cb), 'focused': (tab.elements.find_focused, single_cb), } handler, *arguments = handlers[filter_] -- cgit v1.2.3-54-g00ecf From 93e548eccffe662e451b136b1d1857d2cc90b299 Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Wed, 21 Jul 2021 10:18:32 +0200 Subject: Added tests for click-element focused. --- tests/end2end/data/click_element.html | 2 +- tests/end2end/features/misc.feature | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/tests/end2end/data/click_element.html b/tests/end2end/data/click_element.html index c0783b4dd..b2a691e08 100644 --- a/tests/end2end/data/click_element.html +++ b/tests/end2end/data/click_element.html @@ -7,7 +7,7 @@ "Don't", he shouted Duplicate Duplicate -
+
link ID with dot Date: Wed, 21 Jul 2021 10:36:15 +0200 Subject: Fixed too long line. --- qutebrowser/components/misccommands.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/qutebrowser/components/misccommands.py b/qutebrowser/components/misccommands.py index 5f182aa20..42a285115 100644 --- a/qutebrowser/components/misccommands.py +++ b/qutebrowser/components/misccommands.py @@ -286,7 +286,8 @@ def click_element(tab: apitypes.Tab, filter_: str, value: str = None, *, do_click(elems[0]) if value is None and filter_ != 'focused': - raise cmdutils.CommandError("Argument 'value' is only optional with filter 'focused'!") + raise cmdutils.CommandError("Argument 'value' is only" + "optional with filter 'focused'!") handlers = { 'id': (tab.elements.find_id, value, single_cb), -- cgit v1.2.3-54-g00ecf From eb1e49c46841671faa5724f83ad38250bfbf96c8 Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Fri, 1 Apr 2022 20:45:16 +0200 Subject: Fixed spaces at line end. --- tests/end2end/features/misc.feature | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/end2end/features/misc.feature b/tests/end2end/features/misc.feature index d71503c77..4482e278d 100644 --- a/tests/end2end/features/misc.feature +++ b/tests/end2end/features/misc.feature @@ -460,7 +460,7 @@ Feature: Various utility commands. Scenario: Clicking an element by CSS selector When I open data/click_element.html And I run :click-element css .clickable - Then the javascript message "click_element CSS selector" should be logged + Then the javascript message "click_element CSS selector" should be logged Scenario: Clicking an element with non-unique filter When I open data/click_element.html @@ -474,7 +474,7 @@ Feature: Various utility commands. Scenario: Clicking an element by position When I open data/click_element.html And I run :click-element position 20,42 - Then the javascript message "click_element position" should be logged + Then the javascript message "click_element position" should be logged Scenario: Clicking an element with invalid position When I open data/click_element.html -- cgit v1.2.3-54-g00ecf From ae61f0283140e3cccee26f4d491526fd8788d2d8 Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Wed, 6 Apr 2022 19:37:54 +0200 Subject: Add parse_point utility and tests for it. --- qutebrowser/utils/utils.py | 17 ++++++++++++++++- tests/unit/utils/test_utils.py | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/qutebrowser/utils/utils.py b/qutebrowser/utils/utils.py index 9c68932f3..761e583f9 100644 --- a/qutebrowser/utils/utils.py +++ b/qutebrowser/utils/utils.py @@ -44,7 +44,7 @@ except ImportError: # pragma: no cover """Empty stub at runtime.""" -from PyQt5.QtCore import QUrl, QVersionNumber, QRect +from PyQt5.QtCore import QUrl, QVersionNumber, QRect, QPoint from PyQt5.QtGui import QClipboard, QDesktopServices from PyQt5.QtWidgets import QApplication @@ -856,3 +856,18 @@ def parse_rect(s: str) -> QRect: raise ValueError("Invalid rectangle") return rect + + +def parse_point(s: str) -> QPoint: + """Parse a point string like 13,-42""" + try: + x, y = map(int, s.split(',', maxsplit=1)) + except ValueError as e: + raise ValueError(f"String {s} does not match X,Y") + + try: + point = QPoint(x, y) + except OverflowError as e: + raise ValueError(e) + + return point diff --git a/tests/unit/utils/test_utils.py b/tests/unit/utils/test_utils.py index 330ef3b96..d1e4074ec 100644 --- a/tests/unit/utils/test_utils.py +++ b/tests/unit/utils/test_utils.py @@ -30,7 +30,7 @@ import shlex import math import operator -from PyQt5.QtCore import QUrl, QRect +from PyQt5.QtCore import QUrl, QRect, QPoint from PyQt5.QtGui import QClipboard import pytest import hypothesis @@ -1026,3 +1026,42 @@ class TestParseRect: utils.parse_rect(s) except ValueError as e: print(e) + + +class TestParsePoint: + + @pytest.mark.parametrize('value, expected', [ + ('1,1', QPoint(1, 1)), + ('123,789', QPoint(123, 789)), + ('-123,-789', QPoint(-123, -789)), + ]) + def test_valid(self, value, expected): + assert utils.parse_point(value) == expected + + @pytest.mark.parametrize('value, message', [ + ('1x1', "String 1x1 does not match X,Y"), + ('1e0,1', "String 1e0,1 does not match X,Y"), + ('a,1', "String a,1 does not match X,Y"), + ('¹,1', "String ¹,1 does not match X,Y"), + ]) + def test_invalid(self, value, message): + with pytest.raises(ValueError) as excinfo: + utils.parse_point(value) + assert str(excinfo.value) == message + + @hypothesis.given(strategies.text()) + def test_hypothesis_text(self, s): + try: + utils.parse_point(s) + except ValueError as e: + print(e) + + @hypothesis.given(strategies.tuples( + strategies.integers(), + strategies.integers(), + ).map(lambda tpl: f'{tpl[0]},{tpl[1]}')) + def test_hypothesis_sophisticated(self, s): + try: + utils.parse_point(s) + except ValueError as e: + print(e) -- cgit v1.2.3-54-g00ecf From ea5b9dbdc57474aadb2ae021cdd7d7e765893ea9 Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Wed, 6 Apr 2022 19:38:13 +0200 Subject: Use parse_point in click-element parsing. --- qutebrowser/components/misccommands.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/qutebrowser/components/misccommands.py b/qutebrowser/components/misccommands.py index 42a285115..dce54d25a 100644 --- a/qutebrowser/components/misccommands.py +++ b/qutebrowser/components/misccommands.py @@ -227,11 +227,11 @@ def _wrap_find_at_pos(value: str, tab: apitypes.Tab, callback: Callable[[Optional[apitypes.WebElement]], None] ) -> None: try: - x, y = map(int, value.split(',', maxsplit=1)) - except ValueError: - message.error(f"'{value}' is not a valid point!") + point = utils.parse_point(value) + except ValueError as e: + message.error(f"'{value}' is not a valid point: {e}") return - tab.elements.find_at_pos(QPoint(x, y), callback) + tab.elements.find_at_pos(point, callback) @cmdutils.register() -- cgit v1.2.3-54-g00ecf From 9b856191777c37917d8ff691b47ffddedf6cfdca Mon Sep 17 00:00:00 2001 From: Nicholas42 Date: Wed, 6 Apr 2022 19:39:24 +0200 Subject: Change documentation of click-element as suggested. Co-authored-by: Florian Bruhin --- qutebrowser/components/misccommands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qutebrowser/components/misccommands.py b/qutebrowser/components/misccommands.py index dce54d25a..fb177fb68 100644 --- a/qutebrowser/components/misccommands.py +++ b/qutebrowser/components/misccommands.py @@ -253,7 +253,7 @@ def click_element(tab: apitypes.Tab, filter_: str, value: str = None, *, - id: Get an element based on its ID. - css: Filter by a CSS selector. - position: Click the element at specified position. - Write value as 'x,y'. + Specify `value` as 'x,y'. - focused: Click the currently focused element. value: The value to filter for. Optional for 'focused' filter. target: How to open the clicked element (normal/tab/tab-bg/window). -- cgit v1.2.3-54-g00ecf From e6083ac7eb2bf90a455a2d7ab22f332141a8c97c Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Wed, 6 Apr 2022 19:40:16 +0200 Subject: Update asciidoc. --- doc/help/commands.asciidoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/help/commands.asciidoc b/doc/help/commands.asciidoc index 2363b9499..e84e36c3a 100644 --- a/doc/help/commands.asciidoc +++ b/doc/help/commands.asciidoc @@ -266,7 +266,7 @@ The given filter needs to result in exactly one element, otherwise, an error is - id: Get an element based on its ID. - css: Filter by a CSS selector. - position: Click the element at specified position. - Write value as 'x,y'. + Specify `value` as 'x,y'. - focused: Click the currently focused element. * +'value'+: The value to filter for. Optional for 'focused' filter. -- cgit v1.2.3-54-g00ecf From 709592b46a9330f444cc7758928459653bc164bb Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Wed, 6 Apr 2022 20:09:03 +0200 Subject: Adapted expected errors in end2end test for click-element. --- tests/end2end/features/misc.feature | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/end2end/features/misc.feature b/tests/end2end/features/misc.feature index 4482e278d..007393fbb 100644 --- a/tests/end2end/features/misc.feature +++ b/tests/end2end/features/misc.feature @@ -479,12 +479,12 @@ Feature: Various utility commands. Scenario: Clicking an element with invalid position When I open data/click_element.html And I run :click-element position 20.42 - Then the error "'20.42' is not a valid point!" should be shown + Then the error "'20.42' is not a valid point: String 20.42 does not match X,Y" should be shown Scenario: Clicking an element with non-integer position When I open data/click_element.html And I run :click-element position 20,42.001 - Then the error "'20,42.001' is not a valid point!" should be shown + Then the error "'20,42.001' is not a valid point: String 20,42.001 does not match X,Y" should be shown Scenario: Clicking on focused element when there is none When I open data/click_element.html -- cgit v1.2.3-54-g00ecf From 3f989a48689050a7b30a470fee550ec533c6198a Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Wed, 6 Apr 2022 20:09:37 +0200 Subject: Simplified code for click-element switching between backend-functions. --- qutebrowser/components/misccommands.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/qutebrowser/components/misccommands.py b/qutebrowser/components/misccommands.py index fb177fb68..029b8d26e 100644 --- a/qutebrowser/components/misccommands.py +++ b/qutebrowser/components/misccommands.py @@ -290,13 +290,12 @@ def click_element(tab: apitypes.Tab, filter_: str, value: str = None, *, "optional with filter 'focused'!") handlers = { - 'id': (tab.elements.find_id, value, single_cb), - 'css': (tab.elements.find_css, value, multiple_cb, message.error), - 'position': (_wrap_find_at_pos, value, tab, single_cb), - 'focused': (tab.elements.find_focused, single_cb), + 'id': lambda: tab.elements.find_id(elem_id=value, callback=single_cb), + 'css': lambda: tab.elements.find_css(selector=value, callback=multiple_cb, error_cb=message.error), + 'position': lambda: _wrap_find_at_pos(value=value, tab=tab, callback=single_cb), + 'focused': lambda: tab.elements.find_focused(callback=single_cb), } - handler, *arguments = handlers[filter_] - handler(*arguments) + handlers[filter_]() @cmdutils.register(debug=True) -- cgit v1.2.3-54-g00ecf From da9c56ed03327eadf4c2dbaae109da1f00e212ad Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Wed, 6 Apr 2022 20:13:44 +0200 Subject: Remove unused variable. --- qutebrowser/utils/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qutebrowser/utils/utils.py b/qutebrowser/utils/utils.py index 761e583f9..7d6724c15 100644 --- a/qutebrowser/utils/utils.py +++ b/qutebrowser/utils/utils.py @@ -862,7 +862,7 @@ def parse_point(s: str) -> QPoint: """Parse a point string like 13,-42""" try: x, y = map(int, s.split(',', maxsplit=1)) - except ValueError as e: + except ValueError: raise ValueError(f"String {s} does not match X,Y") try: -- cgit v1.2.3-54-g00ecf From 27313ed3552870f0bb1dc626d1966fdd7fd43719 Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Wed, 6 Apr 2022 22:17:22 +0200 Subject: Remove unused import. --- qutebrowser/components/misccommands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qutebrowser/components/misccommands.py b/qutebrowser/components/misccommands.py index 029b8d26e..b9e4494da 100644 --- a/qutebrowser/components/misccommands.py +++ b/qutebrowser/components/misccommands.py @@ -34,7 +34,7 @@ try: except ImportError: hunter = None -from PyQt5.QtCore import Qt, QPoint +from PyQt5.QtCore import Qt from PyQt5.QtPrintSupport import QPrintPreviewDialog from qutebrowser.api import cmdutils, apitypes, message, config -- cgit v1.2.3-54-g00ecf From f1b6fdf9a3e95788ac34b38e1ed22b46b7914605 Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Wed, 6 Apr 2022 22:17:39 +0200 Subject: Make docstring conformant. --- qutebrowser/utils/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qutebrowser/utils/utils.py b/qutebrowser/utils/utils.py index 7d6724c15..d8959514e 100644 --- a/qutebrowser/utils/utils.py +++ b/qutebrowser/utils/utils.py @@ -859,7 +859,7 @@ def parse_rect(s: str) -> QRect: def parse_point(s: str) -> QPoint: - """Parse a point string like 13,-42""" + """Parse a point string like 13,-42.""" try: x, y = map(int, s.split(',', maxsplit=1)) except ValueError: -- cgit v1.2.3-54-g00ecf From 9a706b49953aaea69060e19eb9e50fbaebfb6def Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Wed, 6 Apr 2022 22:17:58 +0200 Subject: Make linters happy. --- qutebrowser/components/misccommands.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/qutebrowser/components/misccommands.py b/qutebrowser/components/misccommands.py index b9e4494da..a50246301 100644 --- a/qutebrowser/components/misccommands.py +++ b/qutebrowser/components/misccommands.py @@ -27,7 +27,7 @@ import signal import functools import logging import pathlib -from typing import Optional, List, Callable +from typing import Optional, List, Callable, cast try: import hunter @@ -291,8 +291,10 @@ def click_element(tab: apitypes.Tab, filter_: str, value: str = None, *, handlers = { 'id': lambda: tab.elements.find_id(elem_id=value, callback=single_cb), - 'css': lambda: tab.elements.find_css(selector=value, callback=multiple_cb, error_cb=message.error), - 'position': lambda: _wrap_find_at_pos(value=value, tab=tab, callback=single_cb), + 'css': lambda: + tab.elements.find_css(value, callback=multiple_cb, error_cb=message.error), + 'position': lambda: + _wrap_find_at_pos(cast(str, value), tab=tab, callback=single_cb), 'focused': lambda: tab.elements.find_focused(callback=single_cb), } handlers[filter_]() -- cgit v1.2.3-54-g00ecf From d8af9617d90511cc5f41c933b11c66ca94e82add Mon Sep 17 00:00:00 2001 From: Nicholas42 Date: Fri, 8 Apr 2022 13:54:30 +0200 Subject: Simplify unit test for click-element. Co-authored-by: Florian Bruhin --- tests/unit/utils/test_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/utils/test_utils.py b/tests/unit/utils/test_utils.py index d1e4074ec..d4fa4d2aa 100644 --- a/tests/unit/utils/test_utils.py +++ b/tests/unit/utils/test_utils.py @@ -1059,7 +1059,7 @@ class TestParsePoint: @hypothesis.given(strategies.tuples( strategies.integers(), strategies.integers(), - ).map(lambda tpl: f'{tpl[0]},{tpl[1]}')) + ).map(",".join) def test_hypothesis_sophisticated(self, s): try: utils.parse_point(s) -- cgit v1.2.3-54-g00ecf From c48719f014bc62616b1396d9e9ef4e03c882094e Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Fri, 8 Apr 2022 14:00:39 +0200 Subject: Fix missing conversion to str in unit test hypothesis. --- tests/unit/utils/test_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/utils/test_utils.py b/tests/unit/utils/test_utils.py index d4fa4d2aa..21063c669 100644 --- a/tests/unit/utils/test_utils.py +++ b/tests/unit/utils/test_utils.py @@ -1059,7 +1059,7 @@ class TestParsePoint: @hypothesis.given(strategies.tuples( strategies.integers(), strategies.integers(), - ).map(",".join) + ).map(lambda t: ",".join(map(str, t)))) def test_hypothesis_sophisticated(self, s): try: utils.parse_point(s) -- cgit v1.2.3-54-g00ecf From e7cf3e41bca4c2a13f3fd0fbbd748b1c2fb47ae2 Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Fri, 8 Apr 2022 14:00:57 +0200 Subject: Add more unit tests. --- tests/unit/utils/test_utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/unit/utils/test_utils.py b/tests/unit/utils/test_utils.py index 21063c669..b05b4edc8 100644 --- a/tests/unit/utils/test_utils.py +++ b/tests/unit/utils/test_utils.py @@ -1043,6 +1043,8 @@ class TestParsePoint: ('1e0,1', "String 1e0,1 does not match X,Y"), ('a,1', "String a,1 does not match X,Y"), ('¹,1', "String ¹,1 does not match X,Y"), + ('1,,1', "String 1,,1 does not match X,Y"), + ('1', "String 1 does not match X,Y"), ]) def test_invalid(self, value, message): with pytest.raises(ValueError) as excinfo: -- cgit v1.2.3-54-g00ecf From bf562a74fadba3d87c9ef221af3debdee8cd525f Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Fri, 8 Apr 2022 14:05:16 +0200 Subject: Added missing Then in test case for click-element. --- tests/end2end/features/misc.feature | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/end2end/features/misc.feature b/tests/end2end/features/misc.feature index 007393fbb..17d79da95 100644 --- a/tests/end2end/features/misc.feature +++ b/tests/end2end/features/misc.feature @@ -470,6 +470,7 @@ Feature: Various utility commands. Scenario: Clicking first element matching a selector When I open data/click_element.html And I run :click-element --select-first css span + Then the javascript message "click_element clicked" should be logged Scenario: Clicking an element by position When I open data/click_element.html -- cgit v1.2.3-54-g00ecf From 39dbf2266f14fa89458337636f75f6adee35ee9a Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Fri, 8 Apr 2022 14:25:07 +0200 Subject: Remove duplicate value in error message for invalid point. --- qutebrowser/components/misccommands.py | 2 +- tests/end2end/features/misc.feature | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/qutebrowser/components/misccommands.py b/qutebrowser/components/misccommands.py index a50246301..0ba1370d2 100644 --- a/qutebrowser/components/misccommands.py +++ b/qutebrowser/components/misccommands.py @@ -229,7 +229,7 @@ def _wrap_find_at_pos(value: str, tab: apitypes.Tab, try: point = utils.parse_point(value) except ValueError as e: - message.error(f"'{value}' is not a valid point: {e}") + message.error(str(e)) return tab.elements.find_at_pos(point, callback) diff --git a/tests/end2end/features/misc.feature b/tests/end2end/features/misc.feature index 17d79da95..87aaf8cde 100644 --- a/tests/end2end/features/misc.feature +++ b/tests/end2end/features/misc.feature @@ -480,12 +480,12 @@ Feature: Various utility commands. Scenario: Clicking an element with invalid position When I open data/click_element.html And I run :click-element position 20.42 - Then the error "'20.42' is not a valid point: String 20.42 does not match X,Y" should be shown + Then the error "String 20.42 does not match X,Y" should be shown Scenario: Clicking an element with non-integer position When I open data/click_element.html And I run :click-element position 20,42.001 - Then the error "'20,42.001' is not a valid point: String 20,42.001 does not match X,Y" should be shown + Then the error "String 20,42.001 does not match X,Y" should be shown Scenario: Clicking on focused element when there is none When I open data/click_element.html -- cgit v1.2.3-54-g00ecf From d1bc60edb8d577814dab52c536f326083a66ef62 Mon Sep 17 00:00:00 2001 From: Nicholas Schwab Date: Fri, 8 Apr 2022 14:25:30 +0200 Subject: Adapte errors on click-element to filter. --- qutebrowser/components/misccommands.py | 14 +++++++++++--- tests/end2end/features/misc.feature | 6 +++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/qutebrowser/components/misccommands.py b/qutebrowser/components/misccommands.py index 0ba1370d2..3b6394e38 100644 --- a/qutebrowser/components/misccommands.py +++ b/qutebrowser/components/misccommands.py @@ -234,6 +234,14 @@ def _wrap_find_at_pos(value: str, tab: apitypes.Tab, tab.elements.find_at_pos(point, callback) +_FILTER_ERRORS = { + 'id': lambda x: f'with ID "{x}"', + 'css': lambda x: f'matching CSS selector "{x}"', + 'focused': lambda _: 'with focus', + 'position': lambda x: 'at position {x}', +} + + @cmdutils.register() @cmdutils.argument('tab', value=cmdutils.Value.cur_tab) @cmdutils.argument('filter_', choices=['id', 'css', 'position', 'focused']) @@ -269,18 +277,18 @@ def click_element(tab: apitypes.Tab, filter_: str, value: str = None, *, def single_cb(elem: Optional[apitypes.WebElement]) -> None: """Click a single element.""" if elem is None: - message.error(f"No element found matching {filter_}={value}!") + message.error(f"No element found {_FILTER_ERRORS[filter_](value)}!") return do_click(elem) def multiple_cb(elems: List[apitypes.WebElement]) -> None: if not elems: - message.error(f"No element found matching {filter_}={value}!") + message.error(f"No element found {_FILTER_ERRORS[filter_](value)}!") return if not select_first and len(elems) > 1: - message.error(f"Multiple elements found matching {filter_}={value}!") + message.error(f"Multiple elements found {_FILTER_ERRORS[filter_](value)}!") return do_click(elems[0]) diff --git a/tests/end2end/features/misc.feature b/tests/end2end/features/misc.feature index 87aaf8cde..c070a9ea3 100644 --- a/tests/end2end/features/misc.feature +++ b/tests/end2end/features/misc.feature @@ -436,7 +436,7 @@ Feature: Various utility commands. Scenario: Clicking an element with unknown ID When I open data/click_element.html And I run :click-element id blah - Then the error "No element found matching id=blah!" should be shown + Then the error "No element found with ID "blah"!" should be shown Scenario: Clicking an element by ID When I open data/click_element.html @@ -465,7 +465,7 @@ Feature: Various utility commands. Scenario: Clicking an element with non-unique filter When I open data/click_element.html And I run :click-element css span - Then the error "Multiple elements found matching css=span!" should be shown + Then the error "Multiple elements found matching CSS selector "span"!" should be shown Scenario: Clicking first element matching a selector When I open data/click_element.html @@ -493,7 +493,7 @@ Feature: Various utility commands. And I run :click-element position 20,42 And I wait for the javascript message "click_element position" And I run :click-element focused - Then the error "No element found matching focused=None!" should be shown + Then the error "No element found with focus!" should be shown Scenario: Clicking on focused element When I open data/click_element.html -- cgit v1.2.3-54-g00ecf