You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -100,36 +100,52 @@ In the rest of this article, we will walk through some of the highlights of both
100
100
Both variations of the sample project uses the sample Cucumber scenario. In this scenario, Sergey (who likes to search for stuff) is performing a search on the DuckDuckGo search engine:
101
101
102
102
```Gherkin
103
-
Feature: Search by keyword
103
+
Feature: Verify Jungle socks home page
104
104
105
-
Scenario: Searching for a term
106
-
Given Sergey is on the DuckDuckGo home page
107
-
When he searches for "cucumber"
108
-
Then all the result titles should contain the word "cucumber"
105
+
Scenario: Verify jungle socks home page navigation
106
+
Given I open the jungle socks home page url
107
+
Then I should see jungle socks home page title as "JungleSocks"
108
+
Then I should see jungle socks home page header as "Welcome To Jungle Socks!"
109
109
```
110
110
111
111
This scenario lets us explore a few of the new Cucumber 4 expressions. Cucumber 4 supports both the classic regular expressions, and the new _Cucumber Expressions_, which are more readable albeit not as powerful in some cases.
112
112
113
113
The glue code for this scenario uses both regular expressions and cucumber expressions. The glue code looks this this:
114
114
115
115
```java
116
-
@Given("^(?:.*) is on the DuckDuckGo home page")
117
-
publicvoid i_am_on_the_DuckDuckGo_home_page() {
118
-
navigateTo.theDuckDuckGoHomePage();
119
-
}
120
-
121
-
@When("(s)he searches for {string}")
122
-
publicvoid i_search_for(String term) {
123
-
searchFor.term(term);
124
-
}
125
-
126
-
@Then("all the result titles should contain the word {string}")
The `@Given` step uses a regular expression; the action class approach we use here is action-centric, not actor-centric, so we ignore the name of the actor.
@@ -144,152 +160,132 @@ The glue code shown above uses Serenity step libraries as _action classes_ to ma
144
160
These classes are declared using the Serenity `@Steps` annotation, shown below:
145
161
```java
146
162
@Steps
147
-
NavigateTo navigateTo;
148
-
149
-
@Steps
150
-
SearchFor searchFor;
163
+
HomePageActions homePageActions;
151
164
152
165
@Steps
153
-
SearchResult searchResult;
166
+
HomePageActions homePageActions;
167
+
154
168
```
155
169
156
170
The `@Steps`annotation tells Serenity to create a new instance of the class, and inject any other steps or page objects that this instance might need.
157
171
158
172
Each action class models a particular facet of user behaviour: navigating to a particular page, performing a search, or retrieving the results of a search. These classes are designed to be small and self-contained, which makes them more stable and easier to maintain.
159
173
160
-
The `NavigateTo` class is an example of a very simple action class. In a larger application, it might have some other methods related to high level navigation, but in our sample project, it just needs to open the DuckDuckGo home page:
161
-
```java
162
-
publicclassNavigateTo {
163
-
164
-
DuckDuckGoHomePage duckDuckGoHomePage;
165
-
166
-
@Step("Open the DuckDuckGo home page")
167
-
publicvoidtheDuckDuckGoHomePage() {
168
-
duckDuckGoHomePage.open();
169
-
}
170
-
}
171
-
```
172
-
173
-
It does this using a standard Serenity Page Object. Page Objects are often very minimal, storing just the URL of the page itself:
174
-
```java
175
-
@DefaultUrl("https://duckduckgo.com")
176
-
classDuckDuckGoHomePageextendsPageObject {}
177
-
```
178
-
179
-
The second class, `SearchFor`, is an interaction class. It needs to interact with the web page, and to enable this, we make the class extend the Serenity `UIInteractionSteps`. This gives the class full access to the powerful Serenity WebDriver API, including the `$()` method used below, which locates a web element using a `By` locator or an XPath or CSS expression:
180
-
```java
181
-
publicclassSearchForextendsUIInteractionSteps {
182
-
183
-
@Step("Search for term {0}")
184
-
publicvoidterm(Stringterm) {
185
-
$(SearchForm.SEARCH_FIELD).clear();
186
-
$(SearchForm.SEARCH_FIELD).type(term);
187
-
$(SearchForm.SEARCH_BUTTON).click();
188
-
}
189
-
}
190
-
```
191
-
192
-
The `SearchForm` class is typical of a light-weight Page Object: it is responsible uniquely for locating elements on the page, and it does this by defining locators or occasionally by resolving web elements dynamically.
The last step library class used in the step definition code is the `SearchResult` class. The job of this class is to query the web page, and retrieve a list of search results that we can use in the AssertJ assertion at the end of the test. This class also extends `UIInteractionSteps` and
The main advantage of the approach used in this example is not in the lines of code written, although Serenity does reduce a lot of the boilerplate code that you would normally need to write in a web test. The real advantage is in the use of many small, stable classes, each of which focuses on a single job. This application of the _Single Responsibility Principle_ goes a long way to making the test code more stable, easier to understand, and easier to maintain.
188
+
}
220
189
221
-
## The Screenplay starter project
222
-
If you prefer to use the Screenplay pattern, or want to try it out, check out the _screenplay_ branch instead of the _master_ branch. In this version of the starter project, the same scenario is implemented using the Screenplay pattern.
The Screenplay pattern describes tests in terms of actors and the tasks they perform. Tasks are represented as objects performed by an actor, rather than methods. This makes them more flexible and composable, at the cost of being a bit more wordy. Here is an example:
In both approaches, the Page Objects very close or identical. The differences are mainly in the action classes. Screenplay classes emphasise reusable components and a very readable declarative style, whereas Lean Page Objects and Action Classes opt for a more imperative style.
215
+
}
254
216
255
-
The `NavigateTo` class performs the same role as it’s equivalent in the Lean Page Object/Action Class version, and looks quite similar:
In Screenplay, there is a clear distinction between actions (which change the system state) and questions (which read the system state). In Screenplay, we fetch the search results using a Question class, like this:
The main advantage of the approach used in this example is not in the lines of code written, although Serenity does reduce a lot of the boilerplate code that you would normally need to write in a web test. The real advantage is in the use of many small, stable classes, each of which focuses on a single job. This application of the _Single ResponsibilityPrinciple_ goes a long way to making the test code more stable, easier to understand, and easier to maintain.
291
288
292
-
The Screenplay DSL is rich and flexible, and well suited to teams working on large test automation projects with many team members, and who are reasonably comfortable with Java and design patterns. The Lean Page Objects/Action Classes approach proposes a gentler learning curve, but still provides significant advantages in terms of maintainability and reusability.
293
289
294
290
## Executing the tests
295
291
To run the sample project, you can either just run the `CucumberTestSuite` test runner class, or run either `mvn verify` or `gradle test` from the command line.
0 commit comments