DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Demystifying Sorting Assertions With AssertJ
  • Fluent Assertions Reloaded
  • How SAML 2.0 Authentication Works and Why It Matters
  • The Anatomy of Good Unit Testing

Trending

  • Advanced Insight Generation: Revolutionizing Data Ingestion for AI-Powered Search
  • Software Specs 2.0: An Elaborate Example
  • MCP and The Spin-Off CoT Pattern: How AI Agents Really Use Tools
  • Self-Supervised Learning Techniques

Serenity Ensure – Fluent Assertions in Serenity Screenplay

Take a look at this tutorial that demonstrates how to create simpler and easier assertions using the serenity-ensure module.

By 
John Ferguson Smart user avatar
John Ferguson Smart
·
Updated Jul. 05, 19 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
14.2K Views

Join the DZone community and get the full member experience.

Join For Free

Assertions are an important part of any test automation framework, and Serenity gives us many options. You can, of course, use standard JUnit, Hamcrest or AssertJ assertions at any point in a Screenplay test. But more recent versions of Serenity Screenplay provide an alternative approach, which many developers find easier to use and faster to write: the serenity-ensure module.

Introducing serenity-ensure

The Ensure class produces a Performable, so you can integrate them directly into the attemptsTo() method. It also has a very readable DSL and lets you use code completion to discover the assertions you can use for different values, making writing assertions easier and quicker.

An example of code equivalent to the above can be seen here. Suppose you have an input field on your HTML form to enter the age, which you have identified in your Screenplay test using a Target:

Target AGE_FIELD = Target.the("Age field").locatedBy("#age");


Using serenity-ensure, you could check the value of this field using the Ensure.that() method, like this:

...
sam.attemptsTo(
    Ensure.that(AGE).value().isEqualTo("40") 
);

Getting Started With serenity-ensure Assertions

To add serenity-ensure to your project, you need to add a new dependency, as shown here for Maven:

<dependency>
 <groupId>net.serenity-bdd</groupId>
 <artifactId>serenity-ensure</artifactId>
 <version>${serenity.version}</version>
 <scope>test</scope>
</dependency>

The starting point for all serenity-ensure assertions is the net.serenitybdd.screenplay.ensure.Ensureclass.

From here, you use the Ensure.that() method, passing the value you want to check. This can be an actual value (String, Integer, LocalDate, etc.), a Screenplay Question, or a web element locator. The actual assertions available can be different for different data types, and this is one of the things that makes serenity-ensure so convenient to use: Once you have entered Ensure.that(someValue) in your IDE, you can use auto-complete to explore the range of available assertions for that type. There are over a hundred different assertion methods, covering web elements, Strings, Dates and Times, integer and floating point numbers, and collections. You can see the full set of assertions in the Serenity Documentation, but let’s take a look at some of the highlights.

Working With Assertions About Web Elements

The serenity-ensure module has a rich range of assertions related to web elements. We have already seen some of these in the previous code. Other assertions include  isDisplayed()  and  isEnabled() . We can use either a Target instance directly, or use the ElementLocated.by()   method to specify a Target, a By locator, or a CSS or XPath expression. For example:

aster.attemptsTo(
    Ensure.that(ElementLocated.by("#firstName")).isDisplayed()
);


You can use methods like  value() ,  text() or  textContent() to read from a web element. Once you have obtained the value, you can use a range of String assertions, such as  startsWith() shown here:

aster.attemptsTo(
    Ensure.that(FIRST_NAME).value().startsWith("Joe")
);

Working with assertions about collections


We often need to reason about collections of values, rather than just individual values. We can use methods like  values()  and  textContentValues()  to make assertions about collections of web elements. For example:

aster.attemptsTo(
    Ensure.that(ElementLocated.by("#colors option"))
          .values()
          .contains("red","blue","green") 
);


We can also make assertions about the elements themselves. TheMatchingElement class gives us a set of methods to make assertions about these elements, and methods such as  allMatch(),  anyMatch(), and  noneMatch() complete the picture:

aster.attemptsTo(
    Ensure.thatTheSetOf(ElementsLocated.by(".train-line"))
          .allMatch(TheMatchingElement.containsText("Line"))
);



Converting Types

We can also use converter methods such as  asAnInteger(),  asADouble, or  asADate()to convert the String value returned by WebDriver into a form more convenient to make assertions on. For example, we could refactor the code we saw earlier to reason about the age value as an integer like this:

sam.attemptsTo(
    Ensure.that(AGE).value().asAnInteger().isGreaterThan(18) 
);


Alternatively, if you wanted to make an assertion about a date or a time, you could write code like this:

aster.attemptsTo(
    Ensure.that(ElementLocated.by("#currentDate"))
          .value()
          .asADate("dd-MM-yyyy")
          .isBefore(dateLimit)
);


Want to Learn More?

This is just a very brief taste of what the serenity-ensure module can do. You can read the full documentation for the serenity-ensure module here. If you want to learn more about Serenity Screenplay, you can follow the introductory tutorial here. Or if you prefer video tutorials and guided exercises, the Serenity Dojo Screenplay Course will be getting a module on serenity-ensure later this week.

Assertion (software development)

Opinions expressed by DZone contributors are their own.

Related

  • Demystifying Sorting Assertions With AssertJ
  • Fluent Assertions Reloaded
  • How SAML 2.0 Authentication Works and Why It Matters
  • The Anatomy of Good Unit Testing

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: