Search code examples
grailsfunctional-testinggebgrails-4

How to run geb functional test in https?


My application requires the app to run in https since the browser sends payment data to payment gateway through JavaScript library.

If the app is run in http then this error is thrown by the payment gateway.

enter image description here

I have created a simple hello world app and wrote a simple geb spec.

I don't seem to find a way to run the server in https mode. I don't find any helpful resource in the web as well.

Right now it is running in http mode in random port:

Grails application running at http://localhost:54461 in environment: test

I have tried adding https port in build.gradle as

integrationTest {
    systemProperty "webdriver.chrome.driver", "C:\\webdrivers\\chromedriver.exe"

    jvmArgs(
            '-Dgrails.server.port.https=8443'
    )


}

But that seems to get ignored.

I have also tried setting the https port in IntelliJ run configuration as shown below.

enter image description here

I have published the app code in GitHub for reference.

https://github.com/learningcscience/gebhttps

Update

Today I think I made a little more progress.

I could now run the app in a fixed port. I ran the app in 8443 which is for https.

I did this using the spring boot test annotation

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)

In the console now it shows

Grails application running at http://localhost:8443 in environment: test
Starting ChromeDriver 100.0.4896.20 (f9d71f93d32a6487809d6f35a9670c879fe97dfe-refs/branch-heads/4896@{#203}) on port 31898
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.

https://docs.grails.org/latest/guide/testing.html

Now I just need to make the app run using the https rather than http.

I have updated the code in GitHub repo.


Solution

  • ok. The problem is finally solved.

    The last help came from the grails community at https://grails.slack.com/

    Thanks Mattias Reichel for the help.

    I am now going to put step by step process so that others might not get stuck with this issue.

    In order to run functional geb test in https you first need to put SpringBootTest annotation as mentioned in above UPDATE: section.

    I am pasting here again

    @Integration
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
    class EventCreationSpec extends GebSpec {
    

    After that you set baseurl in src/integration-test/resources/GebConfig.groovy.

    I put baseUrl = "https://localhost:8443/"

    My GebConfig looks like this

    import org.openqa.selenium.chrome.ChromeDriver
    import org.openqa.selenium.chrome.ChromeOptions
    import org.openqa.selenium.firefox.FirefoxDriver
    
    environments {
    
    
    
    
        // run via “./gradlew -Dgeb.env=chrome iT”
        chrome {
            driver = {
    
                System.setProperty('webdriver.chrome.driver', 'C:\\webdrivers\\chromedriver.exe')
                new ChromeDriver()
    
            }
        }
    
        // run via “./gradlew -Dgeb.env=chromeHeadless iT”
        chromeHeadless {
            driver = {
                ChromeOptions o = new ChromeOptions()
                o.addArguments('headless')
                new ChromeDriver(o)
            }
        }
    
        // run via “./gradlew -Dgeb.env=firefox iT”
        firefox {
            driver = { new FirefoxDriver() }
        }
    }
    
    baseUrl = "https://localhost:8443/"
    

    After that you need to create a application-test.yml file in src/integration-test/resources/

    The application-test.yml file looks like this

    server:
        port: 8443
        ssl:
            enabled: true
            keyStore: c:/Users/user/selfsigned.jks
            keyStorePassword: pepsicola
            keyAlias: tomcat
    

    you need to create self signed certificate.

    You can go through this process to create the certificate

    https://grails.org/blog/2017-06-28.html

    In the configuration above

    my certificate was in selfsigned.jks keystore in the path c:/Users/user/selfsigned.jks

    After that the functional test will fire in https mode

    In my case

    http://localhost:8443/roadrace

    here is what the gebspec should look like

    Note the SpringBootTest annotation at the top.

    @Integration
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
    @Stepwise
    class EventCreationSpec extends GebSpec {
    
    
        def grailsApplication
        def springSecurityService
        def timeService
    
    
    
        def setup() {
    
        }
    
        def cleanup() {
        }
    
        void "Create and publish event"() {
            when:"The home page is visited"
            go '/roadrace/'
    
            $("#details-button").click()
            $("#proceed-link").click()
    
    
              ... rest of gebspock test steps....
    
    
    
            then:"The title is correct"
                title == "Homepage"
        }
    
    
    }
    

    Please note that i had to go to /roadrace/ because the roadrace is the app context path.

    If you dont have context path you can go to go '/'

    The final hurdle can be when the browser fires up in https it might show

    enter image description here

    For this using geb you can click on the Advanced and then Proceed to localhost (unsafe) links

    I just click the links like this

    go '/roadrace/'

    $("#details-button").click()
    $("#proceed-link").click()
    

    That's all! Now the geb functional test runs in https. Since it is https you can also now communicate to test payment gateway.