summaryrefslogtreecommitdiff
path: root/chromium/tools/telemetry/telemetry/core/backends/webdriver/webdriver_tab_list_backend.py
blob: ea9c8346c4b4520d875b48883993db5860efee67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from telemetry.core.backends.webdriver import webdriver_tab_backend

class WebDriverTabListBackend(object):
  def __init__(self, browser_backend):
    self._browser_backend = browser_backend
    # Stores the window handles.
    self._tab_list = []

  def Init(self):
    self._UpdateTabList()

  def New(self, timeout=None):
    # Webdriver API doesn't support tab controlling.
    raise NotImplementedError()

  def __iter__(self):
    self._UpdateTabList()
    return self._tab_list.__iter__()

  def __len__(self):
    self._UpdateTabList()
    return len(self._tab_list)

  def __getitem__(self, index):
    self._UpdateTabList()
    if len(self._tab_list) <= index:
      raise IndexError('list index out of range')
    return self._tab_list[index]

  def _UpdateTabList(self):
    window_handles = self._browser_backend.driver.window_handles
    old_tab_list = self._tab_list
    self._tab_list = []
    for window_handle in window_handles:
      tab = None
      for old_tab in old_tab_list:
        if old_tab.window_handle == window_handle:
          tab = old_tab
          break
      else:
        tab = webdriver_tab_backend.WebDriverTabBackend(
            self._browser_backend, window_handle)
      self._tab_list.append(tab)