From 544fab7828daa74f12180b45ae591e12f7529aba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Soares?= Date: Wed, 6 Mar 2024 14:29:18 +0000 Subject: [PATCH] prevent exposing TextMatch --- README.md | 4 +- .../main/kotlin/seleniumtestinglib/Core.kt | 123 ++++++++++-------- lib/src/test/java/JavaApiTest.java | 4 +- .../locators/ByAltTextTest.kt | 4 +- .../locators/ByDisplayValueTest.kt | 4 +- .../locators/ByLabelTextTest.kt | 4 +- .../locators/ByPlaceholderTextTest.kt | 4 +- .../seleniumtestinglib/locators/ByRoleTest.kt | 7 +- .../locators/ByTestIdTest.kt | 4 +- .../seleniumtestinglib/locators/ByTextTest.kt | 28 ++-- .../locators/ByTitleTest.kt | 4 +- .../locators/NotFoundTest.kt | 4 +- 12 files changed, 101 insertions(+), 93 deletions(-) diff --git a/README.md b/README.md index 00028e9..581ec65 100644 --- a/README.md +++ b/README.md @@ -27,10 +27,10 @@ The [core API](https://testing-library.com/docs) contains the selectors which ar ```kotlin driver.findElements(altText("first name")) -driver.findElement(displayValue("c => c.startsWith('selen')".asJsExpression())) +driver.findElement(displayValue(JsFunction("c => c.startsWith('selen')"))) driver.findElements(labelText("active")) driver.findElements(placeholderText("first name", exact = false)) -driver.findElements(role(Heading, nameAsFunction = "c => c.startsWith('something')".asJsExpression())) +driver.findElements(role(Heading, nameAsFunction = JsFuntion("c => c.startsWith('something')"))) driver.findElements(role(Button, nameAsRegex = Pattern.compile("confirm"))) driver.findElements(testId("test-id")) driver.findElements(text("present", exact = false, selector = "span")) diff --git a/lib/src/main/kotlin/seleniumtestinglib/Core.kt b/lib/src/main/kotlin/seleniumtestinglib/Core.kt index 8aea9c5..9aac99b 100644 --- a/lib/src/main/kotlin/seleniumtestinglib/Core.kt +++ b/lib/src/main/kotlin/seleniumtestinglib/Core.kt @@ -16,18 +16,18 @@ class TL { * https://testing-library.com/docs/queries/byalttext */ @JvmStatic - fun altText(text: String, exact: Boolean? = null, normalizer: JsExpression? = null) = + fun altText(text: String, exact: Boolean? = null, normalizer: JsFunction? = null) = ByAltText(text) .apply { exact?.let(::exact) } .apply { normalizer?.let(::normalizer) } @JvmStatic - fun altText(text: Pattern, normalizer: JsExpression? = null) = + fun altText(text: Pattern, normalizer: JsFunction? = null) = ByAltText(text) .apply { normalizer?.let(::normalizer) } @JvmStatic - fun altText(text: JsExpression, normalizer: JsExpression? = null) = + fun altText(text: JsFunction, normalizer: JsFunction? = null) = ByAltText(text).apply { normalizer?.let(::normalizer) } @JvmStatic @@ -37,23 +37,23 @@ class TL { fun altText(text: Pattern) = ByAltText(text) @JvmStatic - fun altText(text: JsExpression) = ByAltText(text) + fun altText(text: JsFunction) = ByAltText(text) /** * https://testing-library.com/docs/queries/bydisplayvalue */ @JvmStatic - fun displayValue(value: String, exact: Boolean? = null, normalizer: JsExpression? = null) = + fun displayValue(value: String, exact: Boolean? = null, normalizer: JsFunction? = null) = ByDisplayValue(value) .apply { exact?.let(::exact) } .apply { normalizer?.let(::normalizer) } @JvmStatic - fun displayValue(value: Pattern, normalizer: JsExpression? = null) = + fun displayValue(value: Pattern, normalizer: JsFunction? = null) = ByDisplayValue(value).apply { normalizer?.let(::normalizer) } @JvmStatic - fun displayValue(value: JsExpression, normalizer: JsExpression? = null) = + fun displayValue(value: JsFunction, normalizer: JsFunction? = null) = ByDisplayValue(value).apply { normalizer?.let(::normalizer) } @@ -64,7 +64,7 @@ class TL { fun displayValue(value: Pattern) = ByDisplayValue(value) @JvmStatic - fun displayValue(value: JsExpression) = ByDisplayValue(value) + fun displayValue(value: JsFunction) = ByDisplayValue(value) /** * https://testing-library.com/docs/queries/bylabeltext @@ -74,7 +74,7 @@ class TL { text: String, exact: Boolean? = null, selector: String? = null, - normalizer: JsExpression? = null + normalizer: JsFunction? = null ) = ByLabelText(text) .apply { exact?.let(::exact) } @@ -85,7 +85,7 @@ class TL { fun labelText( text: Pattern, selector: String? = null, - normalizer: JsExpression? = null + normalizer: JsFunction? = null ) = ByLabelText(text) .apply { selector?.let(::selector) } @@ -93,9 +93,9 @@ class TL { @JvmStatic fun labelText( - text: JsExpression, + text: JsFunction, selector: String? = null, - normalizer: JsExpression? = null + normalizer: JsFunction? = null ) = ByLabelText(text) .apply { selector?.let(::selector) } @@ -108,23 +108,23 @@ class TL { fun labelText(text: Pattern) = ByLabelText(text) @JvmStatic - fun labelText(text: JsExpression) = ByLabelText(text) + fun labelText(text: JsFunction) = ByLabelText(text) /** * https://testing-library.com/docs/queries/byplaceholdertext */ @JvmStatic - fun placeholderText(text: String, exact: Boolean? = null, normalizer: JsExpression? = null) = + fun placeholderText(text: String, exact: Boolean? = null, normalizer: JsFunction? = null) = ByPlaceholderText(text) .apply { exact?.let(::exact) } .apply { normalizer?.let(::normalizer) } @JvmStatic - fun placeholderText(text: Pattern, normalizer: JsExpression? = null) = + fun placeholderText(text: Pattern, normalizer: JsFunction? = null) = ByPlaceholderText(text).apply { normalizer?.let(::normalizer) } @JvmStatic - fun placeholderText(text: JsExpression, normalizer: JsExpression? = null) = + fun placeholderText(text: JsFunction, normalizer: JsFunction? = null) = ByPlaceholderText(text).apply { normalizer?.let(::normalizer) } @JvmStatic @@ -134,23 +134,23 @@ class TL { fun placeholderText(text: Pattern) = ByPlaceholderText(text) @JvmStatic - fun placeholderText(text: JsExpression) = ByPlaceholderText(text) + fun placeholderText(text: JsFunction) = ByPlaceholderText(text) /** * https://testing-library.com/docs/queries/bytestid */ @JvmStatic - fun testId(text: String, exact: Boolean? = null, normalizer: JsExpression? = null) = + fun testId(text: String, exact: Boolean? = null, normalizer: JsFunction? = null) = ByTestId(text) .apply { exact?.let(::exact) } .apply { normalizer?.let(::normalizer) } @JvmStatic - fun testId(text: Pattern, normalizer: JsExpression? = null) = + fun testId(text: Pattern, normalizer: JsFunction? = null) = ByTestId(text).apply { normalizer?.let(::normalizer) } @JvmStatic - fun testId(text: JsExpression, normalizer: JsExpression? = null) = + fun testId(text: JsFunction, normalizer: JsFunction? = null) = ByTestId(text).apply { normalizer?.let(::normalizer) } @JvmStatic @@ -160,7 +160,7 @@ class TL { fun testId(text: Pattern) = ByTestId(text) @JvmStatic - fun testId(text: JsExpression) = ByTestId(text) + fun testId(text: JsFunction) = ByTestId(text) /** * https://testing-library.com/docs/queries/bytext @@ -171,7 +171,7 @@ class TL { selector: String? = null, exact: Boolean? = null, ignore: String? = null, - normalizer: JsExpression? = null + normalizer: JsFunction? = null ) = ByText(text) .apply { exact?.let(::exact) } @@ -184,7 +184,7 @@ class TL { text: Pattern, selector: String? = null, ignore: String? = null, - normalizer: JsExpression? = null + normalizer: JsFunction? = null ) = ByText(text) .apply { normalizer?.let(::normalizer) } @@ -193,10 +193,10 @@ class TL { @JvmStatic fun text( - text: JsExpression, + text: JsFunction, selector: String? = null, ignore: String? = null, - normalizer: JsExpression? = null + normalizer: JsFunction? = null ) = ByText(text) .apply { normalizer?.let(::normalizer) } @@ -210,23 +210,23 @@ class TL { fun text(text: Pattern) = ByText(text) @JvmStatic - fun text(text: JsExpression) = ByText(text) + fun text(text: JsFunction) = ByText(text) /** * https://testing-library.com/docs/queries/bytitle */ @JvmStatic - fun title(title: String, exact: Boolean? = null, normalizer: JsExpression? = null) = + fun title(title: String, exact: Boolean? = null, normalizer: JsFunction? = null) = ByTitle(title) .apply { exact?.let(::exact) } .apply { normalizer?.let(::normalizer) } @JvmStatic - fun title(title: Pattern, normalizer: JsExpression? = null) = + fun title(title: Pattern, normalizer: JsFunction? = null) = ByTitle(title).apply { normalizer?.let(::normalizer) } @JvmStatic - fun title(title: JsExpression, normalizer: JsExpression? = null) = + fun title(title: JsFunction, normalizer: JsFunction? = null) = ByTitle(title).apply { normalizer?.let(::normalizer) } @JvmStatic @@ -236,7 +236,7 @@ class TL { fun title(title: Pattern) = ByTitle(title) @JvmStatic - fun title(title: JsExpression) = ByTitle(title) + fun title(title: JsFunction) = ByTitle(title) /** * https://testing-library.com/docs/queries/byrole @@ -246,12 +246,12 @@ class TL { role: Role, name: String? = null, nameAsRegex: Pattern? = null, - nameAsFunction: JsExpression? = null, + nameAsFunction: JsFunction? = null, description: String? = null, descriptionAsRegex: Pattern? = null, - descriptionAsFunction: JsExpression? = null, + descriptionAsFunction: JsFunction? = null, hidden: Boolean? = null, - normalizer: JsExpression? = null, + normalizer: JsFunction? = null, selected: Boolean? = null, busy: Boolean? = null, checked: Boolean? = null, @@ -296,8 +296,7 @@ class TL { } } -abstract class TLBy(private val textMatch: TextMatch) : SeleniumBy() { - +abstract class TLBy internal constructor(private val textMatch: TextMatch) : SeleniumBy() { private val by = javaClass.simpleName private val options = mutableMapOf() protected fun set(key: String, value: Any) = apply { options[key] = value } @@ -328,64 +327,71 @@ abstract class TLBy(private val textMatch: TextMatch) : SeleniumBy() { } } -class ByAltText(text: TextMatch) : TLBy(text) { +class ByAltText private constructor(text: TextMatch) : TLBy(text) { constructor(value: String) : this(value.asJsString()) constructor(value: Pattern) : this(value.asJsExpression()) + constructor(value: JsFunction) : this(value.asJsExpression()) fun exact(exact: Boolean) = apply { set("exact", exact) } - fun normalizer(normalizer: JsExpression) = apply { set("normalizer", normalizer) } + fun normalizer(normalizer: JsFunction) = apply { set("normalizer", normalizer.asJsExpression()) } } -class ByDisplayValue(value: TextMatch) : TLBy(value) { +class ByDisplayValue private constructor(value: TextMatch) : TLBy(value) { constructor(value: String) : this(value.asJsString()) constructor(value: Pattern) : this(value.asJsExpression()) + constructor(value: JsFunction) : this(value.asJsExpression()) fun exact(exact: Boolean) = apply { set("exact", exact) } - fun normalizer(normalizer: JsExpression) = apply { set("normalizer", normalizer) } + fun normalizer(normalizer: JsFunction) = apply { set("normalizer", normalizer.asJsExpression()) } } -class ByLabelText(value: TextMatch) : TLBy(value) { +class ByLabelText private constructor(value: TextMatch) : TLBy(value) { constructor(value: String) : this(value.asJsString()) constructor(value: Pattern) : this(value.asJsExpression()) + constructor(value: JsFunction) : this(value.asJsExpression()) fun exact(exact: Boolean) = apply { set("exact", exact) } fun selector(selector: String) = apply { set("selector", selector) } - fun normalizer(normalizer: JsExpression) = apply { set("normalizer", normalizer) } + fun normalizer(normalizer: JsFunction) = apply { set("normalizer", normalizer.asJsExpression()) } } -class ByPlaceholderText(text: TextMatch) : TLBy(text) { +class ByPlaceholderText private constructor(text: TextMatch) : TLBy(text) { constructor(text: String) : this(text.asJsString()) constructor(text: Pattern) : this(text.asJsExpression()) + constructor(text: JsFunction) : this(text.asJsExpression()) fun exact(exact: Boolean) = apply { set("exact", exact) } - fun normalizer(normalizer: JsExpression) = apply { set("normalizer", normalizer) } + fun normalizer(normalizer: JsFunction) = apply { set("normalizer", normalizer.asJsExpression()) } } -class ByTestId(text: TextMatch) : TLBy(text) { +class ByTestId private constructor(text: TextMatch) : TLBy(text) { constructor(text: String) : this(text.asJsString()) constructor(text: Pattern) : this(text.asJsExpression()) + constructor(text: JsFunction) : this(text.asJsExpression()) fun exact(exact: Boolean) = apply { set("exact", exact) } - fun normalizer(normalizer: JsExpression) = apply { set("normalizer", normalizer) } + fun normalizer(normalizer: JsFunction) = apply { set("normalizer", normalizer.asJsExpression()) } } -class ByText(text: TextMatch) : TLBy(text) { +class ByText private constructor(text: TextMatch) : TLBy(text) { constructor(text: String) : this(text.asJsString()) constructor(text: Pattern) : this(text.asJsExpression()) + constructor(text: JsFunction) : this(text.asJsExpression()) fun exact(exact: Boolean) = apply { set("exact", exact) } fun ignore(ignore: String) = apply { set("ignore", ignore) } fun ignore(ignore: Boolean) = apply { set("ignore", ignore) } fun selector(selector: String) = apply { set("selector", selector) } - fun normalizer(normalizer: JsExpression) = apply { set("normalizer", normalizer) } + fun normalizer(normalizer: JsFunction) = apply { set("normalizer", normalizer.asJsExpression()) } } -class ByTitle(text: TextMatch) : TLBy(text) { +class ByTitle private constructor(text: TextMatch) : TLBy(text) { constructor(text: String) : this(text.asJsString()) constructor(text: Pattern) : this(text.asJsExpression()) + constructor(text: JsFunction) : this(text.asJsExpression()) fun exact(exact: Boolean) = apply { set("exact", exact) } - fun normalizer(normalizer: JsExpression) = apply { set("normalizer", normalizer) } + fun normalizer(normalizer: JsFunction) = apply { set("normalizer", normalizer.asJsExpression()) } } class ByRole private constructor(role: TextMatch) : TLBy(role) { @@ -393,10 +399,10 @@ class ByRole private constructor(role: TextMatch) : TLBy(role) { fun name(name: String) = apply { set("name", name) } fun name(name: Pattern) = apply { set("name", name.asJsExpression()) } - fun name(name: JsExpression) = apply { set("name", name) } + fun name(name: JsFunction) = apply { set("name", name.asJsExpression()) } fun description(description: String) = apply { set("description", description) } fun description(description: Pattern) = apply { set("description", description.asJsExpression()) } - fun description(description: JsExpression) = apply { set("description", description) } + fun description(description: JsFunction) = apply { set("description", description.asJsExpression()) } fun hidden(hidden: Boolean) = apply { set("hidden", hidden) } fun selected(selected: Boolean) = apply { set("selected", selected) } fun busy(busy: Boolean) = apply { set("busy", busy) } @@ -409,17 +415,20 @@ class ByRole private constructor(role: TextMatch) : TLBy(role) { fun level(level: Int) = apply { set("level", level) } fun value(value: Value) = apply { set("value", value.toMap()) } fun queryFallbacks(queryFallbacks: Boolean) = apply { set("queryFallbacks", queryFallbacks) } - fun normalizer(normalizer: JsExpression) = apply { set("normalizer", normalizer) } + fun normalizer(normalizer: JsFunction) = apply { set("normalizer", normalizer.asJsExpression()) } +} + +class JsFunction(val value: String) { + internal fun asJsExpression() = JsExpression(value) } -sealed class TextMatch(open val value: String) { - internal class JsString(override val value: String) : TextMatch(value) +internal sealed class TextMatch(open val value: String) { + class JsString(override val value: String) : TextMatch(value) class JsExpression(override val value: String) : TextMatch(value) override fun toString() = value } -fun String.asJsExpression() = JsExpression(this) private fun String.asJsString() = JsString(this) private fun Pattern.asJsExpression(): JsExpression { @@ -452,7 +461,7 @@ class Value( private val now: Int? = null, private val text: String? = null, private val textAsRegex: Pattern? = null, - private val textAsFunction: JsExpression? = null, + private val textAsFunction: JsFunction? = null, ) { init { require(listOfNotNull(text, textAsRegex, textAsFunction).size <= 1) { "Please provide text just once." } @@ -463,7 +472,7 @@ class Value( "min" to min, "max" to max, "now" to now, - "text" to (text ?: textAsRegex?.asJsExpression() ?: textAsFunction?.value?.asJsExpression()) + "text" to (text ?: textAsRegex?.asJsExpression() ?: textAsFunction?.asJsExpression()) ).filterValues { it != null } } diff --git a/lib/src/test/java/JavaApiTest.java b/lib/src/test/java/JavaApiTest.java index e087ea5..b9e40ae 100644 --- a/lib/src/test/java/JavaApiTest.java +++ b/lib/src/test/java/JavaApiTest.java @@ -1,10 +1,10 @@ import org.junit.jupiter.api.Test; +import seleniumtestinglib.JsFunction; import java.util.regex.Pattern; import static java.util.regex.Pattern.CASE_INSENSITIVE; import static org.junit.jupiter.api.Assertions.assertEquals; -import static seleniumtestinglib.CoreKt.asJsExpression; import static seleniumtestinglib.DriverKt.getDriver; import static seleniumtestinglib.DriverKt.render; import static seleniumtestinglib.Role.Heading; @@ -50,7 +50,7 @@ public void byLabelText() { public void byPlaceholderText() { render(getDriver(), ""); - var result = getDriver().findElement(placeholderText(asJsExpression("c => c.startsWith('User')"))); + var result = getDriver().findElement(placeholderText(new JsFunction("c => c.startsWith('User')"))); assertEquals("input", result.getTagName()); } diff --git a/lib/src/test/kotlin/seleniumtestinglib/locators/ByAltTextTest.kt b/lib/src/test/kotlin/seleniumtestinglib/locators/ByAltTextTest.kt index 7c22b04..ce7c148 100644 --- a/lib/src/test/kotlin/seleniumtestinglib/locators/ByAltTextTest.kt +++ b/lib/src/test/kotlin/seleniumtestinglib/locators/ByAltTextTest.kt @@ -3,8 +3,8 @@ package seleniumtestinglib.locators import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource import org.openqa.selenium.NoSuchElementException +import seleniumtestinglib.JsFunction import seleniumtestinglib.TL.By.altText -import seleniumtestinglib.TextMatch.JsExpression import seleniumtestinglib.driver import seleniumtestinglib.render import java.util.regex.Pattern @@ -47,7 +47,7 @@ class ByAltTextTest { driver.render("
") val result = runCatching { - driver.findElement(altText(JsExpression("c => c.startsWith('inc')"))) + driver.findElement(altText(JsFunction("c => c.startsWith('inc')"))) } assertTrue(result.exceptionOrNull() is NoSuchElementException) diff --git a/lib/src/test/kotlin/seleniumtestinglib/locators/ByDisplayValueTest.kt b/lib/src/test/kotlin/seleniumtestinglib/locators/ByDisplayValueTest.kt index 162df61..baf3f57 100644 --- a/lib/src/test/kotlin/seleniumtestinglib/locators/ByDisplayValueTest.kt +++ b/lib/src/test/kotlin/seleniumtestinglib/locators/ByDisplayValueTest.kt @@ -2,9 +2,9 @@ package seleniumtestinglib.locators import org.openqa.selenium.By.tagName import org.openqa.selenium.support.ui.Select +import seleniumtestinglib.JsFunction import seleniumtestinglib.TL.By.displayValue import seleniumtestinglib.TL.By.placeholderText -import seleniumtestinglib.asJsExpression import seleniumtestinglib.driver import seleniumtestinglib.render import java.util.regex.Pattern @@ -49,7 +49,7 @@ class ByDisplayValueTest { driver.render("") driver.findElement(placeholderText("username")).sendKeys("selenium") - val result = driver.findElement(displayValue("c => c.startsWith('selen')".asJsExpression())) + val result = driver.findElement(displayValue(JsFunction("c => c.startsWith('selen')"))) assertEquals("input", result.tagName) } diff --git a/lib/src/test/kotlin/seleniumtestinglib/locators/ByLabelTextTest.kt b/lib/src/test/kotlin/seleniumtestinglib/locators/ByLabelTextTest.kt index 6375c48..5a40cbe 100644 --- a/lib/src/test/kotlin/seleniumtestinglib/locators/ByLabelTextTest.kt +++ b/lib/src/test/kotlin/seleniumtestinglib/locators/ByLabelTextTest.kt @@ -1,7 +1,7 @@ package seleniumtestinglib.locators +import seleniumtestinglib.JsFunction import seleniumtestinglib.TL.By.labelText -import seleniumtestinglib.asJsExpression import seleniumtestinglib.driver import seleniumtestinglib.render import java.util.regex.Pattern @@ -91,7 +91,7 @@ class ByLabelTextTest { fun function() { driver.render("") - val result = driver.findElement(labelText("c => c.startsWith('User')".asJsExpression())) + val result = driver.findElement(labelText(JsFunction("c => c.startsWith('User')"))) assertEquals("input", result.tagName) } diff --git a/lib/src/test/kotlin/seleniumtestinglib/locators/ByPlaceholderTextTest.kt b/lib/src/test/kotlin/seleniumtestinglib/locators/ByPlaceholderTextTest.kt index d4b2fa9..2c7b36c 100644 --- a/lib/src/test/kotlin/seleniumtestinglib/locators/ByPlaceholderTextTest.kt +++ b/lib/src/test/kotlin/seleniumtestinglib/locators/ByPlaceholderTextTest.kt @@ -3,8 +3,8 @@ package seleniumtestinglib.locators import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource import org.openqa.selenium.NoSuchElementException +import seleniumtestinglib.JsFunction import seleniumtestinglib.TL.By.placeholderText -import seleniumtestinglib.asJsExpression import seleniumtestinglib.driver import seleniumtestinglib.render import java.util.regex.Pattern @@ -48,7 +48,7 @@ class ByPlaceholderTextTest { fun function() { driver.render("") - val result = driver.findElement(placeholderText("c => c.startsWith('User')".asJsExpression())) + val result = driver.findElement(placeholderText(JsFunction("c => c.startsWith('User')"))) assertEquals("input", result.tagName) } diff --git a/lib/src/test/kotlin/seleniumtestinglib/locators/ByRoleTest.kt b/lib/src/test/kotlin/seleniumtestinglib/locators/ByRoleTest.kt index 157cd1a..9dbcc9e 100644 --- a/lib/src/test/kotlin/seleniumtestinglib/locators/ByRoleTest.kt +++ b/lib/src/test/kotlin/seleniumtestinglib/locators/ByRoleTest.kt @@ -8,7 +8,6 @@ import org.openqa.selenium.WebElement import seleniumtestinglib.* import seleniumtestinglib.Role.* import seleniumtestinglib.TL.By.role -import seleniumtestinglib.TextMatch.JsExpression import java.util.regex.Pattern import kotlin.test.Test import kotlin.test.assertEquals @@ -51,7 +50,7 @@ class ByRoleTest { driver.render("""

something as a user something

""") val result = - driver.findElements(role(Heading, nameAsFunction = "c => c.startsWith('something')".asJsExpression())) + driver.findElements(role(Heading, nameAsFunction = JsFunction("c => c.startsWith('something')"))) assertEquals("something as a user something", result.single().accessibleName) } @@ -175,7 +174,7 @@ class ByRoleTest { ) val result = driver.findElements( - role(AlertDialog, descriptionAsFunction = JsExpression("content => content.endsWith('!')")) + role(AlertDialog, descriptionAsFunction = JsFunction("content => content.endsWith('!')")) ) assertEquals("Your session is about to expire!", result.single().text.substringAfter("Close\n")) @@ -322,7 +321,7 @@ class ByRoleTest { of(Value(max = 10), listOf("Volume", "Pitch")), of(Value(text = "medium"), listOf("Volume", "Pitch")), of(Value(textAsRegex = Pattern.compile("med")), listOf("Volume", "Pitch")), - of(Value(textAsFunction = "t => t.startsWith('med')".asJsExpression()), listOf("Volume", "Pitch")), + of(Value(textAsFunction = JsFunction("t => t.startsWith('med')")), listOf("Volume", "Pitch")), ) @ParameterizedTest diff --git a/lib/src/test/kotlin/seleniumtestinglib/locators/ByTestIdTest.kt b/lib/src/test/kotlin/seleniumtestinglib/locators/ByTestIdTest.kt index f00f6cc..3022af2 100644 --- a/lib/src/test/kotlin/seleniumtestinglib/locators/ByTestIdTest.kt +++ b/lib/src/test/kotlin/seleniumtestinglib/locators/ByTestIdTest.kt @@ -1,7 +1,7 @@ package seleniumtestinglib.locators +import seleniumtestinglib.JsFunction import seleniumtestinglib.TL.By.testId -import seleniumtestinglib.asJsExpression import seleniumtestinglib.driver import seleniumtestinglib.render import java.util.regex.Pattern @@ -32,7 +32,7 @@ class ByTestIdTest { fun function() { driver.render("""
""") - val result = driver.findElement(testId("c => c.startsWith('custom')".asJsExpression())) + val result = driver.findElement(testId(JsFunction("c => c.startsWith('custom')"))) assertEquals("div", result.tagName) } diff --git a/lib/src/test/kotlin/seleniumtestinglib/locators/ByTextTest.kt b/lib/src/test/kotlin/seleniumtestinglib/locators/ByTextTest.kt index 07de939..188f899 100644 --- a/lib/src/test/kotlin/seleniumtestinglib/locators/ByTextTest.kt +++ b/lib/src/test/kotlin/seleniumtestinglib/locators/ByTextTest.kt @@ -4,8 +4,8 @@ import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.Arguments.of import org.junit.jupiter.params.provider.MethodSource import org.openqa.selenium.NoSuchElementException -import seleniumtestinglib.TL -import seleniumtestinglib.asJsExpression +import seleniumtestinglib.JsFunction +import seleniumtestinglib.TL.By.text import seleniumtestinglib.driver import seleniumtestinglib.render import java.util.regex.Pattern @@ -19,7 +19,7 @@ class ByTextTest { fun `by text`() { driver.render("
I accept
") - val result = driver.findElement(TL.text("I accept")) + val result = driver.findElement(text("I accept")) assertEquals("span", result.tagName) } @@ -28,7 +28,7 @@ class ByTextTest { fun `ensure quotes are escaped`() { driver.render(""""
with quotes"'`
""") - val result = driver.findElement(TL.By.text("""with quotes"'`""")) + val result = driver.findElement(text("""with quotes"'`""")) assertEquals("span", result.tagName) } @@ -37,7 +37,7 @@ class ByTextTest { fun `with a selector`() { driver.render("
Username
Username
") - val result = driver.findElement(TL.text("Username", selector = "div")) + val result = driver.findElement(text("Username", selector = "div")) assertEquals("div", result.tagName) } @@ -47,7 +47,7 @@ class ByTextTest { driver.render("") val result = runCatching { - driver.findElement(TL.text("abc")) + driver.findElement(text("abc")) } assertTrue(result.exceptionOrNull() is NoSuchElementException) @@ -58,7 +58,7 @@ class ByTextTest { driver.render("Username
Username
") val result = runCatching { - driver.findElement(TL.text("Username", selector = "x")) + driver.findElement(text("Username", selector = "x")) } assertTrue(result.exceptionOrNull() is NoSuchElementException) @@ -76,7 +76,7 @@ class ByTextTest { """ ) - val result = driver.findElement(TL.text("accept", exact = false, selector = "div p")) + val result = driver.findElement(text("accept", exact = false, selector = "div p")) assertEquals("p", result.tagName) } @@ -85,7 +85,7 @@ class ByTextTest { fun regex() { driver.render("

I accept

") - val result = driver.findElement(TL.text(Pattern.compile("ACCEPT", Pattern.CASE_INSENSITIVE))) + val result = driver.findElement(text(Pattern.compile("ACCEPT", Pattern.CASE_INSENSITIVE))) assertEquals("p", result.tagName) } @@ -95,7 +95,7 @@ class ByTextTest { driver.render("

Hello World

") val result = driver.findElement( - TL.text("(content, element) => content.startsWith('Hello') && element.tagName == 'P'".asJsExpression()) + text(JsFunction("(content, element) => content.startsWith('Hello') && element.tagName == 'P'")) ) assertEquals("p", result.tagName) @@ -105,7 +105,7 @@ class ByTextTest { fun `case insensitive`() { driver.render("

I accept

") - val result = driver.findElement(TL.text("ACCEPT", exact = false)) + val result = driver.findElement(text("ACCEPT", exact = false)) assertEquals("p", result.tagName) } @@ -121,7 +121,7 @@ class ByTextTest { """ ) - val result = driver.findElements(TL.text("I accept", ignore = ignore)) + val result = driver.findElements(text("I accept", ignore = ignore)) assertEquals(expectedFound, result.size) } @@ -143,7 +143,7 @@ class ByTextTest { """ ) - val result = driver.findElements(TL.text("I accept").ignore(false)) + val result = driver.findElements(text("I accept").ignore(false)) assertEquals(3, result.size) } @@ -153,7 +153,7 @@ class ByTextTest { driver.render("

I accept

") val result = driver.findElement( - TL.text("I accept123", normalizer = "str => str + '123'".asJsExpression()) + text("I accept123", normalizer = JsFunction( "str => str + '123'")) ) assertEquals("p", result.tagName) diff --git a/lib/src/test/kotlin/seleniumtestinglib/locators/ByTitleTest.kt b/lib/src/test/kotlin/seleniumtestinglib/locators/ByTitleTest.kt index 618c449..31e4014 100644 --- a/lib/src/test/kotlin/seleniumtestinglib/locators/ByTitleTest.kt +++ b/lib/src/test/kotlin/seleniumtestinglib/locators/ByTitleTest.kt @@ -1,7 +1,7 @@ package seleniumtestinglib.locators +import seleniumtestinglib.JsFunction import seleniumtestinglib.TL.By.title -import seleniumtestinglib.asJsExpression import seleniumtestinglib.driver import seleniumtestinglib.render import java.util.regex.Pattern @@ -56,7 +56,7 @@ class ByTitleTest { fun function() { driver.render("
Hello World!
") - val result = driver.findElement(title("c => c.startsWith('foo')".asJsExpression())) + val result = driver.findElement(title(JsFunction("c => c.startsWith('foo')"))) assertEquals("Hello World!", result.text) } diff --git a/lib/src/test/kotlin/seleniumtestinglib/locators/NotFoundTest.kt b/lib/src/test/kotlin/seleniumtestinglib/locators/NotFoundTest.kt index d848d35..f6dbdd2 100644 --- a/lib/src/test/kotlin/seleniumtestinglib/locators/NotFoundTest.kt +++ b/lib/src/test/kotlin/seleniumtestinglib/locators/NotFoundTest.kt @@ -6,13 +6,13 @@ import org.junit.jupiter.params.provider.MethodSource import org.openqa.selenium.By import org.openqa.selenium.NoSuchElementException import seleniumtestinglib.Current.Page +import seleniumtestinglib.JsFunction import seleniumtestinglib.Role.SpinButton import seleniumtestinglib.TL.By.altText import seleniumtestinglib.TL.By.placeholderText import seleniumtestinglib.TL.By.role import seleniumtestinglib.TL.By.text import seleniumtestinglib.Value -import seleniumtestinglib.asJsExpression import seleniumtestinglib.driver import seleniumtestinglib.render import java.util.regex.Pattern.CASE_INSENSITIVE @@ -29,7 +29,7 @@ class NotFoundTest { "ByText(/a/i)" ), of( - placeholderText("el => true".asJsExpression()), + placeholderText(JsFunction("el => true")), "ByPlaceholderText(el => true)" ), of(