Cucumber hooks - http://jira.baeldung.com/browse/BAEL-3590
This commit is contained in:
parent
91633ff4cf
commit
21b8a4b067
|
@ -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 + "]";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.cucumberhooks.books;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class BookStore {
|
||||
private List<Book> books = new ArrayList<>();
|
||||
|
||||
public void addBook(Book book) {
|
||||
books.add(book);
|
||||
}
|
||||
|
||||
public void addAllBooks(Collection<Book> books) {
|
||||
this.books.addAll(books);
|
||||
}
|
||||
|
||||
public List<Book> booksByAuthor(String author) {
|
||||
return books.stream()
|
||||
.filter(book -> Objects.equals(author, book.getAuthor()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public Optional<Book> bookByTitle(String title) {
|
||||
return books.stream()
|
||||
.filter(book -> book.getTitle().equals(title))
|
||||
.findFirst();
|
||||
}
|
||||
}
|
|
@ -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$")
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
@ -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<Book> 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<List<String>> rows = table.asLists(String.class);
|
||||
|
||||
for (List<String> 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());
|
||||
}
|
||||
|
||||
}
|
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue