📜 ⬆️ ⬇️

Functional testing of pain-free web applications

Sometimes it happens like this in life - you are waiting, waiting for something, studying the theory on this issue, considering different approaches to the solution, discussing with the same people like you, listening to the voice of recognized gurus, but not moving further an inch. Then you throw, you forget about this issue altogether, you do other things, and suddenly - on you, everything fell into place, a wonderful mosaic formed of scattered elements, enlightenment appeared, and hair suddenly became thick and silky.

That's about the same story with automatic functional (acceptance) testing. Kent Beck himself wrote about such a cool thing as automatic tests. Well, automated functional tests are generally a tasty morsel for modern agile software development techniques. For example, the same Scrum - includes the practice of "Demo", during which the customer needs to show the development of the product, carried out during the iteration.
Of course, I’m not an agile practitioner, and I haven’t studied the market of tools for functional testing of web projects - it’s possible that everything in this segment used to be zashib. But in the 5 years that I have been working as a programmer - I have only heard such words automatic functional testing a couple of times, Selenium and have never seen practical application.
So, returning to the lyrical introduction, it seems to me that this colossal qualitative change just happened recently. And there is a feeling that in the near future, only lazy will neglect the functional testing of their web project.
What actually happened? I subscribe to the RSS feed for the Springsource blog , and once I found an article with such an intriguing title - The future of functional web testing? .
The Geb and Spock tools described in this article hooked me, and I decided to try it.

Actually further, I will try to step on the throat of a song to interrupt the attack of graphomania and describe a simple example using these tools.

Infrastructure installation


For the tests we need:

')

The first functional test


This section will give an example of automating a simple, but undoubtedly useful, functional test.
The essence of the functional test will be to:
- enter the Wikipedia website
- type in the search "Functional testing"
- make sure that we really hit the desired page
(for example, we will look for the entry in the text of the page of the line “this is software testing in order to verify the feasibility of functional requirements”

Project Directory Structure


The structure of the project directory for testing is as follows:


pom.xml - file with a description of the project in Maven
simplefunctest - package in which classes for test descriptions will be stored

Actually Test


package my.tests.simplefunctest

import geb.spock.GebSpec

class MyFirstSpec extends GebSpec {

def "test search functional testing wiki page" () {
given: "we are at main wiki page"
to MainWikiPage

when : "try to search functional testing page"
searchField.value( " " )
searchButton.click()

then : "check we are on functional testing page"
at FunctionalTestingWikiPage
}
}

* This source code was highlighted with Source Code Highlighter .

What's going on here?
- We create a class for our test suite, inherit from the base class describing the GebSpec test suite . (In theory, BDD terminology, namely specifications, etc., should be used here, but for brevity and understanding I will omit it)
class MyFirstSpec extends GebSpec { ... }

* This source code was highlighted with Source Code Highlighter .

- We create a description of a specific test designed to find the functional testing page on Wikipedia
def "test search functional testing wiki page" () { ... }

* This source code was highlighted with Source Code Highlighter .

- We find ourselves on the main page of Wikipedia
given: "we are at main wiki page"
to MainWikiPage


* This source code was highlighted with Source Code Highlighter .

- We enter the phrase "functional testing" in the search field and initiate a click on the search button
when : "try to search functional testing page"
searchField.value( " " )
searchButton.click()


* This source code was highlighted with Source Code Highlighter .

“Then we check to see if we’re actually on Wikipedia’s functional testing page.”
then : "check we are on functional testing page"
at FunctionalTestingWikiPage


* This source code was highlighted with Source Code Highlighter .


Wikipedia homepage description


package my.tests.simplefunctest

import geb.Page

class MainWikiPage extends Page {
static url = "http://ru.wikipedia.org/"
static at = {title == " — " }
static content = {
searchField { $( "input" , id: "searchInput" )}
searchButton ( to : FunctionalTestingWikiPage) { $( "button" , id: "searchButton" )}
}
}

* This source code was highlighted with Source Code Highlighter .

What's going on here?
- We create a class to describe the main page of Wikipedia, inherit from the base class that describes the Page
class MainWikiPage extends Page { ... }

* This source code was highlighted with Source Code Highlighter .

- Specify the URL of the page (required, since the test begins with this page)
static url = "http://ru.wikipedia.org/"

* This source code was highlighted with Source Code Highlighter .

- We describe the closure to verify that we are on the required page (in this case, we check that the title is “Wikipedia - the free encyclopedia”)
static at = {title == " — " }

* This source code was highlighted with Source Code Highlighter .

- We describe the closure for filling the page (in this case, this is the text field of the search and the button for performing the search (both elements will be found by the tag and id)) (see the article about using the $ () function )
static content = {
searchField { $( "input" , id: "searchInput" )}
searchButton ( to : FunctionalTestingWikiPage) { $( "button" , id: "searchButton" )}
}


* This source code was highlighted with Source Code Highlighter .

Search result page description


package my.tests.simplefunctest

import geb.Page

class FunctionalTestingWikiPage extends Page {

static at = { $().text().contains( " " ) }
}

* This source code was highlighted with Source Code Highlighter .

I hope everything is clear.

Actually start the test


We now turn to the most interesting, for which we all started it, namely to start the tests.
On the command line, in the root directory of the project you need to run:
mvn clean test

* This source code was highlighted with Source Code Highlighter .

Bingo! You should start FireFox (it is configured as the default browser for tests) and execute (without your participation (!!!)) what we have in mind.

Perhaps Firefox will not be enough for you and you go to run the test in IE (a special profile has been set up for this):
mvn clean test

* This source code was highlighted with Source Code Highlighter .

Or maybe in Chrome:
mvn clean test -P chrome

* This source code was highlighted with Source Code Highlighter .

Conclusion


I will not burden either myself or you with a description of libraries — he has them in sufficient quantity on the websites of these libraries, but I just recommend that you take the project out of the repository and try it yourself.

Important links


Source: https://habr.com/ru/post/109077/


All Articles