Beliebte Suchanfragen

Cloud Native

DevOps

IT-Security

Agile Methoden

Java

//

ATDD and Thucydides – part 2 of 2

13.3.2013 | 5 minutes of reading time

In the previous post I described you can use ATDD for closing the gap between business, tester and development, in the second part of the post I will describe Thucydides a powerful tool for doing ATDD.

Thucydides

For testing applications on the web, selenium can be used. Selenium is a framework that allows tests to be written to work on multiple browsers in a generic way. These tests can be defined by recording them in the browser or to be written down in code. When recording selenium tests a problems can occur when the state changes of the web application, an expected element could become unavailable and fail your test. When defining tests in code, it could be possible that tests lose their readability for testers and people who represent the domain.

Keeping tests readable, reusable and repeatable could be a challenge, Thucydides is a selenium extension that allows people from the domain, developers and testers to have those tests. Thucydides is build around the idea that the tests tell the story of the application and are readable for everyone, Thucydides is a ATDD testing framework for the web.

Thucydides keeps the tests readable and reusable by making use of stories, features, acceptance criteria (tests) and steps. Stories define a high level description of a set of features for example user management. A story can be defined in features for example creating and removing a user. These features have acceptance criteria for example “a user manager is able to sort users on the user overview page”. A acceptance criteria is a collection of steps that have to be done to perform the action.

Implementing tests or acceptance criteria, different test languages/tools can be chosen Thucydides supports test tools like EasyB, jBehave and JUnit.  When implementing tests with Thucydides the following concepts are in important:

  • Page(model), a representation of a web page where for example links and buttons are considered properties of a page. The state of a page is represented in a page(model).
  • Steps, a grouping(library) of actions that can be performed on a page. For example clicking on a link on a page.
  • Test, a collection of steps defining a user operation and verify if the operations were done correctly.

The big difference for only choosing selenium is the ability to have steps. Steps adds a abstraction level, that allows the implementer of tests to create a library of operations that can be done on a web page. These libraries can be reused in other tests.

For running Thucydides tests maven is used, maven is a framework that allows to automate build, documentation and reporting processes. After configuring Thucydides in the project pom.xml, tests can be run by using the following command. ‘mvn verify’ (Make sure the web application is available while starting the tests.)

Reporting and documentation are an important part of Thucydides, the reports represent the implementation state of the application. The will also tell the (historic) story of the application, which acceptance criteria are met, a which steps are taken to meet these acceptance criteria. In the reports a drilldown is possible from stories to the actual steps that have be taken to test the story.

Example of a Thucydides report:

For training purposes we’ve created a training application that is based on Weinburg-Myers triangle program example. This training application contains examples of wide range of tests. This training application also contains examples making use of Thucydides. (For running tests please read the README file on github https://github.com/codecentric/TDDTrainingApplication/blob/master/README.md )

The Thucydides concepts are also available in the training application, we use a page model for the representation of a web page.

1public class TriangleCalculationPage extends PageObject {
2 
3public TriangleCalculationPage(WebDriver driver) {
4super(driver);
5}
6 
7@FindBy(name = "triangleSide1")
8private WebElement inputSide1;
9 
10@FindBy(name = "triangleSide2")
11private WebElement inputSide2;
12 
13@FindBy(name = "triangleSide3")
14private WebElement inputSide3;
15 
16@FindBy(css = "button[type='submit']")
17private WebElement submitButton;
18 
19public boolean hasInputSide1() {
20return element(getInputSide1()).isCurrentlyVisible();
21}
22 
23public boolean hasInputSide2() {
24return element(getInputSide2()).isCurrentlyVisible();
25}
26 
27public boolean hasInputSide3() {
28return element(getInputSide3()).isCurrentlyVisible();
29}
30 
31public boolean hasSubmitButton(){
32return element(getSubmitButton()).isCurrentlyVisible();
33}
34 
35}

In the TDD training application there are steps to defined for operation that can be done on a web page.

1public class TriangleCalculationSteps extends ScenarioSteps{
2 
3public TriangleCalculationSteps(Pages pages) {
4super(pages);
5}
6 
7@Step("Open triangle calculation form")
8public void openTriangleCalculationForm(){
9getPages().get(TriangleCalculationPage.class).open();
10}
11 
12@Step("Fill in triangle sides")
13public void fillInTriangleSides(String triangleSide1,String triangleSide2,String triangleSide3){
14TriangleCalculationPage triangleCalculationPage = getPages().currentPageAt(TriangleCalculationPage.class);
15triangleCalculationPage.getInputSide1().clear();
16triangleCalculationPage.getInputSide2().clear();
17triangleCalculationPage.getInputSide3().clear();
18triangleCalculationPage.getInputSide1().sendKeys(triangleSide1);
19triangleCalculationPage.getInputSide2().sendKeys(triangleSide2);
20triangleCalculationPage.getInputSide3().sendKeys(triangleSide3);
21}
22 
23@Step("Submit triangle calculation form")
24public void submitTriangleSideForm(){
25TriangleCalculationPage triangleCalculationPage = getPages().currentPageAt(TriangleCalculationPage.class);
26triangleCalculationPage.getSubmitButton().click();
27}
28 
29@Step("Should see the error page")
30public void shouldBeAtErrorPage(){
31Assert.assertTrue("Should see the error page", getPages().isCurrentPageAt(TriangleCalculationErrorPage.class));
32}
33 
34@Step("Should see the success page")
35public void shouldBeAtSuccessPage(){
36Assert.assertTrue("Should see the success page", getPages().isCurrentPageAt(TriangleCalculationSuccessPage.class));
37}
38 
39@Step("Expect triangle type on success page")
40public void expectTriangleTypeOnSuccessPage(TriangleType triangleType){
41TriangleCalculationSuccessPage triangleCalculationSuccessPage = getPages().currentPageAt(TriangleCalculationSuccessPage.class);
42Assert.assertTrue("Triangle success page should have triangle type element",triangleCalculationSuccessPage.hasTriangleTypeSpan());
43Assert.assertThat("Triangle type is not correct",triangleType, Matchers.equalTo(TriangleType.valueOf(triangleCalculationSuccessPage.getTriangleTypeSpan().getText())));
44}
45 
46}

By having these steps and pagemodels the following stories / scenarios can now be tested. These tests can by run with JUnit for example.

1@RunWith(ThucydidesRunner.class)
2@Story(Application.TriangleCalculation.Success.class)
3public class TriangleCalculationSuccessStory {
4 
5@Managed(uniqueSession = true)
6public WebDriver webDriver;
7 
8@Getter
9@ManagedPages
10public Pages pages;
11 
12@Steps
13public TriangleCalculationSteps steps;
14 
15@Test
16@Title("Test triangle type EQUILATERAL")
17public void testNonNumericEquilateral() {
18fillInTriangleCalculationForm("2", "2", "2", TriangleType.EQUILATERAL);
19}
20 
21@Test
22@Title("Test triangle type ISOSCELES")
23public void testNonNumericInputSide2() {
24fillInTriangleCalculationForm("2", "3", "2", TriangleType.ISOSCELES);
25}
26 
27@Test
28@Title("Test triangle type SCALENE")
29public void testNonNumericInputSide3() {
30fillInTriangleCalculationForm("2", "3", "4", TriangleType.SCALENE);
31}
32 
33private void fillInTriangleCalculationForm(String side1, String side2, String side3, TriangleType triangleType) {
34steps.openTriangleCalculationForm();
35steps.fillInTriangleSides(side1, side2, side3);
36steps.submitTriangleSideForm();
37steps.shouldBeAtSuccessPage();
38steps.expectTriangleTypeOnSuccessPage(triangleType);
39}
40 
41}

Conclusion

ATDD allows domain representatives, testers and developers to work in a more uniform way by having tests written down in a uniform language everybody understands. A uniform language or DSL can be interpreted by people and machines, which allows for readability and repeatability.

For acceptance testing web application frameworks like Thucydides and Fitness can be used. Thucydides allows tests or acceptance criteria to be written for different test tools like EasyB, jBehave or JUnit.

Links

Thucydides
TDD Training application
TDD Weinburg-Myers example
ATDD
DSL
EasyB
Cucumber
JBehave
Selenium
Fitnesse

share post

Likes

0

//

Gemeinsam bessere Projekte umsetzen.

Wir helfen deinem Unternehmen.

Du stehst vor einer großen IT-Herausforderung? Wir sorgen für eine maßgeschneiderte Unterstützung. Informiere dich jetzt.

Hilf uns, noch besser zu werden.

Wir sind immer auf der Suche nach neuen Talenten. Auch für dich ist die passende Stelle dabei.