Question

In our application is a simple input field that will open a new window when the enter key is pressed. With a plain geb test I could have write this:

withNewWindow({ inputField << Keys.ENTER }, wait:true){ at(TargetPage) }

but now we introduced jbehave and as a result we got the test steps (in my opinion it would be the same if we would use spock or any other framework).

Now our test will become:

class StartPageSteps extends GebTest {

    @When("enter is pressed")
    void pressEnter() {
        at StartPage
        inputField << Keys.ENTER
    }

    @Then("target page was opened")
    void targetPageWasOpened() {
        withWindow({$('title').text() == 'title'}){at(TargetPage)}
    }
}

And still seperate classes to describe the pages (reduced version):

class StartPage extends Page {
    static  at =  { title == "StartPage" }
    static url = "..."
    static content = {
        inputField { $("input")}
    }
}

class TargetPage extends Page {
    static at = { title == "title"}
}

But this fails with a org.openqa.selenium.NoSuchWindowException: Could not find a window that would match the specification I can see how the new window gets open and the title is right. I also tried other attributes to locate the window and used wait statements. Any ideas?

Was it helpful?

Solution

You need to use Page.getTitle() to retrieve current page title, $('title') won't work. Your code should be:

@Then("target page was opened")
void targetPageWasOpened() {
    withWindow({title == 'title'}) {at(TargetPage)}
}

EDIT

If the window is opened asynchronously then you can wrap your code with a waitFor() call.

@Then("target page was opened")
void targetPageWasOpened() {
    waitFor {
        withWindow({ isAt TargetPage }) { true }
    }
}

isAt() will suppress any assertion errors and will return false if you're not on TargetPage. If a window with that page is not found then withWindow() will throw NoSuchWindowException imposing another loop inside of waitFor() closure. If the window is found then true will be returned and the waitFor() will complete.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top