I've got REST webservice based on Grails. There's obligatory authentication before you can access any page except login. I use Geb & Spock for integration tests, running through Gradle. My question is: how can I provide authentication before executing any test? I have 2 ideas:
- Run tests in order (log in at first, then run others). How? (must be triggering by command
gradle integrationTest
) - Execute JS script before every test which authenticate me. How to implement this in test?
What kind of authentication? For Basic authentication have a look at: HTTP Basic Authentication credentials passed in URL and encryption so you could just add that to your base url.
If you have to go through the login process, you can use the
setupSpec()
andsetup
methods. The former gets executed before anything else happens, the latter before each individual test method.You can use the @Stepwise notation to stop cookies from being deleted between test methods. If you have different geb classes and want all of them to be prepended with your setup or setupSpec method, use the extends notation:
note that this will execute the login process one extra time because it happens in the BaseClass as well. I got around this with an
if (this.getClass().getPackage().getName() != "gebMethods")
block but I guess there might be a more elegant solution.