• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
8import re
9from typing import TYPE_CHECKING, Optional, Type
10
11from crossbench.action_runner.action.action import (ACTION_TIMEOUT, Action,
12                                                    ActionT)
13from crossbench.action_runner.action.action_type import ActionType
14from crossbench.parse import NumberParser, ObjectParser
15
16if TYPE_CHECKING:
17  from crossbench.action_runner.base import ActionRunner
18  from crossbench.config import ConfigParser
19  from crossbench.runner.run import Run
20  from crossbench.types import JsonDict
21
22
23class SwitchTabAction(Action):
24  TYPE: ActionType = ActionType.SWITCH_TAB
25
26  @classmethod
27  def config_parser(cls: Type[ActionT]) -> ConfigParser[ActionT]:
28    parser = super().config_parser()
29    parser.add_argument(
30        "tab_index",
31        type=NumberParser.any_int,
32        help=(
33            "The index of the tab to switch to. Tabs are indexed in creation "
34            "order. Negative values are allowed, e.g. -1 is the most recently "
35            "opened tab."))
36    parser.add_argument("title", type=ObjectParser.regexp)
37    parser.add_argument("url", type=ObjectParser.regexp)
38    return parser
39
40  def __init__(self,
41               tab_index: Optional[int] = None,
42               title: Optional[re.Pattern] = None,
43               url: Optional[re.Pattern] = None,
44               timeout: dt.timedelta = ACTION_TIMEOUT,
45               index: int = 0) -> None:
46    self._title = title
47    self._url = url
48    self._tab_index = tab_index
49    super().__init__(timeout, index)
50
51  @property
52  def title(self) -> Optional[re.Pattern]:
53    return self._title
54
55  @property
56  def url(self) -> Optional[re.Pattern]:
57    return self._url
58
59  @property
60  def tab_index(self) -> Optional[int]:
61    return self._tab_index
62
63  def run_with(self, run: Run, action_runner: ActionRunner) -> None:
64    action_runner.switch_tab(run, self)
65
66  def to_json(self) -> JsonDict:
67    details = super().to_json()
68    if self._tab_index:
69      details["tab_index"] = self._tab_index
70    if self._title:
71      details["title"] = str(self._title.pattern)
72    if self._url:
73      details["url"] = str(self._url.pattern)
74    return details
75