All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.serenitybdd.screenplay.ensure.Ensure.kt Maven / Gradle / Ivy

There is a newer version: 4.2.12
Show newest version
@file:JvmName("Ensure")

package net.serenitybdd.screenplay.ensure

import net.serenitybdd.screenplay.Actor
import net.serenitybdd.screenplay.AnonymousPerformableFunction
import net.serenitybdd.screenplay.Question
import net.serenitybdd.screenplay.ensure.web.PageObjectEnsure
import net.serenitybdd.screenplay.ensure.web.TargetEnsure
import net.serenitybdd.screenplay.targets.Target
import org.openqa.selenium.By
import java.time.LocalDate
import java.time.LocalTime
import java.util.function.Consumer

fun that(value: String?) = StringEnsure(value)
fun that(value: LocalDate?) = DateEnsure(value)
fun that(value: LocalTime?) = TimeEnsure(value)
fun that(value: Boolean?) = BooleanEnsure(value)
fun that(value: Float?) = FloatEnsure(value)
fun that(value: Double?) = DoubleEnsure(value)
fun  that(value: Comparable) = ComparableEnsure(value)
fun  that(value: Collection?) = CollectionEnsure(value)

@Deprecated("Use that() instead")
fun  thatTheAnswerTo(description: String, question: Question) =
    BooleanEnsure(KnowableBooleanAnswer(question, description))

fun  that(description: String, question: Question) =
    BooleanEnsure(KnowableBooleanAnswer(question, description))

@Deprecated("Use that() instead")
fun  thatTheAnswerTo(question: Question) =
    BooleanEnsure(KnowableBooleanAnswer(question, question.subject))

fun  that(question: Question) = BooleanEnsure(KnowableBooleanAnswer(question, question.subject))

fun  that(question: Question, predicate: (actual: A) -> Boolean) = that("predicate check at ${predicateLocation()}", question, predicate)
fun  that(description: String, question: Question, predicate: (actual: A) -> Boolean) =
    AnonymousPerformableFunction("Ensure that $description",
        object : Consumer {
            override fun accept(actor: Actor) {
                val actual = question.answeredBy(actor)
                if (!predicate.invoke(actual)) {
                    throw AssertionError("Expected $description but was $actual")
                }
            }
        }
    )

fun predicateLocation() : String {
    val className = Throwable().stackTrace[2].className
    val methodName = Throwable().stackTrace[2].methodName
    val lineNumber = Throwable().stackTrace[2].lineNumber
    return "$className.$methodName (line $lineNumber)"
}

@Deprecated("Use that() instead")
fun  thatTheAnswerTo(description: String, question: Question) =
    StringEnsure(KnowableStringAnswer(question, description))

fun  that(description: String, question: Question) =
    StringEnsure(KnowableStringAnswer(question, description))

@Deprecated("Use that() instead")
fun  thatTheAnswerTo(question: Question) =
    StringEnsure(KnowableStringAnswer(question, question.subject))

fun  that(question: Question) = StringEnsure(KnowableStringAnswer(question, question.subject))

@Deprecated("Use that() instead")
fun > thatTheAnswerTo(description: String, question: Question) =
    ComparableEnsure(KnowableComparableAnswer(question, description), null, description)

fun > that(description: String, question: Question) =
    ComparableEnsure(KnowableComparableAnswer(question, description), null, description)

@Deprecated("Use that() instead")
fun > thatTheAnswerTo(question: Question) = that(question.subject, question)
fun > that(question: Question) = that(question.subject, question)

@Deprecated("Use that() instead")
fun  thatTheAnswersTo(description: String, question: Question>) =
    CollectionEnsure(KnowableCollectionAnswer(question, description), description)

fun  that(description: String, question: Question>) =
    CollectionEnsure(KnowableCollectionAnswer(question, description), description)

@Deprecated("Use that() instead")
fun  thatTheAnswersTo(question: Question>) = that(question.subject, question)
fun  that(question: Question>) = that(question.subject, question)

fun  thatTheListOf(description: String, question: Question>) =
    CollectionEnsure(KnowableListAnswer(question, description), description)

fun  thatTheListOf(question: Question>) =
    thatTheListOf(question.subject, question)

fun thatTheCurrentPage() = PageObjectEnsure()
fun that(value: Target) = TargetEnsure(value)
fun that(value: By) = TargetEnsure(value)

// Collection matchers
fun thatTheListOf(value: Target) =
    CollectionEnsure(KnowableCollectionTarget(value), "a collection of ${KnowableCollectionTarget(value)}")

fun thatTheListOf(value: By) =
    CollectionEnsure(KnowableCollectionTarget(value), "a collection of ${KnowableCollectionTarget(value)}")

fun thatAmongst(value: Target) =
    CollectionEnsure(KnowableCollectionTarget(value), "a collection of ${KnowableCollectionTarget(value)}")

fun thatAmongst(value: By) =
    CollectionEnsure(KnowableCollectionTarget(value), "a collection of ${KnowableCollectionTarget(value)}")

fun enableSoftAssertions() = BlackBox.startSoftAssertions()
fun reportSoftAssertions() = BlackBox.reportAnySoftAssertions()

///**
// * Ensure something
// */
//fun that(description: String, check: Consumer) = net.serenitybdd.rest.Ensure.that(description, check)





© 2015 - 2025 Weber Informatics LLC | Privacy Policy