Skip to content

Commit

Permalink
Sample Page Objects code
Browse files Browse the repository at this point in the history
  • Loading branch information
wakaleo committed Oct 11, 2023
1 parent 36c3402 commit 1207564
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 70 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
41 changes: 22 additions & 19 deletions src/test/java/starter/acceptancetests/WhenSearchingForTerms.java
Original file line number Diff line number Diff line change
@@ -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");
}

}
15 changes: 15 additions & 0 deletions src/test/java/starter/actions/NavigateSteps.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
25 changes: 25 additions & 0 deletions src/test/java/starter/actions/SearchSteps.java
Original file line number Diff line number Diff line change
@@ -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<String> getSearchResults() {
return findAll(SearchForm.ARTICLE_HEADINGS).texts();
}
}

This file was deleted.

12 changes: 0 additions & 12 deletions src/test/java/starter/actions/navigation/NavigateTo.java

This file was deleted.

16 changes: 0 additions & 16 deletions src/test/java/starter/actions/search/LookForInformation.java

This file was deleted.

7 changes: 0 additions & 7 deletions src/test/java/starter/actions/search/SearchArticle.java

This file was deleted.

8 changes: 0 additions & 8 deletions src/test/java/starter/actions/search/SearchForm.java

This file was deleted.

12 changes: 12 additions & 0 deletions src/test/java/starter/pageobjects/SearchForm.java
Original file line number Diff line number Diff line change
@@ -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");
}

0 comments on commit 1207564

Please sign in to comment.