1# Copyright 2024 The Chromium Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5from __future__ import annotations 6 7import datetime as dt 8from typing import TYPE_CHECKING, Type 9 10from crossbench.action_runner.action.action import (ACTION_TIMEOUT, Action, 11 ActionT) 12from crossbench.action_runner.action.action_type import ActionType 13from crossbench.parse import ObjectParser 14 15if TYPE_CHECKING: 16 from crossbench.action_runner.base import ActionRunner 17 from crossbench.config import ConfigParser 18 from crossbench.runner.run import Run 19 from crossbench.types import JsonDict 20 21 22class WaitForElementAction(Action): 23 TYPE: ActionType = ActionType.WAIT_FOR_ELEMENT 24 25 @classmethod 26 def config_parser(cls: Type[ActionT]) -> ConfigParser[ActionT]: 27 parser = super().config_parser() 28 parser.add_argument( 29 "selector", type=ObjectParser.non_empty_str, required=True) 30 return parser 31 32 def __init__(self, 33 selector: str, 34 timeout: dt.timedelta = ACTION_TIMEOUT, 35 index: int = 0): 36 self._selector = selector 37 super().__init__(timeout, index) 38 39 @property 40 def selector(self) -> str: 41 return self._selector 42 43 def run_with(self, run: Run, action_runner: ActionRunner) -> None: 44 action_runner.wait_for_element(run, self) 45 46 def validate(self) -> None: 47 super().validate() 48 if not self.selector: 49 raise ValueError(f"{self}.selector is missing.") 50 51 def to_json(self) -> JsonDict: 52 details = super().to_json() 53 details["selector"] = self.selector 54 return details 55