Geb Testing Workshop
Workshop for UI testing with Geb and Spock. The workshop won't go into any detail about Spock or testing in Groovy, so if you realize you don't understand the general structure of the test files, please checkout Spock manual, the Groovy documentation or my Spock Testing Workshop first ;).
Project Setup with Maven
- Add Groovy as a (test) dependency (see pom.xml).
- Add Spock dependency (see pom.xml).
- Add Geb dependency (see pom.xml).
- Add Selenium dependency (see pom.xml).
- Add something to actually get a WebDriver implementation. In this project I chose WebDriverManager, which can easily be utilized in Geb's configuration (see pom.xml). It is used in GebConfig to download and setup the driver binary (e.g. see GebConfig).
- Make Maven aware of
/src/test/groovybeing a test source directory since Maven’s default is
- Add GMavenPlus to compile Groovy sources, since Maven’s default settings only compile Java (see pom.xml).
There are several alternatives to get Groovy code compiled in Maven. See the Groovy documentation on Groovy Maven integration for an overview.
- Make Maven Failsafe plugin aware of files ending with
*UiSpecare test class files since its default is
*ITand execute it during integration-test phase (see pom.xml).
In general this project should work just fine. However, we are dealing with quite a stack of technology here: you are using Geb, which relies on WebDriver to automate a browser that you have installed on your system (and which may or may not be of the latest version for your system).
So lets check if everything works fine first:
- Open GoogleUiSpec and execute it via your IDE.
If this did not work for you, please check the following things:
- Can you (manually) reach the Google start page? If not, please check your internet connection.
- Did your IDE understand the project structure? Is it fit for Groovy? It should generally work out of the box for IntelliJ Idea.
- If your browser just starts and then does nothing, you probably need a new version of Selenium. So change the version in the pom.xml. Notice that you probably will also need a new version of WebDriverManager (see pom.xml). You can also try to configure a different browser in the GebConfig.
- If the test fails but generally executes, the structure of the Google start page might have changed and the test needs adjustments. Please check for issues and feel free to raise a new one.
- In any other case, please raise an issue.
Part 1: The Browser
First we'll use the Browser instance provided by
GebReportingSpec to navigate to the main Google page at
- Add a new feature method to GoogleUiSpec, that checks if "the Google logo can be found on the Google start page".
- Write another one to check if "the main search input field can be found".
Part 2: Pages
If you did not refactor your test code, you now have at least one duplicate line of code for navigating to the Google page. Also you have put some knowledge about the page structure into your test, which will probably keep being duplicated: the selector for the main search input.
- Create a
GoogleStartPageclass containing the URL. Now change your features to use
gofor navigating to the page.
- Add an
atcheck to your page. Use your selector for the bigger Google logo for this. Now shorten your first feature method to one single call.
- Move your selector for the main search input field to the page's content. Refactor your features to use the
GoogleStartPageand contain no more knowledge about the HTML structure.
Part 3: More interaction and waiting
Web pages often contain dynamic elements these days. While these may appear immediately to your eye, they actually appear at an undefined time (e.g. not after the browser regards the page as loaded).
- Add a new feature method, which checks if "search suggestions are shown on typing one character".
Note that the suggestions appear quite fast but not immediately. You might need to
- Write a feature method to verify that "the search input content is changed to a suggestion selected by down key"
- Write another feature method to check that "clicking on a suggestion opens the result page for the suggestion". Create a
atcheck for this test.
- Add an
GoogleResultsPage, then create another feature method, which tests that "the results page can be reached via URL".
Part 4: Configuration
Let's explore the possibilities of the GebConfig file.
- Configure the
baseUrlin the GebConfig script and change your
GoogleStartPageto rely on that.
- Configure a
reportsDirand make your GoogleUiSpec extend
GebSpec. Run your tests and check the directory.
- Make Geb only create a report for failed tests. Check the effects in the reports directory.
Part 5: Modules
Web application often contain an element multiple times. E.g. quite many pages contain elements (text fields, checkboxes, radio buttons, buttons). Geb
Modules describing the structure of those elements and hold possible interactions with them. It also allows to create your own
- Use a
TextInputmodule for the search input field. Understand how this might be useful.
- Create a
ResultModuleallows to access search result's title and URL. Add a list of
ResultModules to your
GoogleResultsPage. Add a feature to your GoogleUiSpec to check that "when searching for 'Wiki', the top result is 'Wikipedia'".
- Create a
SuggestionModule, which allows to get the text actually typed and the supplement assumed by Google. Write a test verifying that "all suggestions start with the user typed text"