Skip to content

Commit 9154e9f

Browse files
authored
Merge pull request eugenp#8449 from Maiklins/BAEL-3590-cucumber-hooks
BAEL-3590 cucumber hooks
2 parents e8d2c9a + e7823a3 commit 9154e9f

File tree

5 files changed

+179
-0
lines changed

5 files changed

+179
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.cucumberhooks.books;
2+
3+
public class Book {
4+
5+
private String title;
6+
private String author;
7+
8+
public Book(String title, String author) {
9+
this.title = title;
10+
this.author = author;
11+
}
12+
13+
public Book() {}
14+
15+
public String getTitle() {
16+
return title;
17+
}
18+
19+
public void setTitle(String title) {
20+
this.title = title;
21+
}
22+
23+
public String getAuthor() {
24+
return author;
25+
}
26+
27+
public void setAuthor(String author) {
28+
this.author = author;
29+
}
30+
31+
@Override
32+
public String toString() {
33+
return "Book [title=" + title + ", author=" + author + "]";
34+
}
35+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.cucumberhooks.books;
2+
3+
import java.util.*;
4+
import java.util.stream.Collectors;
5+
6+
public class BookStore {
7+
private List<Book> books = new ArrayList<>();
8+
9+
public void addBook(Book book) {
10+
books.add(book);
11+
}
12+
13+
public void addAllBooks(Collection<Book> books) {
14+
this.books.addAll(books);
15+
}
16+
17+
public List<Book> booksByAuthor(String author) {
18+
return books.stream()
19+
.filter(book -> Objects.equals(author, book.getAuthor()))
20+
.collect(Collectors.toList());
21+
}
22+
23+
public Optional<Book> bookByTitle(String title) {
24+
return books.stream()
25+
.filter(book -> book.getTitle().equals(title))
26+
.findFirst();
27+
}
28+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung.cucumberhooks.books;
2+
3+
import io.cucumber.core.api.Scenario;
4+
import io.cucumber.java.After;
5+
import io.cucumber.java.AfterStep;
6+
import io.cucumber.java.Before;
7+
import io.cucumber.java.BeforeStep;
8+
import io.cucumber.java8.En;
9+
import io.cucumber.junit.Cucumber;
10+
import io.cucumber.junit.CucumberOptions;
11+
import org.junit.runner.RunWith;
12+
13+
@RunWith(Cucumber.class)
14+
@CucumberOptions(features = "classpath:features/book-store-with-hooks.feature",
15+
glue = "com.baeldung.cucumberhooks.books"
16+
)
17+
public class BookStoreWithHooksIntegrationTest implements En {
18+
19+
public BookStoreWithHooksIntegrationTest() {
20+
Before(1, () -> startBrowser());
21+
}
22+
23+
@Before(order=2, value="@Screenshots")
24+
public void beforeScenario(Scenario scenario) {
25+
takeScreenshot();
26+
}
27+
28+
@After
29+
public void afterScenario(Scenario scenario) {
30+
takeScreenshot();
31+
}
32+
33+
@BeforeStep
34+
public void beforeStep(Scenario scenario) {
35+
takeScreenshot();
36+
}
37+
38+
@AfterStep
39+
public void afterStep(Scenario scenario) {
40+
takeScreenshot();
41+
closeBrowser();
42+
}
43+
44+
public void takeScreenshot() {
45+
//code to take and save screenshot
46+
}
47+
48+
public void startBrowser() {
49+
//code to open browser
50+
}
51+
52+
public void closeBrowser() {
53+
//code to close browser
54+
}
55+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.cucumberhooks.books;
2+
3+
import io.cucumber.datatable.DataTable;
4+
import io.cucumber.java.en.Given;
5+
import io.cucumber.java.en.Then;
6+
import io.cucumber.java.en.When;
7+
import io.cucumber.java8.En;
8+
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
import static org.junit.Assert.assertEquals;
13+
14+
public class BookStoreWithHooksRunSteps implements En {
15+
16+
private BookStore store;
17+
private List<Book> foundBooks;
18+
private Book foundBook;
19+
20+
public BookStoreWithHooksRunSteps() {
21+
store = new BookStore();
22+
foundBooks = new ArrayList<>();
23+
}
24+
25+
@Given("^The following books are available in the store$")
26+
public void haveBooksInTheStore(DataTable table) {
27+
List<List<String>> rows = table.asLists(String.class);
28+
29+
for (List<String> columns: rows) {
30+
store.addBook(new Book(columns.get(0), columns.get(1)));
31+
}
32+
}
33+
34+
@When("^I ask for a book by the author (.+)$")
35+
public void searchForBooksByAuthor(String author) {
36+
foundBooks = store.booksByAuthor(author);
37+
}
38+
39+
@Then("^The salesperson says that there are (\\d+) books$")
40+
public void findBooks(int count) {
41+
assertEquals(count, foundBooks.size());
42+
}
43+
44+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Feature: Book Store With Hooks
2+
Background: The Book Store
3+
Given The following books are available in the store
4+
| The Devil in the White City | Erik Larson |
5+
| The Lion, the Witch and the Wardrobe | C.S. Lewis |
6+
| In the Garden of Beasts | Erik Larson |
7+
8+
@Screenshots
9+
Scenario: 1 - Find books by author
10+
When I ask for a book by the author Erik Larson
11+
Then The salesperson says that there are 2 books
12+
13+
Scenario: 2 - Find books by author, but isn't there
14+
When I ask for a book by the author Marcel Proust
15+
Then The salesperson says that there are 0 books
16+
17+

0 commit comments

Comments
 (0)