Skip to content
Open
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
9 changes: 9 additions & 0 deletions pychrome/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ def activate_tab(self, tab_id, timeout=None):
rp = requests.get("%s/json/activate/%s" % (self.dev_url, tab_id), timeout=timeout)
return rp.text

def get_current_tab(self, timeout=None):
rp = requests.get("%s/json/activate" % self.dev_url, timeout=timeout)
activated_tab_id = rp.json().get('id')

if activated_tab_id in self._tabs:
return self._tabs[activated_tab_id]
else:
return None

def close_tab(self, tab_id, timeout=None):
if isinstance(tab_id, Tab):
tab_id = tab_id.id
Expand Down
20 changes: 20 additions & 0 deletions tests/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ def test_browser_activate_tab():
for tab in tabs:
browser.activate_tab(tab)

def test_browser_get_current_tab():
browser = pychrome.Browser()
tab1 = browser.new_tab()
tab2 = browser.new_tab()

browser.activate_tab(tab1)
current_tab = browser.get_current_tab()
assert current_tab == tab1

browser.activate_tab(tab2)
current_tab = browser.get_current_tab()
assert current_tab == tab2

browser.close_tab(tab2)
current_tab = browser.get_current_tab()
assert current_tab == tab1

browser.close_tab(tab1)
current_tab = browser.get_current_tab()
assert current_tab is None

def test_browser_tabs_map():
browser = pychrome.Browser()
Expand Down