From 21b8a4b067d47940950c581f36da2d85541ee016 Mon Sep 17 00:00:00 2001 From: mikr Date: Sun, 29 Dec 2019 15:40:04 +0100 Subject: [PATCH] Cucumber hooks - http://jira.baeldung.com/browse/BAEL-3590 --- .../baeldung/cucumberhooks/books/Book.java | 35 ++++++++++++ .../cucumberhooks/books/BookStore.java | 28 ++++++++++ .../books/BookStoreRunSteps.java | 1 + .../BookStoreWithHooksIntegrationTest.java | 56 +++++++++++++++++++ .../books/BookStoreWithHooksRunSteps.java | 44 +++++++++++++++ .../features/book-store-with-hooks.feature | 17 ++++++ 6 files changed, 181 insertions(+) create mode 100644 testing-modules/testing-libraries/src/main/java/com/baeldung/cucumberhooks/books/Book.java create mode 100644 testing-modules/testing-libraries/src/main/java/com/baeldung/cucumberhooks/books/BookStore.java create mode 100644 testing-modules/testing-libraries/src/test/java/com/baeldung/cucumberhooks/books/BookStoreWithHooksIntegrationTest.java create mode 100644 testing-modules/testing-libraries/src/test/java/com/baeldung/cucumberhooks/books/BookStoreWithHooksRunSteps.java create mode 100644 testing-modules/testing-libraries/src/test/resources/features/book-store-with-hooks.feature diff --git a/testing-modules/testing-libraries/src/main/java/com/baeldung/cucumberhooks/books/Book.java b/testing-modules/testing-libraries/src/main/java/com/baeldung/cucumberhooks/books/Book.java new file mode 100644 index 0000000000..dc8f1dcf64 --- /dev/null +++ b/testing-modules/testing-libraries/src/main/java/com/baeldung/cucumberhooks/books/Book.java @@ -0,0 +1,35 @@ +package com.baeldung.cucumberhooks.books; + +public class Book { + + private String title; + private String author; + + public Book(String title, String author) { + this.title = title; + this.author = author; + } + + public Book() {} + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + @Override + public String toString() { + return "Book [title=" + title + ", author=" + author + "]"; + } +} diff --git a/testing-modules/testing-libraries/src/main/java/com/baeldung/cucumberhooks/books/BookStore.java b/testing-modules/testing-libraries/src/main/java/com/baeldung/cucumberhooks/books/BookStore.java new file mode 100644 index 0000000000..cc4e42d28f --- /dev/null +++ b/testing-modules/testing-libraries/src/main/java/com/baeldung/cucumberhooks/books/BookStore.java @@ -0,0 +1,28 @@ +package com.baeldung.cucumberhooks.books; + +import java.util.*; +import java.util.stream.Collectors; + +public class BookStore { + private List books = new ArrayList<>(); + + public void addBook(Book book) { + books.add(book); + } + + public void addAllBooks(Collection books) { + this.books.addAll(books); + } + + public List booksByAuthor(String author) { + return books.stream() + .filter(book -> Objects.equals(author, book.getAuthor())) + .collect(Collectors.toList()); + } + + public Optional bookByTitle(String title) { + return books.stream() + .filter(book -> book.getTitle().equals(title)) + .findFirst(); + } +} diff --git a/testing-modules/testing-libraries/src/test/java/com/baeldung/cucumberbackground/books/BookStoreRunSteps.java b/testing-modules/testing-libraries/src/test/java/com/baeldung/cucumberbackground/books/BookStoreRunSteps.java index 981fe41f11..49429b722f 100644 --- a/testing-modules/testing-libraries/src/test/java/com/baeldung/cucumberbackground/books/BookStoreRunSteps.java +++ b/testing-modules/testing-libraries/src/test/java/com/baeldung/cucumberbackground/books/BookStoreRunSteps.java @@ -20,6 +20,7 @@ public class BookStoreRunSteps { public void setUp() { store = new BookStore(); foundBooks = new ArrayList<>(); + System.out.print("Book store steps run simple"); } @Given("^I have the following books in the store$") diff --git a/testing-modules/testing-libraries/src/test/java/com/baeldung/cucumberhooks/books/BookStoreWithHooksIntegrationTest.java b/testing-modules/testing-libraries/src/test/java/com/baeldung/cucumberhooks/books/BookStoreWithHooksIntegrationTest.java new file mode 100644 index 0000000000..98c2ec781c --- /dev/null +++ b/testing-modules/testing-libraries/src/test/java/com/baeldung/cucumberhooks/books/BookStoreWithHooksIntegrationTest.java @@ -0,0 +1,56 @@ +package com.baeldung.cucumberhooks.books; + +import io.cucumber.core.api.Scenario; +import io.cucumber.java.After; +import io.cucumber.java.AfterStep; +import io.cucumber.java.Before; +import io.cucumber.java.BeforeStep; +import io.cucumber.java8.En; +import io.cucumber.junit.Cucumber; +import io.cucumber.junit.CucumberOptions; +import org.junit.runner.RunWith; + +@RunWith(Cucumber.class) +@CucumberOptions(features = "classpath:features/book-store-with-hooks.feature", +glue ="com.baeldung.cucumberhooks.books" +) +public class BookStoreWithHooksIntegrationTest implements En { + + public BookStoreWithHooksIntegrationTest() { + Before(1, () -> startBrowser()); + } + + @Before(order=2, value="@Screenshots") + public void beforeScenario(Scenario scenario) { + takeScreenshot(); + } + + @After + public void afterScenario(Scenario scenario) { + takeScreenshot(); + } + + @BeforeStep + public void beforeStep(Scenario scenario) { + takeScreenshot(); + } + + @AfterStep + public void afterStep(Scenario scenario) { + takeScreenshot(); + closeBrowser(); + } + + public void takeScreenshot() { + //code to take and save screenshot + } + + public void startBrowser() { + //code to open browser + } + + public void closeBrowser() { + //code to close browser + } +} + diff --git a/testing-modules/testing-libraries/src/test/java/com/baeldung/cucumberhooks/books/BookStoreWithHooksRunSteps.java b/testing-modules/testing-libraries/src/test/java/com/baeldung/cucumberhooks/books/BookStoreWithHooksRunSteps.java new file mode 100644 index 0000000000..8ebda5ffa4 --- /dev/null +++ b/testing-modules/testing-libraries/src/test/java/com/baeldung/cucumberhooks/books/BookStoreWithHooksRunSteps.java @@ -0,0 +1,44 @@ +package com.baeldung.cucumberhooks.books; + +import io.cucumber.datatable.DataTable; +import io.cucumber.java.en.Given; +import io.cucumber.java.en.Then; +import io.cucumber.java.en.When; +import io.cucumber.java8.En; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +public class BookStoreWithHooksRunSteps implements En { + + private BookStore store; + private List foundBooks; + private Book foundBook; + + public BookStoreWithHooksRunSteps() { + store = new BookStore(); + foundBooks = new ArrayList<>(); + } + + @Given("^The following books are available in the store$") + public void haveBooksInTheStore(DataTable table) { + List> rows = table.asLists(String.class); + + for (List columns: rows) { + store.addBook(new Book(columns.get(0), columns.get(1))); + } + } + + @When("^I ask for a book by the author (.+)$") + public void searchForBooksByAuthor(String author) { + foundBooks = store.booksByAuthor(author); + } + + @Then("^The salesperson says that there are (\\d+) books$") + public void findBooks(int count) { + assertEquals(count, foundBooks.size()); + } + +} diff --git a/testing-modules/testing-libraries/src/test/resources/features/book-store-with-hooks.feature b/testing-modules/testing-libraries/src/test/resources/features/book-store-with-hooks.feature new file mode 100644 index 0000000000..78e136d68d --- /dev/null +++ b/testing-modules/testing-libraries/src/test/resources/features/book-store-with-hooks.feature @@ -0,0 +1,17 @@ +Feature: Book Store With Hooks + Background: The Book Store + Given The following books are available in the store + | The Devil in the White City | Erik Larson | + | The Lion, the Witch and the Wardrobe | C.S. Lewis | + | In the Garden of Beasts | Erik Larson | + + @Screenshots + Scenario: 1 - Find books by author + When I ask for a book by the author Erik Larson + Then The salesperson says that there are 2 books + + Scenario: 2 - Find books by author, but isn't there + When I ask for a book by the author Marcel Proust + Then The salesperson says that there are 0 books + +