Skip to content

Commit

Permalink
feat: add type_key action
Browse files Browse the repository at this point in the history
  • Loading branch information
adeprez committed Oct 4, 2024
1 parent 38d7e3f commit f1ea68f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ def switch_tab(self, tab_id: int) -> None:
window_handles = self.driver.window_handles
self.driver.switch_to.window(window_handles[tab_id])

def type_key(self, key: str) -> None:
"""Type a key"""
ActionChains(self.driver).send_keys(key).perform()

def resolve_xpath(self, xpath: str):
"""
Return the element for the corresponding xpath, the underlying driver may switch iframe if necessary
Expand Down
34 changes: 27 additions & 7 deletions lavague-sdk/lavague/sdk/base_driver/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
from abc import ABC, abstractmethod
from contextlib import contextmanager
from datetime import datetime
from typing import Callable, Dict, List, Optional, Union, TypeVar, Generic
from pydantic import BaseModel
from lavague.sdk.action.navigation import NavigationOutput
from typing import Callable, Dict, Generic, List, Optional, TypeVar, Union

from lavague.sdk.action.navigation import NavigationCommand, NavigationOutput
from lavague.sdk.base_driver.interaction import (
InteractionType,
PossibleInteractionsByXpath,
ScrollDirection,
)
from lavague.sdk.base_driver.node import DOMNode
from pydantic import BaseModel


class DriverObservation(BaseModel):
Expand All @@ -35,19 +35,34 @@ def execute(self, action: NavigationOutput) -> None:
"""Execute an action"""
with self.resolve_xpath(action.xpath) as node:
match action.navigation_command:
case InteractionType.CLICK:
case NavigationCommand.CLICK:
node.click()

case InteractionType.TYPE:
case NavigationCommand.SET_VALUE:
node.set_value(action.value or "")

case InteractionType.HOVER:
case NavigationCommand.SET_VALUE_AND_ENTER:
node.set_value((action.value or "") + "\ue007")

case NavigationCommand.TYPE_KEY:
self.type_key(action.value or "")

case NavigationCommand.HOVER:
node.hover()

case InteractionType.SCROLL:
case NavigationCommand.BACK:
self.back()

case NavigationCommand.PASS:
pass

case NavigationCommand.SCROLL:
direction = ScrollDirection.from_string(action.value or "DOWN")
self.scroll(action.xpath, direction)

case NavigationCommand.SWITCH_TAB:
self.switch_tab(int(action.value or "0"))

case _:
raise NotImplementedError(
f"Action {action.navigation_command} not implemented"
Expand Down Expand Up @@ -102,6 +117,11 @@ def switch_tab(self, tab_id: int) -> None:
"""Switch to the tab with the given id"""
pass

@abstractmethod
def type_key(self, key: str) -> None:
"""Type a key"""
pass

@abstractmethod
def resolve_xpath(self, xpath: str) -> T:
"""
Expand Down

0 comments on commit f1ea68f

Please sign in to comment.