Skip to content

Commit ceef32a

Browse files
committed
Added tests for pop-up windows support (closes cobrateam#161)
1 parent 38cb3c0 commit ceef32a

5 files changed

Lines changed: 57 additions & 1 deletion

File tree

‎tests/base.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from mouse_interaction import MouseInteractionTest
2121
from status_code import StatusCodeTest
2222
from type import SlowlyTypeTest
23+
from popups import PopupWindowsTest
2324

2425

2526
class BaseBrowserTests(ElementTest, FindElementsTest, FormElementsTest, ClickElementsTest, CookiesTest, SlowlyTypeTest):
@@ -87,7 +88,7 @@ def test_should_receive_browser_on_parent(self):
8788
self.assertEqual(self.browser, element.parent)
8889

8990

90-
class WebDriverTests(BaseBrowserTests, IFrameElementsTest, ElementDoestNotExistTest, IsElementPresentTest, AsyncFinderTests, IsTextPresentTest, StatusCodeTest, MouseInteractionTest):
91+
class WebDriverTests(BaseBrowserTests, IFrameElementsTest, ElementDoestNotExistTest, IsElementPresentTest, AsyncFinderTests, IsTextPresentTest, StatusCodeTest, MouseInteractionTest, PopupWindowsTest):
9192

9293
def test_can_execute_javascript(self):
9394
"should be able to execute javascript"

‎tests/fake_webapp.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def read_static(static_name):
1919
EXAMPLE_IFRAME_HTML = read_static('iframe.html')
2020
EXAMPLE_ALERT_HTML = read_static('alert.html')
2121
EXAMPLE_TYPE_HTML = read_static('type.html')
22+
EXAMPLE_POPUP_HTML = read_static('popup.html')
2223

2324
app = Flask(__name__)
2425

@@ -77,6 +78,11 @@ def query_string():
7778
abort(500)
7879

7980

81+
@app.route('/popup')
82+
def popup():
83+
return EXAMPLE_POPUP_HTML
84+
85+
8086
def start_flask_app(host, port):
8187
"""Runs the server."""
8288
app.run(host=host, port=port)

‎tests/popups.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Copyright 2012 splinter authors. All rights reserved.
4+
# Use of this source code is governed by a BSD-style
5+
# license that can be found in the LICENSE file.
6+
7+
8+
class PopupWindowsTest(object):
9+
10+
def test_can_work_on_popups(self):
11+
''' Can work on pop-up windows and switch back to the parent '''
12+
parent_window = self.browser.current_window # Save the parent window
13+
self.browser.find_by_id("open-popup").first.click()
14+
15+
for window in self.browser.windows:
16+
self.browser.switch_to_window(window) # Switch to a different window (the pop-up)
17+
# Check if this is the one we want by comparing the title
18+
if self.browser.title == "Pop-up Window":
19+
break
20+
# Most of the times there are only two windows - the parent and the pop-up
21+
# In this case the following condition also does the trick
22+
# if window != browser.parent_window:
23+
# break
24+
25+
value = self.browser.find_by_tag('h1').first.value
26+
self.assertEqual(value, 'Pop-up Example Header') # Proves we can interact with the popup
27+
self.browser.find_by_id("close-popup").first.click()
28+
29+
self.browser.switch_to_window(parent_window) # Switch to the main window again
30+
value = self.browser.find_by_tag('h1').first.value
31+
self.assertEqual(value, 'Example Header') # Proves we can interact with the parent

‎tests/static/index.html‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,12 @@ <h2>inside</h2>
124124
<div class="dragged">no</div>
125125
<div class="has-class-first has-class-middle has-class-end"></div>
126126
<div id="html-property" class="outer html > classes">inner <div class="inner-html">inner text</div> html test</div>
127+
<a id="open-popup" href="javascript:poptastic('/popup')">Open pop-up window</a>
128+
<script>
129+
function poptastic(url) {
130+
var pop = window.open(url, 'name', 'width=300, height=300');
131+
if (window.focus) { pop.focus() }
132+
}
133+
</script>
127134
</body>
128135
</html>

‎tests/static/popup.html‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE HTML>
2+
3+
<html>
4+
<head>
5+
<title>Pop-up Window</title>
6+
</head>
7+
<body>
8+
<h1>Pop-up Example Header</h1>
9+
<a id="close-popup" href="javascript:window.close()">Close this window</a>
10+
</body>
11+
</html>

0 commit comments

Comments
 (0)