Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented Playwrights Highlighting #3828

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Browser/keywords/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from ..base import LibraryComponent
from ..generated.playwright_pb2 import Request
from ..utils import DownloadInfo, keyword, logger
from ..utils import DownloadInfo, HighlightMode, keyword, logger


class Evaluation(LibraryComponent):
Expand Down Expand Up @@ -86,6 +86,8 @@ def highlight_elements(
width: str = "2px",
style: str = "dotted",
color: str = "blue",
*,
mode: HighlightMode = HighlightMode.border,
):
"""Adds a highlight to elements matched by the ``selector``. Provides a style adjustment.

Expand All @@ -95,10 +97,11 @@ def highlight_elements(

| =Arguments= | =Description= |
| ``selector`` | Selectors which shall be highlighted. See the `Finding elements` section for details about the selectors. |
| ``duration`` | Sets for how long the selector shall be highlighted. Defaults to ``5s`` => 5 seconds. |
| ``duration`` | Sets for how long the selector shall be highlighted. Defaults to ``5s`` => 5 seconds. If set to 0 seconds, the highlighting is not deleted. |
| ``width`` | Sets the width of the higlight border. Defaults to 2px. |
| ``style`` | Sets the style of the border. Defaults to dotted. |
| ``color`` | Sets the color of the border. Valid colors i.e. are: ``red``, ``blue``, ``yellow``, ``pink``, ``black`` |
| ``mode`` | Sets the mode of the highlight. Valid modes are: ``border`` (classic mode), ``playwright`` (Playwrights native). If ``playwright`` is used, ``width``, ``style`` and ``color`` is ignored and only one highlighting can happen at the same time. |

Keyword does not fail if selector resolves to multiple elements.

Expand All @@ -118,6 +121,7 @@ def highlight_elements(
style=style,
color=color,
strict=False,
mode=mode.name,
)
)
count: int = response.body
Expand Down
1 change: 1 addition & 0 deletions Browser/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
FormatterTypes,
GeoLocation,
HighLightElement,
HighlightMode,
HttpCredentials,
LambdaFunction,
NewPageDetails,
Expand Down
13 changes: 13 additions & 0 deletions Browser/utils/data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,19 @@ class HighLightElement(TypedDict):
color: str


class HighlightMode(Enum):
"""Highlight mode for the element.

``border``: Highlights the element with a border outside of the selected element.
This is the classic way to highlight an element of Browser librarary.

``playwright``: Highlights the element with Playwrights built in function.
"""

border = auto()
playwright = auto()


class LambdaFunction:
@classmethod
def from_string(cls, string: str) -> "LambdaFunction":
Expand Down
30 changes: 26 additions & 4 deletions node/playwright-wrapper/evaluation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,17 @@ export async function highlightElements(
const style = request.getStyle();
const color = request.getColor();
const strictMode = request.getStrict();
const count = await highlightAll(selector, duration, width, style, color, strictMode, state);
const mode = request.getMode();
const count = await highlightAll(
selector,
duration,
width,
style,
color,
strictMode,
state,
mode as 'border' | 'playwright',
);
return intResponse(count, `Highlighted ${count} elements for ${duration}.`);
}

Expand All @@ -452,6 +462,7 @@ async function highlightAll(
color: string,
strictMode: boolean,
state: PlaywrightState,
mode: 'border' | 'playwright' = 'border',
): Promise<number> {
const locator = await findLocator(state, selector, strictMode, undefined, false);
let count: number;
Expand All @@ -462,6 +473,15 @@ async function highlightAll(
return 0;
}
logger.info(`Locator count is ${count}`);
if (mode === 'playwright') {
locator.highlight();
if (duration !== 0) {
setTimeout(() => {
state.getActivePage()?.locator('none.highlight-no-element').highlight();
}, duration);
}
return count;
}
await locator.evaluateAll(
(elements: Array<Element>, options: EvaluationOptions) => {
elements.forEach((e: Element) => {
Expand All @@ -478,9 +498,11 @@ async function highlightAll(
d.style.border = `${options?.wdt ?? '1px'} ${options?.stl ?? `dotted`} ${options?.clr ?? `blue`}`;
d.style.boxSizing = 'content-box';
document.body.appendChild(d);
setTimeout(() => {
d.remove();
}, options?.dur ?? 5000);
if (options?.dur !== 0) {
setTimeout(() => {
d.remove();
}, options?.dur ?? 5000);
}
});
},
{ dur: duration, wdt: width, stl: style, clr: color },
Expand Down
1 change: 1 addition & 0 deletions protobuf/playwright.proto
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ message Request {
string style = 4;
string color = 5;
bool strict = 6;
string mode = 7;
}

message SelectElementSelector {
Expand Down