inline fun Browser.withWindow(nameOrHandle: String? = null, windowContext: WebDriver.() -> Unit): Unit
Switch the focus of future commands for this driver to the window with the given name/handle.
The name/handle can be omitted and the switching will be performed automatically if and only if there is only two windows currently opened.
Once the window has been selected, all subsequent calls on the WebDriver interface are made to that window till the end of windowContext.
If a exception is thrown inside windowContext, the driver will return to the previous window.
Browser.drive(driverFactory) {
// Given I navigate to the page under test, which contains links that open windows
to(pageWithWindowsUrl)
// And I'm in the context of the original window
assertEquals(`$`("h1", 0).text, "Page with links that open windows")
// And I open a new window by clicking on a link
`$`("#ddg-page", 0).click()
// When I change the driver's context to the just opened window
withWindow(windowHandles.toSet().minus(windowHandle).first()) {
// Then I should be able to interact with such window
assertEquals(
`$`(".tag-home__item", 0).text.trim(),
"The search engine that doesn't track you. Learn More.")
}
// And I should return into the context of the original window
assertEquals(`$`("h1", 0).text, "Page with links that open windows")
// And only one window should be opened
assertEquals(windowHandles.size, 1)
}
nameOrHandle
- The name of the window or the handle as returned by WebDriver.getWindowHandle
windowContext
- Here you can interact with the given window.
NoSuchWindowException
- If the window cannot be found or, in the case of no name or handle is indicated,
there is not exactly two windows currently opened.