Search code examples
groovyspockgebui-testing

How to automate clicking confirmation pop-up using geb?


I'm trying to automate a web application's ui-tests by using Groovy, Maven, Geb and Spock. I've a page that shows confirmation pop-up to ask user "Are you sure? -Yes -No" after clicking a button on the page. I am able to click the button on the page and I also need to click to "Yes" button which is in the pop-up window. When I inspect the "Yes" button on Google Chrome it looks available, so that I used its name like this on page:

MyPage.groovy

import geb.Page

class page extends Page{
    static url = "myPage"
    static at = { waitFor { title == "My Page" }}

    static content =
    {
        confirmBtn {$("input[value*='Confirm']")}
        yesBtn {$("input[value*='Yes']")}
    }
}

This is what I tried to click "Yes":

MySpec.groovy

import geb.spock.GebSpec
import MyPage

    class MySpec extends GebSpec{
        def "Confirm"(){
            given:
            def page = to MyPage

            when:
            go MyPage
            page.confirmBtn.click()
            yesBtn.click()

            then:
            ...
        }
    }

As a result it gives me the following exception:

org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with

How can I click that "Yes" button, do you have any suggestions?

EDIT:

I debugged the code by adding the following lines before clicking the button:

waitFor { yesBtn.isEnabled() }
println "isDisplayed: " + yesBtn.isDisplayed()
println "isEnabled: " + yesBtn.isEnabled()

But whether I waitFor button to get enabled or displayed or not it always prints:

isDisplayed: false
isEnabled: true

And after reading this post I got the idea that dom needs to get refreshed somehow.


Solution

  • As the button will not be there when the page is first loaded, you need to inform Geb of this fact by passing required: false like so:

    yesBtn(required:false) { $("input[value*='Yes']") }