diff --git a/README.md b/README.md index 5cb0f65..757ed0b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Serenity JUnit Starter project -Get started quickly with Serenity BDD and JUnit 5 with this simple starter project. +Get started quickly with Serenity BDD and JUnit 5 using Page Objects and Action Classes with this simple starter project. ## Get the code diff --git a/src/test/java/starter/acceptancetests/WhenSearchingForTerms.java b/src/test/java/starter/acceptancetests/WhenSearchingForTerms.java index ea568aa..89e44de 100644 --- a/src/test/java/starter/acceptancetests/WhenSearchingForTerms.java +++ b/src/test/java/starter/acceptancetests/WhenSearchingForTerms.java @@ -1,39 +1,42 @@ package starter.acceptancetests; +import net.serenitybdd.annotations.Steps; import net.serenitybdd.junit5.SerenityJUnit5Extension; -import net.serenitybdd.screenplay.Actor; -import net.serenitybdd.screenplay.annotations.CastMember; -import net.serenitybdd.screenplay.ensure.Ensure; -import net.serenitybdd.screenplay.questions.page.TheWebPage; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import starter.actions.navigation.NavigateTo; -import starter.actions.search.LookForInformation; +import starter.actions.NavigateSteps; +import starter.actions.SearchSteps; + +import static org.assertj.core.api.Assertions.assertThat; @ExtendWith(SerenityJUnit5Extension.class) class WhenSearchingForTerms { - @CastMember - Actor actor; + @Steps + NavigateSteps navigate; + + @Steps + SearchSteps search; @Test @DisplayName("Should be able to search for red things") void searchForRedThings() { - actor.attemptsTo( - NavigateTo.theSearchHomePage(), - LookForInformation.about("red"), - Ensure.that(TheWebPage.title()).containsIgnoringCase("red") - ); + navigate.opensTheHomePage(); + + search.searchForTerm("red"); + + assertThat(search.getSearchResults()).anyMatch(title -> title.toLowerCase().contains("red")); } @Test - @DisplayName("Should be able to search for green things") + @DisplayName("Result page title should mention the search term") void searchForGreenThings() { - actor.attemptsTo( - NavigateTo.theSearchHomePage(), - LookForInformation.about("green"), - Ensure.that(TheWebPage.title()).containsIgnoringCase("green") - ); + navigate.opensTheHomePage(); + + search.searchForTerm("green"); + + assertThat(search.getTitle()).containsIgnoringCase("green"); } + } diff --git a/src/test/java/starter/actions/NavigateSteps.java b/src/test/java/starter/actions/NavigateSteps.java new file mode 100644 index 0000000..18d1c8e --- /dev/null +++ b/src/test/java/starter/actions/NavigateSteps.java @@ -0,0 +1,15 @@ +package starter.actions; + +import net.serenitybdd.annotations.Step; +import net.serenitybdd.core.steps.UIInteractionSteps; +import starter.pageobjects.SearchForm; + +public class NavigateSteps extends UIInteractionSteps { + + SearchForm searchForm; + + @Step("User opens the DuckDuckGo home page") + public void opensTheHomePage() { + searchForm.open(); + } +} diff --git a/src/test/java/starter/actions/SearchSteps.java b/src/test/java/starter/actions/SearchSteps.java new file mode 100644 index 0000000..d5a3e08 --- /dev/null +++ b/src/test/java/starter/actions/SearchSteps.java @@ -0,0 +1,25 @@ +package starter.actions; + +import net.serenitybdd.annotations.Step; +import net.serenitybdd.annotations.Steps; +import net.serenitybdd.core.steps.UIInteractionSteps; +import org.openqa.selenium.Keys; +import starter.pageobjects.SearchForm; + +import java.util.List; + +public class SearchSteps extends UIInteractionSteps { + + SearchForm searchForm; + + @Step("User searches for '{0}'") + public void searchForTerm(String searchTerm) { + find(SearchForm.SEARCH_FIELD).sendKeys(searchTerm); + find(SearchForm.SEARCH_BUTTON).click(); + } + + @Step("Check the search results") + public List getSearchResults() { + return findAll(SearchForm.ARTICLE_HEADINGS).texts(); + } +} diff --git a/src/test/java/starter/actions/navigation/DuckDuckGoHomePage.java b/src/test/java/starter/actions/navigation/DuckDuckGoHomePage.java deleted file mode 100644 index 7df5e56..0000000 --- a/src/test/java/starter/actions/navigation/DuckDuckGoHomePage.java +++ /dev/null @@ -1,7 +0,0 @@ -package starter.actions.navigation; - -import net.serenitybdd.annotations.DefaultUrl; -import net.serenitybdd.core.pages.PageObject; - -@DefaultUrl("https://duckduckgo.com/") -public class DuckDuckGoHomePage extends PageObject {} diff --git a/src/test/java/starter/actions/navigation/NavigateTo.java b/src/test/java/starter/actions/navigation/NavigateTo.java deleted file mode 100644 index 73e3046..0000000 --- a/src/test/java/starter/actions/navigation/NavigateTo.java +++ /dev/null @@ -1,12 +0,0 @@ -package starter.actions.navigation; - -import net.serenitybdd.screenplay.Performable; -import net.serenitybdd.screenplay.Task; -import net.serenitybdd.screenplay.actions.Open; - -public class NavigateTo { - public static Performable theSearchHomePage() { - return Task.where("{0} opens the DuckDuckGo home page", - Open.browserOn().the(DuckDuckGoHomePage.class)); - } -} diff --git a/src/test/java/starter/actions/search/LookForInformation.java b/src/test/java/starter/actions/search/LookForInformation.java deleted file mode 100644 index 7e34a7b..0000000 --- a/src/test/java/starter/actions/search/LookForInformation.java +++ /dev/null @@ -1,16 +0,0 @@ -package starter.actions.search; - -import net.serenitybdd.screenplay.Performable; -import net.serenitybdd.screenplay.Task; -import net.serenitybdd.screenplay.actions.Enter; -import org.openqa.selenium.Keys; - -public class LookForInformation { - public static Performable about(String searchTerm) { - return Task.where("{0} searches for '" + searchTerm + "'", - Enter.theValue(searchTerm) - .into(SearchForm.SEARCH_FIELD) - .thenHit(Keys.ENTER) - ); - } -} diff --git a/src/test/java/starter/actions/search/SearchArticle.java b/src/test/java/starter/actions/search/SearchArticle.java deleted file mode 100644 index 7115aec..0000000 --- a/src/test/java/starter/actions/search/SearchArticle.java +++ /dev/null @@ -1,7 +0,0 @@ -package starter.actions.search; - -import net.serenitybdd.screenplay.targets.Target; - -public class SearchArticle { - public static final Target BODY = Target.the("article identifier").locatedBy("//article"); -} diff --git a/src/test/java/starter/actions/search/SearchForm.java b/src/test/java/starter/actions/search/SearchForm.java deleted file mode 100644 index dc60fa9..0000000 --- a/src/test/java/starter/actions/search/SearchForm.java +++ /dev/null @@ -1,8 +0,0 @@ -package starter.actions.search; - -import net.serenitybdd.screenplay.targets.Target; - -class SearchForm { - static Target SEARCH_FIELD = Target.the("search field").locatedBy("#searchbox_input"); - -} diff --git a/src/test/java/starter/pageobjects/SearchForm.java b/src/test/java/starter/pageobjects/SearchForm.java new file mode 100644 index 0000000..03c31d9 --- /dev/null +++ b/src/test/java/starter/pageobjects/SearchForm.java @@ -0,0 +1,12 @@ +package starter.pageobjects; + +import net.serenitybdd.annotations.DefaultUrl; +import net.serenitybdd.core.pages.PageObject; +import org.openqa.selenium.By; + +@DefaultUrl("https://duckduckgo.com/") +public class SearchForm extends PageObject { + public static final By SEARCH_FIELD = By.name("q"); + public static final By SEARCH_BUTTON = By.cssSelector("[aria-label='Search']"); + public static final By ARTICLE_HEADINGS = By.cssSelector("[data-testid=result] h2"); +}