Skip to content

Commit 33ff81a

Browse files
Cong Liurogerwang
authored andcommitted
[test] fix for random orders of devtools in window_handles
DevTools window opened at start may appear before or after the app window in `driver.window_handles`. In versions before nw19, the order of handles is deterministic. This new behavior causes webdriver pointing to devtools window initially sometimes. Therefore all following testing operations will fail of cause. The patch refactored `switch_to_devtools` and added `switch_to_app` helpers. When test starts, invoke `switch_to_app` to make sure webdriver is working on the app window. And use `switch_to_devtools` without `devtools_window` argument to make it iterate exisiting window handles until found a devtools window and switch to it. tests affected: * issue3780-jailed-elements * issue3780-jailed * issue3835-inspect-crash * issue4007-reload-lost-app-window * issue4121-inpect-node-crash * issue4269-click-link-crash * react-devtools-extension
1 parent c58f605 commit 33ff81a

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed

test/sanity/issue3835-inspect-crash/test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
driver.implicitly_wait(2)
1515
time.sleep(1)
1616
try:
17+
switch_to_app(driver)
1718
print driver.current_url
1819
print 'wait for devtools open'
1920
wait_window_handles(driver, 2)
2021
print driver.window_handles
2122
print 'switch to devtools'
22-
switch_to_devtools(driver, devtools_window=driver.window_handles[-1])
23+
switch_to_devtools(driver)
2324
print 'click Console panel'
2425
devtools_click_tab(driver, 'console')
2526
print 'send_keys "chrome<enter>"'

test/sanity/issue4121-inpect-node-crash/test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
driver.implicitly_wait(2)
1515
time.sleep(1)
1616
try:
17+
switch_to_app(driver)
1718
print driver.current_url
1819
driver.find_element_by_id('require').click()
1920
print 'wait for devtools open'
2021
wait_window_handles(driver, 2)
2122
print 'switch to devtools'
22-
switch_to_devtools(driver, devtools_window=driver.window_handles[-1])
23+
switch_to_devtools(driver)
2324
devtools_click_tab(driver, 'console')
2425
driver.find_element_by_class_name('console-message-url').click()
2526
sources_panel = driver.find_element_by_css_selector('.panel.sources')

test/sanity/issue4269-click-link-crash/test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@
3030
driver.implicitly_wait(2)
3131
time.sleep(1)
3232
try:
33+
switch_to_app(driver)
3334
print driver.current_url
3435
print 'wait for devtools window'
3536
wait_window_handles(driver, 3)
3637
print driver.window_handles
3738
print 'switch to devtools window'
38-
switch_to_devtools(driver, devtools_window=driver.window_handles[1]) # devtools comes to the 2nd
39+
switch_to_devtools(driver)
3940
devtools_click_tab(driver, 'console')
4041
driver.find_element_by_css_selector('.console-message-text .webkit-html-external-link').click()
4142
wait_window_handles(driver, 4)

test/sanity/nw_util.py

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,55 @@ def wait_switch_window_name(driver, name, timeout=60):
2828
if timeout <= 0:
2929
raise Exception('Timeout when waiting for window handles')
3030

31+
32+
def switch_to_app(driver, window_handle=None):
33+
def is_app_url(url):
34+
return url.startswith('chrome-extension://') or url.startswith('file://') or url.startswith('http://') or url.startswith('https://')
35+
36+
if window_handle is not None:
37+
driver.switch_to_window(window_handle)
38+
39+
if is_app_url(driver.current_url):
40+
return
41+
42+
elif window_handle is not None: # raise exception when given window is a devtools window
43+
raise Exception('Provided window handle is not an app window. %s' % driver.current_url)
44+
45+
for handle in driver.window_handles:
46+
driver.switch_to_window(handle)
47+
if is_app_url(driver.current_url):
48+
return
49+
50+
raise Exception('No app window found.')
51+
3152
def switch_to_devtools(driver, devtools_window=None):
32-
if devtools_window:
53+
def wait_for_devtools_ready():
54+
# necessary compatible for older alphaN
55+
# where devtools is loaded in an iframe
56+
inspector_frames = driver.find_elements_by_id('inspector-app-iframe')
57+
if inspector_frames:
58+
driver.switch_to_frame(inspector_frames[0])
59+
60+
# wait for devtools is completely loaded
61+
while driver.execute_script('return document.readyState') != 'complete':
62+
time.sleep(1)
63+
64+
if devtools_window is not None:
3365
driver.switch_to_window(devtools_window)
3466

35-
# necessary compatible for older alphaN
36-
# where devtools is loaded in an iframe
37-
inspector_frames = driver.find_elements_by_id('inspector-app-iframe')
38-
if inspector_frames:
39-
driver.switch_to_frame(inspector_frames[0])
67+
if driver.current_url.startswith('chrome-devtools://'):
68+
wait_for_devtools_ready()
69+
return
70+
elif devtools_window is not None: # raise exception when given window is not a devtools
71+
raise Exception('Provided window handle is not a devtools window. %s' % driver.current_url)
4072

41-
# wait for devtools is completely loaded
42-
while driver.execute_script('return document.readyState') != 'complete':
43-
time.sleep(1)
73+
for handle in driver.window_handles:
74+
driver.switch_to_window(handle)
75+
if driver.current_url.startswith('chrome-devtools://'):
76+
wait_for_devtools_ready()
77+
return
78+
79+
raise Exception('No devtools window found.')
4480

4581
def devtools_click_tab(driver, tab_name):
4682
driver.execute_script('return document.querySelector(".tabbed-pane").shadowRoot.getElementById("tab-%s")' % tab_name).click()

0 commit comments

Comments
 (0)