This commit is contained in:
mikr
2019-12-29 15:40:04 +01:00
parent 91633ff4cf
commit 21b8a4b067
6 changed files with 181 additions and 0 deletions
@@ -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());
}
}