Skip to content

Commit

Permalink
Handle dialog type windows popping up and closing
Browse files Browse the repository at this point in the history
  • Loading branch information
tomdcc committed Jul 30, 2014
1 parent ee79cd7 commit a44eaa2
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 0 deletions.
23 changes: 23 additions & 0 deletions integration-test/src/cucumber/features/Navigation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,26 @@ Scenario: Redirect with single variable name
Scenario: Redirect with explicit nice parameter name
When I go via the instant redirect page for dog id of '1'
Then I am at 'page2.jsp?dog_id=1'

Scenario: Popups can be closed without switching to them
When I go to the home page
And I click on the popup button
Then another window has popped up
When I close the popped up window
Then I am at the home page

Scenario: Popups can be closed after switching to them
When I go to the home page
And I click on the popup button
Then another window has popped up
When I switch to the popped up window
And I close the popped up window
Then I am at the home page

Scenario: Popups can be closed by interaction
When I go to the home page
And I click on the popup button
Then the second page has popped up
When I click the close button
Then the popped up window has closed
And I am at the home page
2 changes: 2 additions & 0 deletions integration-test/src/main/webapp/index.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,7 @@
<%
}
%>

<div><input type='button' id='popupButton' onclick="window.open('<%= request.getContextPath()%>/page2.jsp')" value="Popup window"></div>
</body>
</html>
1 change: 1 addition & 0 deletions integration-test/src/main/webapp/page2.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
</head>
<body>
<h1>Page 2</h1>
<div><input type='button' id='closeButton' onclick="window.close()" value="Close window"></div>
</body>
</html>
1 change: 1 addition & 0 deletions integration-test/src/test/groovy/pages/HomePage.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ class HomePage extends Page {
static content = {
greeting(required: false) { $('#greeting') }
greetingString(required: false) { $('#greeting')?.text() }
popupButton { $('#popupButton') }
}
}
3 changes: 3 additions & 0 deletions integration-test/src/test/groovy/pages/SecondPage.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ import geb.Page
class SecondPage extends Page {
static at = { title == 'Test app page 2' }
static url = 'page2.jsp'
static content = {
closeButton { $('#closeButton') }
}
}
54 changes: 54 additions & 0 deletions src/main/groovy/io/jdev/geb/cucumber/core/NavigationSteps.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,18 @@ import java.lang.reflect.Modifier

class NavigationSteps extends StepsBase {
PageFinder pageFinder
String mainWindowHandle

public void before (Scenario scenario, Binding binding, PageFinder pageFinder, Decoder variableDecoder) {
super.before(scenario, binding, variableDecoder)
this.pageFinder = pageFinder
}

public void after(Scenario scenario) {
super.after(scenario)
cleanupWindows()
}

public void to(String pageName, Map params = [:]) {
Class<? extends Page> pageClass = pageFinder.getPageClass(pageName)
assert pageClass
Expand Down Expand Up @@ -131,4 +137,52 @@ class NavigationSteps extends StepsBase {
[(paramName): paramValue]
}

public void assertPopup() {
println "assertPopup, windowHandles: $browser.driver.windowHandles, current windowHandle: $browser.driver.windowHandle"
if(mainWindowHandle && mainWindowHandle != browser.driver.windowHandle) {
throw new IllegalStateException("Previous main window handle set but doesn't match current window handle - nested popups not currently supported")
}
mainWindowHandle = browser.driver.windowHandle
browser.waitFor { browser.driver.windowHandles.size() > 1 }
}

public void switchToPopup(String pageName) {
assertPopup()
def newWindowHandle = browser.driver.windowHandles.find { it != mainWindowHandle }
browser.driver.switchTo().window(newWindowHandle)
if(pageName) {
at(pageName)
}
}

public void popupClosed() {
browser.driver.switchTo().window(mainWindowHandle)
browser.waitFor { browser.driver.windowHandles.size() == 1 }
}

public void closePopup() {
println "closePopup, windowHandles: $browser.driver.windowHandles, current windowHandle: $browser.driver.windowHandle"
// just playing it safe
if(!mainWindowHandle) {
mainWindowHandle = browser.driver.windowHandle
}
if(mainWindowHandle == browser.driver.windowHandle) {
// select the window which isn't the main one
String otherHandle = browser.driver.windowHandles.find { it != mainWindowHandle }
assert otherHandle, "Could not find other window to close"
browser.driver.switchTo().window(otherHandle)
}
browser.driver.close()
browser.driver.switchTo().window(mainWindowHandle)
}

private cleanupWindows() {
if(mainWindowHandle && browser.driver.windowHandles.size() > 1) {
browser.driver.windowHandles.findAll { it != mainWindowHandle } .each { handle ->
browser.driver.switchTo().window(handle).close()
}
browser.driver.switchTo().window(mainWindowHandle)
}
mainWindowHandle = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,22 @@ Then(~/^I am at '(.*)'$/) { String path ->
steps.atPath(path)
}

Then(~/^another window has popped up$/) { ->
steps.assertPopup()
}

When(~/I switch to the popped up window/) { ->
steps.switchToPopup(null)
}

Then(~/^the (.* (?:page|dialog)) has popped up$/) { String pageName ->
steps.switchToPopup(pageName)
}

When(~/I close the popped up window/) { ->
steps.closePopup()
}

Then(~/the popped up window has closed/) { ->
steps.popupClosed()
}

0 comments on commit a44eaa2

Please sign in to comment.